From 07dcb7660bcf0812bf49d7d0c5e7b3c7bd72f81d Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 2 Nov 2024 15:32:39 +0000 Subject: [PATCH 1/2] refactor: introduce a `DummyInput` wrapper `DummyInput` can be used to provide an empty `Input` instance that remembers some state from a previous `Input` instance. In the future we may decide to persist additional state, such as sprint or jump. Currently only sneak is persisted. --- .../src/main/java/net/xolt/freecam/Freecam.java | 13 +++++++------ .../java/net/xolt/freecam/util/DummyInput.java | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/net/xolt/freecam/util/DummyInput.java diff --git a/common/src/main/java/net/xolt/freecam/Freecam.java b/common/src/main/java/net/xolt/freecam/Freecam.java index a03f5393..2cc21e00 100644 --- a/common/src/main/java/net/xolt/freecam/Freecam.java +++ b/common/src/main/java/net/xolt/freecam/Freecam.java @@ -14,6 +14,7 @@ import net.xolt.freecam.config.ModConfig; import net.xolt.freecam.tripod.TripodRegistry; import net.xolt.freecam.tripod.TripodSlot; +import net.xolt.freecam.util.DummyInput; import net.xolt.freecam.util.FreeCamera; import net.xolt.freecam.util.FreecamPosition; import net.xolt.freecam.variant.api.BuildVariant; @@ -46,9 +47,8 @@ public static void preTick(Minecraft mc) { if (isEnabled()) { // Prevent player from being controlled when freecam is enabled if (mc.player != null && mc.player.input instanceof KeyboardInput && !isPlayerControlEnabled()) { - Input input = new Input(); - input.shiftKeyDown = mc.player.input.shiftKeyDown; // Makes player continue to sneak after freecam is enabled. - mc.player.input = input; + // Makes player continue to sneak after freecam is enabled. + mc.player.input = new DummyInput(mc.player.input); } mc.gameRenderer.setRenderHand(ModConfig.INSTANCE.visual.showHand); @@ -155,13 +155,14 @@ public static void switchControls() { return; } + playerControlEnabled = !playerControlEnabled; if (playerControlEnabled) { - freeCamera.input = new KeyboardInput(MC.options); - } else { MC.player.input = new KeyboardInput(MC.options); freeCamera.input = new Input(); + } else { + freeCamera.input = new KeyboardInput(MC.options); + // We set MC.player.input every tick anyway, so don't bother here } - playerControlEnabled = !playerControlEnabled; } private static void onEnableTripod(TripodSlot tripod) { diff --git a/common/src/main/java/net/xolt/freecam/util/DummyInput.java b/common/src/main/java/net/xolt/freecam/util/DummyInput.java new file mode 100644 index 00000000..7476968a --- /dev/null +++ b/common/src/main/java/net/xolt/freecam/util/DummyInput.java @@ -0,0 +1,15 @@ +package net.xolt.freecam.util; + +import net.minecraft.client.player.Input; + +public class DummyInput extends Input { + + public DummyInput(Input old) { + this(old.shiftKeyDown); + } + + public DummyInput(boolean isSneaking) { + // Makes player continue to sneak after freecam is enabled. + this.shiftKeyDown = isSneaking; + } +} From 77d45c6b0ef9d76b435c9f8b617d5fef43083946 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 2 Nov 2024 15:45:12 +0000 Subject: [PATCH 2/2] refactor: no need to reset camera's input Resetting `freeCamera`'s input is pointless immediately before setting `freeCamera = null`. --- common/src/main/java/net/xolt/freecam/Freecam.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/net/xolt/freecam/Freecam.java b/common/src/main/java/net/xolt/freecam/Freecam.java index 2cc21e00..71b8d896 100644 --- a/common/src/main/java/net/xolt/freecam/Freecam.java +++ b/common/src/main/java/net/xolt/freecam/Freecam.java @@ -246,7 +246,6 @@ private static void onDisable() { MC.setCameraEntity(MC.player); playerControlEnabled = false; freeCamera.despawn(); - freeCamera.input = new Input(); freeCamera = null; if (MC.player != null) {