From 017c547d0dd93995a5f12609a987a6f861b3be18 Mon Sep 17 00:00:00 2001 From: zelophed Date: Wed, 8 Jun 2022 02:16:07 +0200 Subject: [PATCH] socially distanced camera - zoom out the third person view when mounting a train - added a command to manually zoom out as well :) - removed op permission requirement from highlight command --- .../trains/CameraDistanceModifier.java | 30 ++++++++++++++ .../simibubi/create/events/ClientEvents.java | 21 ++++++++++ .../foundation/command/AllCommands.java | 1 + .../command/CameraDistanceCommand.java | 41 +++++++++++++++++++ .../foundation/command/HighlightCommand.java | 1 - .../command/SConfigureConfigPacket.java | 17 +++++++- .../create/foundation/config/CClient.java | 7 ++++ .../create/foundation/mixin/CameraMixin.java | 40 ++++++++++++++++++ src/main/resources/create.mixins.json | 1 + 9 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/logistics/trains/CameraDistanceModifier.java create mode 100644 src/main/java/com/simibubi/create/foundation/command/CameraDistanceCommand.java create mode 100644 src/main/java/com/simibubi/create/foundation/mixin/CameraMixin.java diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/CameraDistanceModifier.java b/src/main/java/com/simibubi/create/content/logistics/trains/CameraDistanceModifier.java new file mode 100644 index 000000000..ce02689f2 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/trains/CameraDistanceModifier.java @@ -0,0 +1,30 @@ +package com.simibubi.create.content.logistics.trains; + +import com.simibubi.create.foundation.config.AllConfigs; +import com.simibubi.create.foundation.utility.animation.LerpedFloat; + +public class CameraDistanceModifier { + + private static final LerpedFloat multiplier = LerpedFloat.linear().startWithValue(1); + + public static float getMultiplier(float partialTicks) { + return multiplier.getValue(partialTicks); + } + + public static void tick() { + multiplier.tickChaser(); + } + + public static void reset() { + multiplier.chase(1, 0.1, LerpedFloat.Chaser.EXP); + } + + public static void zoomOut() { + zoomOut(AllConfigs.CLIENT.mountedZoomMultiplier.getF()); + } + + public static void zoomOut(float targetMultiplier) { + multiplier.chase(targetMultiplier, 0.075, LerpedFloat.Chaser.EXP); + } + +} diff --git a/src/main/java/com/simibubi/create/events/ClientEvents.java b/src/main/java/com/simibubi/create/events/ClientEvents.java index 486e07763..073b27a43 100644 --- a/src/main/java/com/simibubi/create/events/ClientEvents.java +++ b/src/main/java/com/simibubi/create/events/ClientEvents.java @@ -35,6 +35,8 @@ import com.simibubi.create.content.logistics.block.depot.EjectorTargetHandler; import com.simibubi.create.content.logistics.block.display.DisplayLinkBlockItem; import com.simibubi.create.content.logistics.block.mechanicalArm.ArmInteractionPointHandler; import com.simibubi.create.content.logistics.item.LinkedControllerClientHandler; +import com.simibubi.create.content.logistics.trains.CameraDistanceModifier; +import com.simibubi.create.content.logistics.trains.entity.CarriageContraptionEntity; import com.simibubi.create.content.logistics.trains.entity.CarriageCouplingRenderer; import com.simibubi.create.content.logistics.trains.entity.TrainRelocator; import com.simibubi.create.content.logistics.trains.management.edgePoint.TrackTargetingClient; @@ -89,6 +91,7 @@ import net.minecraftforge.client.event.RenderTooltipEvent; import net.minecraftforge.event.TickEvent.ClientTickEvent; import net.minecraftforge.event.TickEvent.Phase; import net.minecraftforge.event.TickEvent.RenderTickEvent; +import net.minecraftforge.event.entity.EntityMountEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.event.world.WorldEvent; @@ -165,6 +168,7 @@ public class ClientEvents { TrainRelocator.clientTick(); DisplayLinkBlockItem.clientTick(); CurvedTrackInteraction.clientTick(); + CameraDistanceModifier.tick(); } @SubscribeEvent @@ -275,6 +279,23 @@ public class ClientEvents { TurntableHandler.gameRenderTick(); } + @SubscribeEvent + public static void onMount(EntityMountEvent event) { + if (event.getEntityMounting() != Minecraft.getInstance().player) + return; + + if (event.isDismounting()) { + CameraDistanceModifier.reset(); + return; + } + + if (!event.isMounting() || !(event.getEntityBeingMounted() instanceof CarriageContraptionEntity carriage)) { + return; + } + + CameraDistanceModifier.zoomOut(); + } + protected static boolean isGameActive() { return !(Minecraft.getInstance().level == null || Minecraft.getInstance().player == null); } diff --git a/src/main/java/com/simibubi/create/foundation/command/AllCommands.java b/src/main/java/com/simibubi/create/foundation/command/AllCommands.java index ffcebb774..bb46cca08 100644 --- a/src/main/java/com/simibubi/create/foundation/command/AllCommands.java +++ b/src/main/java/com/simibubi/create/foundation/command/AllCommands.java @@ -55,6 +55,7 @@ public class AllCommands { return Commands.literal("util") .then(ReplaceInCommandBlocksCommand.register()) .then(ClearBufferCacheCommand.register()) + .then(CameraDistanceCommand.register()) .then(FlySpeedCommand.register()) //.then(KillTPSCommand.register()) .build(); diff --git a/src/main/java/com/simibubi/create/foundation/command/CameraDistanceCommand.java b/src/main/java/com/simibubi/create/foundation/command/CameraDistanceCommand.java new file mode 100644 index 000000000..770a1f2ef --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/command/CameraDistanceCommand.java @@ -0,0 +1,41 @@ +package com.simibubi.create.foundation.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.FloatArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.simibubi.create.foundation.networking.AllPackets; + +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.PacketDistributor; + +public class CameraDistanceCommand { + + public static ArgumentBuilder register() { + return Commands.literal("camera") + .then(Commands.literal("reset") + .executes(ctx -> { + ServerPlayer player = ctx.getSource().getPlayerOrException(); + AllPackets.channel.send( + PacketDistributor.PLAYER.with(() -> player), + new SConfigureConfigPacket(SConfigureConfigPacket.Actions.zoomMultiplier.name(), "1") + ); + + return Command.SINGLE_SUCCESS; + }) + ).then(Commands.argument("multiplier", FloatArgumentType.floatArg(0)) + .executes(ctx -> { + float multiplier = FloatArgumentType.getFloat(ctx, "multiplier"); + ServerPlayer player = ctx.getSource().getPlayerOrException(); + AllPackets.channel.send( + PacketDistributor.PLAYER.with(() -> player), + new SConfigureConfigPacket(SConfigureConfigPacket.Actions.zoomMultiplier.name(), String.valueOf(multiplier)) + ); + + return Command.SINGLE_SUCCESS; + }) + ); + } + +} diff --git a/src/main/java/com/simibubi/create/foundation/command/HighlightCommand.java b/src/main/java/com/simibubi/create/foundation/command/HighlightCommand.java index 60f78d7d3..496885d84 100644 --- a/src/main/java/com/simibubi/create/foundation/command/HighlightCommand.java +++ b/src/main/java/com/simibubi/create/foundation/command/HighlightCommand.java @@ -28,7 +28,6 @@ public class HighlightCommand { public static ArgumentBuilder register() { return Commands.literal("highlight") - .requires(cs -> cs.hasPermission(2)) .then(Commands.argument("pos", BlockPosArgument.blockPos()) .then(Commands.argument("players", EntityArgument.players()) .executes(ctx -> { diff --git a/src/main/java/com/simibubi/create/foundation/command/SConfigureConfigPacket.java b/src/main/java/com/simibubi/create/foundation/command/SConfigureConfigPacket.java index ea0a82028..29545696b 100644 --- a/src/main/java/com/simibubi/create/foundation/command/SConfigureConfigPacket.java +++ b/src/main/java/com/simibubi/create/foundation/command/SConfigureConfigPacket.java @@ -7,6 +7,7 @@ import org.apache.logging.log4j.LogManager; import com.simibubi.create.Create; import com.simibubi.create.content.contraptions.goggles.GoggleConfigScreen; +import com.simibubi.create.content.logistics.trains.CameraDistanceModifier; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.config.ui.BaseConfigScreen; import com.simibubi.create.foundation.config.ui.ConfigHelper; @@ -113,7 +114,8 @@ public class SConfigureConfigPacket extends SimplePacketBase { fixLighting(() -> Actions::experimentalLighting), overlayReset(() -> Actions::overlayReset), openPonder(() -> Actions::openPonder), - fabulousWarning(() -> Actions::fabulousWarning) + fabulousWarning(() -> Actions::fabulousWarning), + zoomMultiplier(() -> Actions::zoomMultiplier) ; @@ -212,6 +214,19 @@ public class SConfigureConfigPacket extends SimplePacketBase { Minecraft.getInstance().player.getUUID()); } + @OnlyIn(Dist.CLIENT) + private static void zoomMultiplier(String value) { + try { + float v = Float.parseFloat(value); + if (v <= 0) + return; + + CameraDistanceModifier.zoomOut(v); + } catch (NumberFormatException ignored) { + Create.LOGGER.debug("Received non-float value {} in zoom packet, ignoring", value); + } + } + private static MutableComponent boolToText(boolean b) { return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED); diff --git a/src/main/java/com/simibubi/create/foundation/config/CClient.java b/src/main/java/com/simibubi/create/foundation/config/CClient.java index 9161ba3fb..4bd6662c5 100644 --- a/src/main/java/com/simibubi/create/foundation/config/CClient.java +++ b/src/main/java/com/simibubi/create/foundation/config/CClient.java @@ -73,6 +73,10 @@ public class CClient extends ConfigBase { public final ConfigFloat ambientVolumeCap = f(.1f, 0, 1, "ambientVolumeCap", Comments.ambientVolumeCap); + //train group + public final ConfigGroup trains = group(1, "trains"); + public final ConfigFloat mountedZoomMultiplier = f(3, 0, "mountedZoomMultiplier", Comments.mountedZoomMultiplier); + @Override public String getName() { return "client"; @@ -138,6 +142,9 @@ public class CClient extends ConfigBase { static String sound = "Sound settings"; static String enableAmbientSounds = "Make cogs rumble and machines clatter."; static String ambientVolumeCap = "Maximum volume modifier of Ambient noise"; + + static String trains = "Railway related settings"; + static String mountedZoomMultiplier = "How far away the Camera should zoom when seated on a train"; } } diff --git a/src/main/java/com/simibubi/create/foundation/mixin/CameraMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/CameraMixin.java new file mode 100644 index 000000000..89c792ba6 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/mixin/CameraMixin.java @@ -0,0 +1,40 @@ +package com.simibubi.create.foundation.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.simibubi.create.content.logistics.trains.CameraDistanceModifier; +import com.simibubi.create.foundation.utility.AnimationTickHolder; + +import net.minecraft.client.Camera; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.BlockGetter; + +@Mixin(Camera.class) +public abstract class CameraMixin { + + @Shadow + private double getMaxZoom(double pStartingDistance) { + throw new AssertionError(); + } + + @Shadow + protected void move(double pDistanceOffset, double pVerticalOffset, double pHorizontalOffset) { + throw new AssertionError(); + } + + @Inject( + method = "Lnet/minecraft/client/Camera;setup(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/world/entity/Entity;ZZF)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;move(DDD)V", ordinal = 0), + cancellable = true + ) + public void modifySetup(BlockGetter pLevel, Entity pEntity, boolean pDetached, boolean pThirdPersonReverse, float pPartialTick, CallbackInfo ci) { + move(-this.getMaxZoom(4.0D * CameraDistanceModifier.getMultiplier(AnimationTickHolder.getPartialTicks())), 0, 0); + ci.cancel(); + } + + +} diff --git a/src/main/resources/create.mixins.json b/src/main/resources/create.mixins.json index d362e164e..2dfecaaf2 100644 --- a/src/main/resources/create.mixins.json +++ b/src/main/resources/create.mixins.json @@ -14,6 +14,7 @@ "accessor.ServerLevelAccessor" ], "client": [ + "CameraMixin", "DestroyProgressMixin", "EntityContraptionInteractionMixin", "FixNormalScalingMixin",