getOrDefault instead of custom hashmap

This commit is contained in:
Leijurv 2018-08-02 09:20:35 -04:00
parent bf569f6f66
commit a60b398b0c
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
4 changed files with 15 additions and 25 deletions

View File

@ -1,6 +1,5 @@
package baritone.bot;
import baritone.bot.utils.DefaultHashMap;
import baritone.bot.utils.Helper;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
@ -23,12 +22,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 DefaultHashMap<>(false);
private final Map<KeyBinding, Boolean> inputForceStateMap = new HashMap<>();
/**
* Maps keycodes to whether or not we are forcing their state down.
*/
private final Map<Integer, Boolean> keyCodeForceStateMap = new DefaultHashMap<>(false);
private final Map<Integer, Boolean> keyCodeForceStateMap = new HashMap<>();
/**
* Returns whether or not we are forcing down the specified {@link KeyBinding}.
@ -37,7 +36,7 @@ public final class InputOverrideHandler implements Helper {
* @return Whether or not it is being forced down
*/
public final boolean isInputForcedDown(KeyBinding key) {
return inputForceStateMap.get(key);
return inputForceStateMap.getOrDefault(key, false);
}
/**
@ -56,7 +55,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.get(keyCode);
return Keyboard.isKeyDown(keyCode) || keyCodeForceStateMap.getOrDefault(keyCode, false);
}
/**

View File

@ -6,6 +6,10 @@ import java.util.Collection;
import java.util.List;
public interface IPath {
/**
*
*/
List<BlockPos> movements();
/**
* All positions along the way.

View File

@ -1,17 +1,17 @@
package baritone.bot.pathing.action;
import baritone.bot.InputOverrideHandler.Input;
import baritone.bot.utils.DefaultHashMap;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos;
import java.util.HashMap;
import java.util.Map;
public class ActionState {
protected ActionStatus status;
public ActionGoal goal;
protected final Map<Input, Boolean> inputState = new DefaultHashMap<>(false);
protected final Map<Input, Boolean> inputState = new HashMap<>();
public ActionState setStatus(ActionStatus status) {
this.status = status;
@ -53,10 +53,14 @@ public class ActionState {
}
public ActionState setInput(Input input, boolean forced) {
this.inputState.put(input, forced);
inputState.put(input, forced);
return this;
}
public boolean getInput(Input input) {
return inputState.getOrDefault(input, false);
}
public enum ActionStatus {
WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED;
}

View File

@ -1,17 +0,0 @@
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;
}
}