diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-04 00:23:11 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-04 00:23:11 +0100 |
commit | e2c7360e9a87e07a0b8dbf0d2180e4961908582b (patch) | |
tree | 9706fba37df0a61aba83ca0970a07d09b08ffef0 /src | |
parent | 914f78e3cb19c73b357f58cf54c9534094cbef1a (diff) | |
download | skyhanni-e2c7360e9a87e07a0b8dbf0d2180e4961908582b.tar.gz skyhanni-e2c7360e9a87e07a0b8dbf0d2180e4961908582b.tar.bz2 skyhanni-e2c7360e9a87e07a0b8dbf0d2180e4961908582b.zip |
garden keybinds.
Diffstat (limited to 'src')
6 files changed, 211 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java index 42693324a..afb204dca 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java @@ -210,6 +210,7 @@ public class SkyHanniMod { loadModule(new GardenVisitorTimer()); loadModule(new GardenNextPlotPrice()); loadModule(new GardenCropMilestoneDisplay()); + loadModule(new GardenCustomKeybinds()); Commands.INSTANCE.init(); diff --git a/src/main/java/at/hannibal2/skyhanni/config/Features.java b/src/main/java/at/hannibal2/skyhanni/config/Features.java index abed49281..9eb7554cf 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/Features.java +++ b/src/main/java/at/hannibal2/skyhanni/config/Features.java @@ -12,6 +12,7 @@ import at.hannibal2.skyhanni.features.misc.HideArmor; import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager; import com.google.gson.annotations.Expose; import net.minecraft.client.Minecraft; +import org.lwjgl.input.Keyboard; public class Features extends Config { @@ -166,6 +167,32 @@ public class Features extends Config { editOverlay(activeConfigCategory, 200, 16, bingo.bingoCardPos); return; } + + if (runnableId.equals("gardenKeyBindPresetDisabled")) { + garden.keyBindAttack = Keyboard.KEY_NONE; + garden.keyBindLeft = Keyboard.KEY_NONE; + garden.keyBindRight = Keyboard.KEY_NONE; + garden.keyBindForward = Keyboard.KEY_NONE; + garden.keyBindBack = Keyboard.KEY_NONE; + garden.keyBindJump = Keyboard.KEY_NONE; + garden.keyBindSneak = Keyboard.KEY_NONE; + + Minecraft.getMinecraft().thePlayer.closeScreen(); + return; + } + + if (runnableId.equals("gardenKeyBindPresetDefault")) { + garden.keyBindAttack = -100; + garden.keyBindLeft = Keyboard.KEY_A; + garden.keyBindRight = Keyboard.KEY_D; + garden.keyBindForward = Keyboard.KEY_W; + garden.keyBindBack = Keyboard.KEY_S; + garden.keyBindJump = Keyboard.KEY_SPACE; + garden.keyBindSneak = Keyboard.KEY_LSHIFT; + + Minecraft.getMinecraft().thePlayer.closeScreen(); + return; + } } @Expose diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java b/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java index 74f48da48..0c8b824d2 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/Garden.java @@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.config.features; import at.hannibal2.skyhanni.config.core.config.Position; import at.hannibal2.skyhanni.config.core.config.annotations.*; import com.google.gson.annotations.Expose; +import org.lwjgl.input.Keyboard; public class Garden { @@ -205,4 +206,67 @@ public class Garden { @ConfigOption(name = "Plot Price", desc = "Show the price of the plot in coins when inside the Configure Plots inventory.") @ConfigEditorBoolean public boolean plotPrice = true; + + @Expose + @ConfigOption(name = "Custom Keybind", desc = "") + @ConfigEditorAccordion(id = 8) + public boolean keybind = false; + + @Expose + @ConfigOption(name = "Enabled", desc = "Use custom keybinds while having a farming tool in the hand in the garden.") + @ConfigEditorBoolean + @ConfigAccordionId(id = 8) + public boolean keyBindEnabled = false; + + @ConfigOption(name = "Disable All", desc = "Disabling all keys.") + @ConfigEditorButton(runnableId = "gardenKeyBindPresetDisabled", buttonText = "Disable") + @ConfigAccordionId(id = 8) + public int keyBindPresetDisable = 0; + + @ConfigOption(name = "Set Default", desc = "Reset all keys to default.") + @ConfigEditorButton(runnableId = "gardenKeyBindPresetDefault", buttonText = "Default") + @ConfigAccordionId(id = 8) + public int keyBindPresetDefault = 0; + + @Expose + @ConfigOption(name = "Attack", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = -100) + public int keyBindAttack = -100; + + @Expose + @ConfigOption(name = "Move Left", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_A) + public int keyBindLeft = Keyboard.KEY_A; + + @Expose + @ConfigOption(name = "Move Right", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_D) + public int keyBindRight = Keyboard.KEY_D; + + @Expose + @ConfigOption(name = "Move Forward", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_W) + public int keyBindForward = Keyboard.KEY_W; + + @Expose + @ConfigOption(name = "Move Back", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_S) + public int keyBindBack = Keyboard.KEY_S; + + @Expose + @ConfigOption(name = "Jump", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_SPACE) + public int keyBindJump = Keyboard.KEY_SPACE; + + @Expose + @ConfigOption(name = "Sneak", desc = "") + @ConfigAccordionId(id = 8) + @ConfigEditorKeybind(defaultKey = Keyboard.KEY_LSHIFT) + public int keyBindSneak = Keyboard.KEY_LSHIFT; } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCustomKeybinds.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCustomKeybinds.kt new file mode 100644 index 000000000..f80fcb0ce --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCustomKeybinds.kt @@ -0,0 +1,106 @@ +package at.hannibal2.skyhanni.features.garden + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.core.config.KeybindHelper +import at.hannibal2.skyhanni.config.features.Garden +import at.hannibal2.skyhanni.events.GardenToolChangeEvent +import at.hannibal2.skyhanni.mixins.transformers.AccessorKeyBinding +import net.minecraft.client.Minecraft +import net.minecraft.client.settings.KeyBinding +import net.minecraftforge.event.world.WorldEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class 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>() + + init { + map[mcSettings.keyBindAttack] = { shConfig.keyBindAttack } + map[mcSettings.keyBindLeft] = { shConfig.keyBindLeft } + map[mcSettings.keyBindRight] = { shConfig.keyBindRight } + map[mcSettings.keyBindForward] = { shConfig.keyBindForward } + 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) { + if (isEnabled() && GardenAPI.cropInHand != 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() + } + + mcBinding.keyCode = newKeyCode + alreadyBoundedKeys.add(mcBinding.keyCodeDefault) + counter++ + } + + if (counter > 0) { + KeyBinding.resetKeyBindingArrayAndHash() + } + } + + private fun disableAlreadyExistingKeybinds(newKeyCode: Int, alreadyBoundedKeys: MutableList<Int>) { + if (newKeyCode != 0) { + for (keyBinding in mcSettings.keyBindings) { + if (keyBinding.keyCode == newKeyCode) { + if (!alreadyBoundedKeys.contains(keyBinding.keyCodeDefault)) { + keyBinding.unpressKeyIfDown() + cache[keyBinding] = keyBinding.keyCode + keyBinding.keyCode = 0 + } + } + } + } + } + + 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() + } + } + + private fun KeyBinding.unpressKeyIfDown() { + if (KeybindHelper.isKeyDown(keyCode)) { + (this as AccessorKeyBinding).skyhanni_unpressKey() + } + } + + private fun isEnabled() = GardenAPI.inGarden() && shConfig.keyBindEnabled +} diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java new file mode 100644 index 000000000..f19859468 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/AccessorKeyBinding.java @@ -0,0 +1,12 @@ +package at.hannibal2.skyhanni.mixins.transformers; + +import net.minecraft.client.settings.KeyBinding; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(KeyBinding.class) +public interface AccessorKeyBinding { + + @Invoker("unpressKey") + void skyhanni_unpressKey(); +} diff --git a/src/main/resources/mixins.skyhanni.json b/src/main/resources/mixins.skyhanni.json index efb308c4a..683d8405c 100644 --- a/src/main/resources/mixins.skyhanni.json +++ b/src/main/resources/mixins.skyhanni.json @@ -17,6 +17,7 @@ "renderer.MixinRendererLivingEntity" ], "client": [ + "AccessorKeyBinding", "MixinGuiIngame", "gui.MixinGuiNewChat" ] |