Fix issue with InputOverrideHandler that could cause while loop holdups

This commit is contained in:
Brady 2018-08-01 18:25:23 -07:00
parent bc7cd2efcc
commit 6a686d11f3
No known key found for this signature in database
GPG Key ID: 73A788379A197567
5 changed files with 36 additions and 34 deletions

View File

@ -1,7 +1,8 @@
package baritone.bot;
import baritone.bot.event.IGameEventListener;
import baritone.Baritone;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
/**
* @author Brady
@ -13,6 +14,23 @@ public final class GameEventHandler implements IGameEventListener {
@Override
public final void onTick() {
Baritone.onTick();
baritone.Baritone.onTick();
}
@Override
public void onProcessKeyBinds() {
InputOverrideHandler inputHandler = Baritone.INSTANCE.getInputOverrideHandler();
// Simulate the key being held down this tick
for (InputOverrideHandler.Input input : InputOverrideHandler.Input.values()) {
KeyBinding keyBinding = input.getKeyBinding();
if (inputHandler.isInputForcedDown(keyBinding) && !keyBinding.isKeyDown()) {
int keyCode = keyBinding.getKeyCode();
if (keyCode < Keyboard.KEYBOARD_SIZE)
KeyBinding.onTick(keyCode < 0 ? keyCode + 100 : keyCode);
}
}
}
}

View File

@ -25,7 +25,7 @@ public final class InputOverrideHandler implements Helper {
private final Map<KeyBinding, Boolean> inputForceStateMap = new HashMap<>();
/**
* Maps keycodes to whether or not we are forcing their state down
* Maps keycodes to whether or not we are forcing their state down.
*/
private final Map<Integer, Boolean> keyCodeForceStateMap = new HashMap<>();
@ -52,7 +52,7 @@ public final class InputOverrideHandler implements Helper {
/**
* A redirection in multiple places of {@link Keyboard#isKeyDown}.
*
* @return Whether or not the specified key is down or overriden.
* @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);

View File

@ -9,7 +9,12 @@ import net.minecraft.client.Minecraft;
public interface IGameEventListener {
/**
* Run once per game tick from {@link Minecraft#runTick}.
* Run once per game tick from {@link Minecraft#runTick()}
*/
void onTick();
/**
* Run once per game tick from {@link Minecraft#processKeyBinds()}
*/
void onProcessKeyBinds();
}

View File

@ -2,11 +2,9 @@ package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.IntHashMap;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
@ -16,33 +14,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(KeyBinding.class)
public abstract class MixinKeyBinding {
@Redirect(
method = "onTick",
at = @At(
value = "INVOKE",
target = "net/minecraft/util/IntHashMap.lookup(I)Ljava/lang/Object;"
)
)
private static Object lookup(IntHashMap<KeyBinding> HASH, int keyCode) {
KeyBinding keyBinding = HASH.lookup(keyCode);
// If we're overriding the key state, we don't want to be incrementing the pressTime
if (keyBinding != null && Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown(keyBinding))
return null;
return keyBinding;
}
@Inject(
method = "isPressed",
at = @At("HEAD"),
cancellable = true
)
private void isPressed(CallbackInfoReturnable<Boolean> cir) {
if (Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown((KeyBinding) (Object) this))
cir.setReturnValue(true);
}
@Inject(
method = "isKeyDown",
at = @At("HEAD"),

View File

@ -57,6 +57,14 @@ public class MixinMinecraft {
return Baritone.INSTANCE.getInputOverrideHandler().isKeyDown(keyCode);
}
@Inject(
method = "processKeyBinds",
at = @At("HEAD")
)
private void runTickKeyboard(CallbackInfo ci) {
Baritone.INSTANCE.getGameEventHandler().onProcessKeyBinds();
}
@Redirect(
method = {
"setIngameFocus",