2019-02-23 18:27:24 +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 Lesser 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package baritone.utils;
|
|
|
|
|
|
|
|
import baritone.Baritone;
|
|
|
|
import baritone.api.BaritoneAPI;
|
|
|
|
import baritone.api.event.events.TickEvent;
|
|
|
|
import baritone.api.utils.IInputOverrideHandler;
|
|
|
|
import baritone.api.utils.input.Input;
|
|
|
|
import baritone.behavior.Behavior;
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.util.MovementInputFromOptions;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @author Brady
|
|
|
|
* @since 7/31/2018
|
|
|
|
*/
|
|
|
|
public final class InputOverrideHandler extends Behavior implements IInputOverrideHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps inputs to whether or not we are forcing their state down.
|
|
|
|
*/
|
|
|
|
private final Map<Input, Boolean> inputForceStateMap = new HashMap<>();
|
|
|
|
|
|
|
|
private final BlockBreakHelper blockBreakHelper;
|
2019-02-26 22:57:44 +00:00
|
|
|
private final BlockPlaceHelper blockPlaceHelper;
|
2019-02-23 18:27:24 +00:00
|
|
|
|
|
|
|
public InputOverrideHandler(Baritone baritone) {
|
|
|
|
super(baritone);
|
|
|
|
this.blockBreakHelper = new BlockBreakHelper(baritone.getPlayerContext());
|
2019-02-26 22:57:44 +00:00
|
|
|
this.blockPlaceHelper = new BlockPlaceHelper(baritone.getPlayerContext());
|
2019-02-23 18:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public final boolean isInputForcedDown(Input input) {
|
|
|
|
return input == null ? false : this.inputForceStateMap.getOrDefault(input, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether or not the specified {@link Input} is being forced down.
|
|
|
|
*
|
|
|
|
* @param input The {@link Input}
|
|
|
|
* @param forced Whether or not the state is being forced
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public final void setInputForceState(Input input, boolean forced) {
|
|
|
|
this.inputForceStateMap.put(input, forced);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the override state for all keys
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public final void clearAllKeys() {
|
|
|
|
this.inputForceStateMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final void onTick(TickEvent event) {
|
|
|
|
if (event.getType() == TickEvent.Type.OUT) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-26 22:57:44 +00:00
|
|
|
if (isInputForcedDown(Input.CLICK_LEFT)) {
|
|
|
|
setInputForceState(Input.CLICK_RIGHT, false);
|
|
|
|
}
|
2019-02-23 18:27:24 +00:00
|
|
|
blockBreakHelper.tick(isInputForcedDown(Input.CLICK_LEFT));
|
2019-02-26 22:57:44 +00:00
|
|
|
blockPlaceHelper.tick(isInputForcedDown(Input.CLICK_RIGHT));
|
2019-02-23 18:27:24 +00:00
|
|
|
|
|
|
|
if (inControl()) {
|
|
|
|
if (ctx.player().movementInput.getClass() != PlayerMovementInput.class) {
|
|
|
|
ctx.player().movementInput = new PlayerMovementInput(this);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ctx.player().movementInput.getClass() == PlayerMovementInput.class) { // allow other movement inputs that aren't this one, e.g. for a freecam
|
|
|
|
ctx.player().movementInput = new MovementInputFromOptions(Minecraft.getMinecraft().gameSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// only set it if it was previously incorrect
|
|
|
|
// gotta do it this way, or else it constantly thinks you're beginning a double tap W sprint lol
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean inControl() {
|
2019-02-26 22:57:44 +00:00
|
|
|
// if we are not primary (a bot) we should set the movementinput even when idle (not pathing)
|
2019-02-23 18:27:24 +00:00
|
|
|
return baritone.getPathingBehavior().isPathing() || baritone != BaritoneAPI.getProvider().getPrimaryBaritone();
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockBreakHelper getBlockBreakHelper() {
|
|
|
|
return blockBreakHelper;
|
|
|
|
}
|
2019-09-19 20:30:40 +00:00
|
|
|
}
|