Add default-value HashMap

This commit is contained in:
Howard Stark 2018-08-02 03:41:21 -04:00
parent b6b67fdd71
commit cb238ec130
No known key found for this signature in database
GPG Key ID: 9FA4E350B33067F3
3 changed files with 19 additions and 12 deletions

View File

@ -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<KeyBinding, Boolean> inputForceStateMap = new HashMap<>();
private final Map<KeyBinding, Boolean> inputForceStateMap = new DefaultHashMap<>(false);
/**
* Maps keycodes to whether or not we are forcing their state down.
*/
private final Map<Integer, Boolean> keyCodeForceStateMap = new HashMap<>();
private final Map<Integer, Boolean> 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);
}
/**

View File

@ -1,8 +0,0 @@
package baritone.bot.pathing;
public class State {
public State() {
}
}

View File

@ -0,0 +1,14 @@
package baritone.bot.utils;
import java.util.HashMap;
public class DefaultHashMap<K,V> extends HashMap<K,V> {
protected V defaultValue;
public DefaultHashMap(V defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public V get(Object k) {
return containsKey(k) ? super.get(k) : defaultValue;
}
}