Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate Macro Bind Indicator #218

Merged
merged 8 commits into from
Jun 8, 2023
Merged
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() {

Bitlett marked this conversation as resolved.
Show resolved Hide resolved
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;
}

}