From a83074e77327e677ab0896356be172108e94e298 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 11 Nov 2018 14:20:38 -0600 Subject: [PATCH] Map Input to state in InputOverrideHandler --- src/main/java/baritone/behavior/Behavior.java | 2 +- .../baritone/utils/InputOverrideHandler.java | 49 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/baritone/behavior/Behavior.java b/src/main/java/baritone/behavior/Behavior.java index 154897d1..66434d7a 100644 --- a/src/main/java/baritone/behavior/Behavior.java +++ b/src/main/java/baritone/behavior/Behavior.java @@ -21,7 +21,7 @@ import baritone.Baritone; import baritone.api.behavior.IBehavior; /** - * A type of game event listener that can be toggled. + * A type of game event listener that is given {@link Baritone} instance context. * * @author Brady * @since 8/1/2018 6:29 PM diff --git a/src/main/java/baritone/utils/InputOverrideHandler.java b/src/main/java/baritone/utils/InputOverrideHandler.java index b42b0ee1..9c3eed7f 100755 --- a/src/main/java/baritone/utils/InputOverrideHandler.java +++ b/src/main/java/baritone/utils/InputOverrideHandler.java @@ -23,6 +23,7 @@ import baritone.behavior.Behavior; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -41,13 +42,9 @@ public final class InputOverrideHandler extends Behavior implements Helper { } /** - * Maps keybinds to whether or not we are forcing their state down. + * Maps inputs to whether or not we are forcing their state down. */ - private final Map inputForceStateMap = new HashMap<>(); - - public final void clearAllKeys() { - inputForceStateMap.clear(); - } + private final Map inputForceStateMap = new HashMap<>(); /** * Returns whether or not we are forcing down the specified {@link KeyBinding}. @@ -56,7 +53,17 @@ public final class InputOverrideHandler extends Behavior implements Helper { * @return Whether or not it is being forced down */ public final boolean isInputForcedDown(KeyBinding key) { - return inputForceStateMap.getOrDefault(key, false); + return isInputForcedDown(Input.getInputForBind(key)); + } + + /** + * Returns whether or not we are forcing down the specified {@link Input}. + * + * @param input The input + * @return Whether or not it is being forced down + */ + public final boolean isInputForcedDown(Input input) { + return input == null ? false : this.inputForceStateMap.getOrDefault(input, false); } /** @@ -66,7 +73,14 @@ public final class InputOverrideHandler extends Behavior implements Helper { * @param forced Whether or not the state is being forced */ public final void setInputForceState(Input input, boolean forced) { - inputForceStateMap.put(input.getKeyBinding(), forced); + this.inputForceStateMap.put(input, forced); + } + + /** + * Clears the override state for all keys + */ + public final void clearAllKeys() { + this.inputForceStateMap.clear(); } @Override @@ -97,7 +111,9 @@ public final class InputOverrideHandler extends Behavior implements Helper { } /** - * An {@link Enum} representing the possible inputs that we may want to force. + * An {@link Enum} representing the inputs that control the player's + * behavior. This includes moving, interacting with blocks, jumping, + * sneaking, and sprinting. */ public enum Input { @@ -146,6 +162,11 @@ public final class InputOverrideHandler extends Behavior implements Helper { */ SPRINT(mc.gameSettings.keyBindSprint); + /** + * Map of {@link KeyBinding} to {@link Input}. Values should be queried through {@link #getInputForBind(KeyBinding)} + */ + private static final Map bindToInputMap = new HashMap<>(); + /** * The actual game {@link KeyBinding} being forced. */ @@ -161,5 +182,15 @@ public final class InputOverrideHandler extends Behavior implements Helper { public final KeyBinding getKeyBinding() { return this.keyBinding; } + + /** + * Finds the {@link Input} constant that is associated with the specified {@link KeyBinding}. + * + * @param binding The {@link KeyBinding} to find the associated {@link Input} for + * @return The {@link Input} associated with the specified {@link KeyBinding} + */ + public static Input getInputForBind(KeyBinding binding) { + return bindToInputMap.computeIfAbsent(binding, b -> Arrays.stream(values()).filter(input -> input.keyBinding == b).findFirst().orElse(null)); + } } }