aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden
diff options
context:
space:
mode:
authorRoman / Linnea Gräf <nea@nea.moe>2023-04-27 12:41:20 +0200
committerGitHub <noreply@github.com>2023-04-27 12:41:20 +0200
commit361ac65d7c3559884af9c400560218fed10b09f6 (patch)
tree72b51842f38d66e0d66535ea61775aa6c7e26f37 /src/main/java/at/hannibal2/skyhanni/features/garden
parent2dd1970d6d31ea1e2bbfa30b57141ea9a4720834 (diff)
downloadskyhanni-361ac65d7c3559884af9c400560218fed10b09f6.tar.gz
skyhanni-361ac65d7c3559884af9c400560218fed10b09f6.tar.bz2
skyhanni-361ac65d7c3559884af9c400560218fed10b09f6.zip
The mixinification of custom keybinds (#62)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/garden')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/farming/GardenCustomKeybinds.kt109
1 files changed, 30 insertions, 79 deletions
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