From cb238ec13027b58755fc5a2cb67c9024f8071907 Mon Sep 17 00:00:00 2001 From: Howard Stark Date: Thu, 2 Aug 2018 03:41:21 -0400 Subject: [PATCH] Add default-value HashMap --- .../java/baritone/bot/InputOverrideHandler.java | 9 +++++---- src/main/java/baritone/bot/pathing/State.java | 8 -------- .../java/baritone/bot/utils/DefaultHashMap.java | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) delete mode 100644 src/main/java/baritone/bot/pathing/State.java create mode 100644 src/main/java/baritone/bot/utils/DefaultHashMap.java diff --git a/src/main/java/baritone/bot/InputOverrideHandler.java b/src/main/java/baritone/bot/InputOverrideHandler.java index 4db6bb28..746aa39d 100755 --- a/src/main/java/baritone/bot/InputOverrideHandler.java +++ b/src/main/java/baritone/bot/InputOverrideHandler.java @@ -1,5 +1,6 @@ package baritone.bot; +import baritone.bot.utils.DefaultHashMap; import baritone.bot.utils.Helper; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -22,12 +23,12 @@ public final class InputOverrideHandler implements Helper { /** * Maps keybinds to whether or not we are forcing their state down. */ - private final Map inputForceStateMap = new HashMap<>(); + private final Map inputForceStateMap = new DefaultHashMap<>(false); /** * Maps keycodes to whether or not we are forcing their state down. */ - private final Map keyCodeForceStateMap = new HashMap<>(); + private final Map keyCodeForceStateMap = new DefaultHashMap<>(false); /** * Returns whether or not we are forcing down the specified {@link KeyBinding}. @@ -36,7 +37,7 @@ public final class InputOverrideHandler implements Helper { * @return Whether or not it is being forced down */ public final boolean isInputForcedDown(KeyBinding key) { - return inputForceStateMap.computeIfAbsent(key, k -> false); + return inputForceStateMap.get(key); } /** @@ -55,7 +56,7 @@ public final class InputOverrideHandler implements Helper { * @return Whether or not the specified key is down or overridden. */ public boolean isKeyDown(int keyCode) { - return Keyboard.isKeyDown(keyCode) || keyCodeForceStateMap.computeIfAbsent(keyCode, k -> false); + return Keyboard.isKeyDown(keyCode) || keyCodeForceStateMap.get(keyCode); } /** diff --git a/src/main/java/baritone/bot/pathing/State.java b/src/main/java/baritone/bot/pathing/State.java deleted file mode 100644 index da26ccb5..00000000 --- a/src/main/java/baritone/bot/pathing/State.java +++ /dev/null @@ -1,8 +0,0 @@ -package baritone.bot.pathing; - -public class State { - - public State() { - - } -} diff --git a/src/main/java/baritone/bot/utils/DefaultHashMap.java b/src/main/java/baritone/bot/utils/DefaultHashMap.java new file mode 100644 index 00000000..4e0d9073 --- /dev/null +++ b/src/main/java/baritone/bot/utils/DefaultHashMap.java @@ -0,0 +1,14 @@ +package baritone.bot.utils; + +import java.util.HashMap; + +public class DefaultHashMap extends HashMap { + protected V defaultValue; + public DefaultHashMap(V defaultValue) { + this.defaultValue = defaultValue; + } + @Override + public V get(Object k) { + return containsKey(k) ? super.get(k) : defaultValue; + } +} \ No newline at end of file