Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introduce a DummyInput wrapper #253

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions common/src/main/java/net/xolt/freecam/Freecam.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -245,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) {
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/net/xolt/freecam/util/DummyInput.java
Original file line number Diff line number Diff line change
@@ -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;
}
}