aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt109
-rw-r--r--src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java9
-rw-r--r--src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java22
-rw-r--r--src/main/resources/mixins.skyhanni.json1
5 files changed, 62 insertions, 81 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
index cf6ff22b9..de4bc0c34 100644
--- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
+++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
@@ -247,7 +247,7 @@ public class SkyHanniMod {
loadModule(new GardenVisitorTimer());
loadModule(new GardenNextPlotPrice());
loadModule(new GardenCropMilestoneDisplay());
- loadModule(new GardenCustomKeybinds());
+ loadModule(GardenCustomKeybinds.INSTANCE);
loadModule(new ChickenHeadTimer());
loadModule(new GardenOptimalSpeed());
loadModule(new GardenDeskInSBMenu());
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 e5338cebc..25491f068 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,21 +2,21 @@ 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 io.github.moulberry.moulconfig.internal.KeybindHelper
import net.minecraft.client.Minecraft
import net.minecraft.client.settings.KeyBinding
-import net.minecraftforge.event.world.WorldEvent
-import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.lwjgl.input.Keyboard
+import org.lwjgl.input.Mouse
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
+import java.util.*
-class GardenCustomKeybinds {
+object GardenCustomKeybinds {
private val shConfig: Garden get() = SkyHanniMod.feature.garden
private val mcSettings get() = Minecraft.getMinecraft().gameSettings
- private val cache = mutableMapOf<KeyBinding, Int>()
- private val map = mutableMapOf<KeyBinding, () -> Int>()
+ private val map: MutableMap<KeyBinding, () -> Int> = IdentityHashMap()
init {
map[mcSettings.keyBindAttack] = { shConfig.keyBindAttack }
@@ -26,86 +26,37 @@ class GardenCustomKeybinds {
map[mcSettings.keyBindBack] = { shConfig.keyBindBack }
map[mcSettings.keyBindJump] = { shConfig.keyBindJump }
map[mcSettings.keyBindSneak] = { shConfig.keyBindSneak }
-
- Runtime.getRuntime().addShutdownHook(Thread { reset() })
- }
-
- @SubscribeEvent
- fun onGardenToolChange(event: GardenToolChangeEvent) {
- update()
}
- private fun update() {
- if (isEnabled() && GardenAPI.toolInHand != null) {
- applyCustomKeybinds()
- } else {
- reset()
- }
- }
-
- @SubscribeEvent
- fun onWorldChange(event: WorldEvent.Load) {
- reset()
- }
-
- private fun applyCustomKeybinds() {
- val alreadyBoundedKeys = mutableListOf<Int>()
- var counter = 0
- for ((mcBinding, skyHanniBinding) in map) {
- val newKeyCode = skyHanniBinding()
- if (newKeyCode == mcBinding.keyCode) continue
-
- disableAlreadyExistingKeybinds(newKeyCode, alreadyBoundedKeys)
-
- if (!cache.containsKey(mcBinding)) {
- cache[mcBinding] = mcBinding.keyCode
- mcBinding.unpressKeyIfDown()
- }
+ private fun isEnabled() = GardenAPI.inGarden() && shConfig.keyBindEnabled
- mcBinding.keyCode = newKeyCode
- alreadyBoundedKeys.add(mcBinding.keyCodeDefault)
- counter++
- }
+ private fun isActive() = isEnabled() && GardenAPI.toolInHand != null
- if (counter > 0) {
- KeyBinding.resetKeyBindingArrayAndHash()
- }
- }
- private fun disableAlreadyExistingKeybinds(newKeyCode: Int, alreadyBoundedKeys: MutableList<Int>) {
- if (newKeyCode == 0) return
- for (keyBinding in mcSettings.keyBindings) {
- if (keyBinding.keyCode != newKeyCode) continue
- if (alreadyBoundedKeys.contains(keyBinding.keyCodeDefault)) continue
- keyBinding.unpressKeyIfDown()
- cache[keyBinding] = keyBinding.keyCode
- keyBinding.keyCode = 0
+ private fun isHeld(keyCode: Int): Boolean {
+ if (keyCode == 0) return false
+ return if (keyCode < 0) {
+ Mouse.isButtonDown(keyCode + 100)
+ } else {
+ Keyboard.isKeyDown(keyCode)
}
}
- private fun reset() {
- var counter = 0
- for ((key, keyCode) in cache) {
- if (key.keyCode != keyCode) {
- key.unpressKeyIfDown()
- counter++
- key.keyCode = keyCode
- }
- }
- cache.clear()
- if (counter > 0) {
- KeyBinding.resetKeyBindingArrayAndHash()
- }
+ @JvmStatic
+ fun isKeyDown(keyBinding: KeyBinding, cir: CallbackInfoReturnable<Boolean>) {
+ if (!isActive()) return
+ val override = map[keyBinding] ?: return
+ val keyCode = override()
+ cir.returnValue = isHeld(keyCode)
}
- private fun KeyBinding.unpressKeyIfDown() {
- try {
- if (KeybindHelper.isKeyDown(keyCode)) {
- (this as AccessorKeyBinding).skyhanni_unpressKey()
- }
- } catch (_: IllegalStateException) {
- }
+ @JvmStatic
+ fun onTick(keyCode: Int, ci: CallbackInfo) {
+ if (!isActive()) return
+ if (keyCode == 0) return
+ val keyBinding = map.entries.firstOrNull { it.value() == keyCode }?.key ?: return
+ ci.cancel()
+ keyBinding as AccessorKeyBinding
+ keyBinding.pressTime_skyhanni++
}
-
- private fun isEnabled() = GardenAPI.inGarden() && shConfig.keyBindEnabled
-}
+} \ 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 f19859468..53abc49dc 100644
--- a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java
+++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java
@@ -2,11 +2,18 @@ package at.hannibal2.skyhanni.mixins.transformers;
import net.minecraft.client.settings.KeyBinding;
import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(KeyBinding.class)
public interface AccessorKeyBinding {
+ @Accessor("pressTime")
+ int getPressTime_skyhanni();
+
+ @Accessor("pressTime")
+ void setPressTime_skyhanni(int pressTime);
+
@Invoker("unpressKey")
- void skyhanni_unpressKey();
+ void skyhanni_unpressKey();
}
diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java
new file mode 100644
index 000000000..35a9bc780
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinKeyBinding.java
@@ -0,0 +1,22 @@
+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.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+@Mixin(KeyBinding.class)
+public class MixinKeyBinding {
+ @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<Boolean> cir) {
+ GardenCustomKeybinds.isKeyDown((KeyBinding) (Object) this, cir);
+ }
+}
diff --git a/src/main/resources/mixins.skyhanni.json b/src/main/resources/mixins.skyhanni.json
index 3e6a55175..41fa37979 100644
--- a/src/main/resources/mixins.skyhanni.json
+++ b/src/main/resources/mixins.skyhanni.json
@@ -21,6 +21,7 @@
"MixinEntityRenderer",
"MixinGuiIngame",
"MixinGuiPlayerTabOverlay",
+ "MixinKeyBinding",
"gui.MixinGuiNewChat",
"gui.MixinGuiScreen"
]