Skip to content

Commit

Permalink
Merge pull request #218 from JayMensink/duplicate-macro-indicator
Browse files Browse the repository at this point in the history
Duplicate Macro Bind Indicator
  • Loading branch information
Matthias1590 authored Jun 8, 2023
2 parents cffa749 + 0c77f39 commit 7c0cfa5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);

}
3 changes: 3 additions & 0 deletions src/main/java/tools/redstone/redstonetools/macros/Macro.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,4 +11,9 @@ public static void removeKeyBinding(KeyBinding keyBinding) {
((KeyBindingMixin)keyBinding).removeKeybinding(keyBinding);
}

public static boolean isKeyAlreadyBound(Key key) {
KeyBinding foundKeyBinding = ( (KeyBindingMixin)MinecraftClient.getInstance().options.attackKey ).getBindingFromKey(key);
return foundKeyBinding != null;
}

}

0 comments on commit 7c0cfa5

Please sign in to comment.