From d462d17308d2ac2754f73a64b61ac343677e0f30 Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Sat, 6 May 2023 21:12:12 +0200 Subject: No more inv walk (#67) --- .../garden/farming/GardenCustomKeybinds.kt | 54 ++++++++-------------- .../mixins/transformers/AccessorKeyBinding.java | 5 ++ .../mixins/transformers/MixinKeyBinding.java | 10 ++-- 3 files changed, 32 insertions(+), 37 deletions(-) (limited to 'src/main/java/at/hannibal2') diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt index b920cdba6..a96ccf62c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt @@ -2,15 +2,13 @@ package at.hannibal2.skyhanni.features.garden.farming import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.features.Garden +import at.hannibal2.skyhanni.events.GardenToolChangeEvent import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.mixins.transformers.AccessorKeyBinding import net.minecraft.client.Minecraft -import net.minecraft.client.gui.inventory.GuiEditSign import net.minecraft.client.settings.KeyBinding -import org.lwjgl.input.Keyboard -import org.lwjgl.input.Mouse +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.spongepowered.asm.mixin.injection.callback.CallbackInfo -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable import java.util.* object GardenCustomKeybinds { @@ -18,7 +16,7 @@ object GardenCustomKeybinds { private val mcSettings get() = Minecraft.getMinecraft().gameSettings private val map: MutableMap Int> = IdentityHashMap() - private var lastWindowOpenTime = 0L + private val cache: MutableMap = mutableMapOf() init { map[mcSettings.keyBindAttack] = { shConfig.keyBindAttack } @@ -33,47 +31,35 @@ object GardenCustomKeybinds { private fun isEnabled() = GardenAPI.inGarden() && shConfig.keyBindEnabled - private fun isActive(): Boolean { - if (!isEnabled()) return false - if (GardenAPI.toolInHand == null) return false + private fun isActive() = isEnabled() && GardenAPI.toolInHand != null - if (Minecraft.getMinecraft().currentScreen != null) { - if (Minecraft.getMinecraft().currentScreen is GuiEditSign) { - lastWindowOpenTime = System.currentTimeMillis() - } - return false - } - - // TODO remove workaround - if (System.currentTimeMillis() < lastWindowOpenTime + 300) return false - - return true - } - - private fun isHeld(keyCode: Int): Boolean { - if (keyCode == 0) return false - return if (keyCode < 0) { - Mouse.isButtonDown(keyCode + 100) - } else { - Keyboard.isKeyDown(keyCode) + @SubscribeEvent + fun onToolChange(event: GardenToolChangeEvent) { + map.forEach { (keyBinding, override) -> + keyBinding as AccessorKeyBinding + val keyCode = if (isActive()) override() else keyBinding.keyCode + keyBinding.pressed_skyhanni = cache[keyCode] ?: false } } @JvmStatic - fun isKeyDown(keyBinding: KeyBinding, cir: CallbackInfoReturnable) { + fun onTick(keyCode: Int, ci: CallbackInfo) { + if (keyCode == 0) return if (!isActive()) return - val override = map[keyBinding] ?: return - val keyCode = override() - cir.returnValue = isHeld(keyCode) + val keyBinding = map.entries.firstOrNull { it.value() == keyCode }?.key ?: return + ci.cancel() + keyBinding as AccessorKeyBinding + keyBinding.pressTime_skyhanni++ } @JvmStatic - fun onTick(keyCode: Int, ci: CallbackInfo) { - if (!isActive()) return + fun setKeyBindState(keyCode: Int, pressed: Boolean, ci: CallbackInfo) { if (keyCode == 0) return + cache[keyCode] = pressed + if (!isActive()) return val keyBinding = map.entries.firstOrNull { it.value() == keyCode }?.key ?: return ci.cancel() keyBinding as AccessorKeyBinding - keyBinding.pressTime_skyhanni++ + keyBinding.pressed_skyhanni = pressed } } \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java index 53abc49dc..a06f3c339 100644 --- a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java @@ -7,6 +7,11 @@ import org.spongepowered.asm.mixin.gen.Invoker; @Mixin(KeyBinding.class) public interface AccessorKeyBinding { + @Accessor("pressed") + boolean getPressed_skyhanni(); + + @Accessor("pressed") + void setPressed_skyhanni(boolean newVal); @Accessor("pressTime") int getPressTime_skyhanni(); diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java index 35a9bc780..8862b57e4 100644 --- a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java @@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.mixins.transformers; import at.hannibal2.skyhanni.features.garden.farming.GardenCustomKeybinds; import net.minecraft.client.settings.KeyBinding; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -10,13 +11,16 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(KeyBinding.class) public class MixinKeyBinding { + @Shadow private int keyCode; + @Inject(method = "onTick", at = @At("HEAD"), cancellable = true) private static void noOnTick(int keyCode, CallbackInfo ci) { GardenCustomKeybinds.onTick(keyCode, ci); } - @Inject(method = "isKeyDown", at = @At("HEAD"), cancellable = true) - public void noIsKeyDown(CallbackInfoReturnable cir) { - GardenCustomKeybinds.isKeyDown((KeyBinding) (Object) this, cir); + @Inject(method = "setKeyBindState", at = @At("HEAD"), cancellable = true) + private static void nosetKeyBindState(int keyCode, boolean pressed, CallbackInfo ci) { + GardenCustomKeybinds.setKeyBindState(keyCode, pressed, ci); } + } -- cgit