baritone/src/main/java/baritone/bot/utils/InputOverrideHandler.java

173 lines
5.1 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
2018-08-08 03:16:53 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-14 22:04:41 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot.utils;
2018-08-01 17:10:48 +00:00
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
import java.util.HashMap;
import java.util.Map;
/**
* An interface with the game's control system allowing the ability to
* force down certain controls, having the same effect as if we were actually
* physically forcing down the assigned key.
2018-08-01 17:10:48 +00:00
*
* @author Brady
* @since 7/31/2018 11:20 PM
*/
public final class InputOverrideHandler implements Helper {
2018-08-14 22:04:41 +00:00
public InputOverrideHandler() {}
2018-08-01 17:10:48 +00:00
/**
* Maps keybinds to whether or not we are forcing their state down.
2018-08-01 17:10:48 +00:00
*/
2018-08-02 13:20:35 +00:00
private final Map<KeyBinding, Boolean> inputForceStateMap = new HashMap<>();
2018-08-01 17:10:48 +00:00
/**
* Maps keycodes to whether or not we are forcing their state down.
2018-08-01 17:10:48 +00:00
*/
2018-08-02 13:20:35 +00:00
private final Map<Integer, Boolean> keyCodeForceStateMap = new HashMap<>();
2018-08-01 17:10:48 +00:00
2018-08-07 04:07:15 +00:00
public final void clearAllKeys() {
inputForceStateMap.clear();
keyCodeForceStateMap.clear();
}
2018-08-01 17:10:48 +00:00
/**
* Returns whether or not we are forcing down the specified {@link KeyBinding}.
*
* @param key The KeyBinding object
* @return Whether or not it is being forced down
*/
public final boolean isInputForcedDown(KeyBinding key) {
2018-08-02 13:20:35 +00:00
return inputForceStateMap.getOrDefault(key, false);
2018-08-01 17:10:48 +00:00
}
/**
* Sets whether or not the specified {@link Input} is being forced down.
*
2018-08-06 22:53:35 +00:00
* @param input The {@link Input}
2018-08-01 17:10:48 +00:00
* @param forced Whether or not the state is being forced
*/
public final void setInputForceState(Input input, boolean forced) {
2018-08-06 22:53:35 +00:00
if (!forced)
2018-08-06 01:52:55 +00:00
System.out.println(input);
2018-08-01 17:10:48 +00:00
inputForceStateMap.put(input.getKeyBinding(), forced);
}
/**
* A redirection in multiple places of {@link Keyboard#isKeyDown}.
*
* @return Whether or not the specified key is down or overridden.
2018-08-01 17:10:48 +00:00
*/
public boolean isKeyDown(int keyCode) {
2018-08-02 13:20:35 +00:00
return Keyboard.isKeyDown(keyCode) || keyCodeForceStateMap.getOrDefault(keyCode, false);
2018-08-01 17:10:48 +00:00
}
/**
* Sets whether or not the specified key code is being forced down.
*
* @param keyCode The key code
2018-08-06 22:53:35 +00:00
* @param forced Whether or not the state is being forced
2018-08-01 17:10:48 +00:00
*/
public final void setKeyForceState(int keyCode, boolean forced) {
keyCodeForceStateMap.put(keyCode, forced);
}
/**
* An {@link Enum} representing the possible inputs that we may want to force.
*/
public enum Input {
/**
* The move forward input
*/
MOVE_FORWARD(mc.gameSettings.keyBindForward),
/**
* The move back input
*/
MOVE_BACK(mc.gameSettings.keyBindBack),
/**
* The move left input
*/
MOVE_LEFT(mc.gameSettings.keyBindLeft),
/**
* The move right input
*/
MOVE_RIGHT(mc.gameSettings.keyBindRight),
/**
* The attack input
*/
CLICK_LEFT(mc.gameSettings.keyBindAttack),
/**
* The use item input
*/
2018-08-01 18:35:44 +00:00
CLICK_RIGHT(mc.gameSettings.keyBindUseItem),
2018-08-01 17:10:48 +00:00
2018-08-01 18:35:44 +00:00
/**
* The jump input
*/
2018-08-06 22:53:35 +00:00
JUMP(mc.gameSettings.keyBindJump),
/**
* The sneak input
*/
SNEAK(mc.gameSettings.keyBindSneak);
2018-08-01 17:10:48 +00:00
/**
* The actual game {@link KeyBinding} being forced.
*/
private KeyBinding keyBinding;
Input(KeyBinding keyBinding) {
this.keyBinding = keyBinding;
}
/**
* @return The actual game {@link KeyBinding} being forced.
*/
public final KeyBinding getKeyBinding() {
return this.keyBinding;
}
}
}