From b1cc52c778f326cc62bdf154b05d1e5103e17b11 Mon Sep 17 00:00:00 2001 From: Jay Mensink Date: Sun, 4 Jun 2023 16:40:58 +0200 Subject: [PATCH 1/2] Duplicate Macro Bind Indicator If you try to bind a macro to a key that is already bound, it will indicate this by making the bind key red. --- .../tools/redstone/redstonetools/macros/Macro.java | 3 +++ .../macros/gui/screen/MacroEditScreen.java | 3 +++ .../redstone/redstonetools/utils/KeyBindingUtils.java | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/main/java/tools/redstone/redstonetools/macros/Macro.java b/src/main/java/tools/redstone/redstonetools/macros/Macro.java index 20974ad9..2e30e36e 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/Macro.java +++ b/src/main/java/tools/redstone/redstonetools/macros/Macro.java @@ -1,5 +1,6 @@ package tools.redstone.redstonetools.macros; +import net.fabricmc.fabric.api.client.keybinding.KeyBindingRegistry; import tools.redstone.redstonetools.macros.actions.Action; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.minecraft.client.MinecraftClient; @@ -85,10 +86,12 @@ public void setKey(Key key) { } public void changeKeyBindingKeyCode() { + if (this.keyBinding != null) { MinecraftClient.getInstance().options.setKeyCode(keyBinding,key); KeyBinding.updateKeysByCode(); } + } public Key getKey(){ diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java index d48cb60d..f3895aac 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java +++ b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java @@ -24,6 +24,7 @@ import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import tools.redstone.redstonetools.utils.KeyBindingUtils; import java.util.List; @@ -92,6 +93,7 @@ public void init() { Key keyCode = macro.getKey(); Text text = keyCode.getLocalizedText(); if (keyCode == InputUtil.UNKNOWN_KEY) text = Text.of(""); + if ( KeyBindingUtils.isKeyAlreadyBound(keyCode) ) { text = new LiteralText(text.getString()).formatted(Formatting.RED); } keyBindButton = new ButtonWidget(this.width / 2 + 26, 55, 75, 20, text, (button) -> { detectingKeycodeKey = true; @@ -244,6 +246,7 @@ private boolean updateKeybinding(Key key) { detectingKeycodeKey = false; Text text = key.getLocalizedText(); if (key == InputUtil.UNKNOWN_KEY) text = Text.of(""); + if ( KeyBindingUtils.isKeyAlreadyBound(key) ) { text = new LiteralText(text.getString()).formatted(Formatting.RED); } keyBindButton.setMessage(text); macro.setKey(key); diff --git a/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java b/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java index 91b9e7a8..f6b87f46 100644 --- a/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java +++ b/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java @@ -1,6 +1,8 @@ package tools.redstone.redstonetools.utils; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil.Key; import tools.redstone.redstonetools.macros.KeyBindingMixin; public class KeyBindingUtils { @@ -9,4 +11,12 @@ public static void removeKeyBinding(KeyBinding keyBinding) { ((KeyBindingMixin)keyBinding).removeKeybinding(keyBinding); } + public static boolean isKeyAlreadyBound(Key key) { + KeyBinding[] keyBindings = MinecraftClient.getInstance().options.allKeys; + for (KeyBinding keyBinding : keyBindings) { + if (keyBinding.getDefaultKey() == key) { return true; } + } + return false; + } + } From 156529f37e487cc98442b4fb9766cda7d403c327 Mon Sep 17 00:00:00 2001 From: Jay Mensink Date: Sun, 4 Jun 2023 18:23:58 +0200 Subject: [PATCH 2/2] Duplicate Macro Bindings - Fixed custom binds Custom binds used to not be included in the check for whether a key was already bound. Now they are. Issue #218. --- .../redstone/redstonetools/macros/KeyBindingMixin.java | 3 +++ .../redstonetools/mixin/macros/KeyBindingMixinImpl.java | 5 +++++ .../redstone/redstonetools/utils/KeyBindingUtils.java | 7 ++----- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/tools/redstone/redstonetools/macros/KeyBindingMixin.java b/src/main/java/tools/redstone/redstonetools/macros/KeyBindingMixin.java index e471d3b2..3e0f1cda 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/KeyBindingMixin.java +++ b/src/main/java/tools/redstone/redstonetools/macros/KeyBindingMixin.java @@ -1,9 +1,12 @@ package tools.redstone.redstonetools.macros; import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil.Key; public interface KeyBindingMixin { void removeKeybinding(KeyBinding keyBinding); + KeyBinding getBindingFromKey(Key key); + } diff --git a/src/main/java/tools/redstone/redstonetools/mixin/macros/KeyBindingMixinImpl.java b/src/main/java/tools/redstone/redstonetools/mixin/macros/KeyBindingMixinImpl.java index 677cc486..8ac71b89 100644 --- a/src/main/java/tools/redstone/redstonetools/mixin/macros/KeyBindingMixinImpl.java +++ b/src/main/java/tools/redstone/redstonetools/mixin/macros/KeyBindingMixinImpl.java @@ -22,4 +22,9 @@ public void removeKeybinding(KeyBinding keyBinding) { KEY_TO_BINDINGS.entrySet().removeIf(entry -> entry.getValue().equals(this)); } + @Override + public KeyBinding getBindingFromKey(InputUtil.Key key) { + return KEY_TO_BINDINGS.get(key); + } + } diff --git a/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java b/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java index f6b87f46..56e752eb 100644 --- a/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java +++ b/src/main/java/tools/redstone/redstonetools/utils/KeyBindingUtils.java @@ -12,11 +12,8 @@ public static void removeKeyBinding(KeyBinding keyBinding) { } public static boolean isKeyAlreadyBound(Key key) { - KeyBinding[] keyBindings = MinecraftClient.getInstance().options.allKeys; - for (KeyBinding keyBinding : keyBindings) { - if (keyBinding.getDefaultKey() == key) { return true; } - } - return false; + KeyBinding foundKeyBinding = ( (KeyBindingMixin)MinecraftClient.getInstance().options.attackKey ).getBindingFromKey(key); + return foundKeyBinding != null; } }