diff --git a/fabric-key-binding-api-v1/src/client/java/net/fabricmc/fabric/mixin/client/keybinding/KeyBindingMixin.java b/fabric-key-binding-api-v1/src/client/java/net/fabricmc/fabric/mixin/client/keybinding/KeyBindingMixin.java index 706e78d245..a9b9b3f094 100644 --- a/fabric-key-binding-api-v1/src/client/java/net/fabricmc/fabric/mixin/client/keybinding/KeyBindingMixin.java +++ b/fabric-key-binding-api-v1/src/client/java/net/fabricmc/fabric/mixin/client/keybinding/KeyBindingMixin.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; +import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -72,17 +73,34 @@ public void fabric_setContext(KeyBindingContext context) { this.fabric_context = context; } + @Unique + private static boolean fabric_isActiveAndHasNoConflict(@Nullable Set conflictingKeyBinds, KeyBinding binding) { + KeyBindingMixin mixed = (KeyBindingMixin) (Object) binding; + if (!mixed.fabric_context.isActive(MinecraftClient.getInstance())) return false; + + if (conflictingKeyBinds == null) { + return true; + } else { + boolean hasNoConflict = true; + + for (KeyBinding conflictingKeyBind : mixed.fabric_conflictingKeyBinds) { + // single ampersand so it always added to the set + hasNoConflict = hasNoConflict & conflictingKeyBinds.add(conflictingKeyBind); + } + + return hasNoConflict; + } + } + @Inject(method = "onKeyPressed", at = @At("HEAD")) private static void onKeyPressed(InputUtil.Key key, CallbackInfo ci) { List list = KeyBindingRegistryImpl.KEY_TO_BINDINGS.get(key); if (list == null) return; - Set uniqueKeyBinds = list.size() <= 1 ? null : Collections.newSetFromMap(new IdentityHashMap<>()); + Set conflictingKeyBinds = list.size() <= 1 ? null : Collections.newSetFromMap(new IdentityHashMap<>()); for (KeyBinding binding : list) { - KeyBindingMixin mixed = (KeyBindingMixin) (Object) binding; - - if (mixed.fabric_context.isActive(MinecraftClient.getInstance()) && (uniqueKeyBinds == null || uniqueKeyBinds.addAll(mixed.fabric_conflictingKeyBinds))) { + if (fabric_isActiveAndHasNoConflict(conflictingKeyBinds, binding)) { ((KeyBindingMixin) (Object) binding).timesPressed++; } } @@ -93,12 +111,10 @@ private static void setKeyPressed(InputUtil.Key key, boolean pressed, CallbackIn List list = KeyBindingRegistryImpl.KEY_TO_BINDINGS.get(key); if (list == null) return; - Set uniqueKeyBinds = list.size() <= 1 ? null : Collections.newSetFromMap(new IdentityHashMap<>()); + Set conflictingKeyBinds = list.size() <= 1 ? null : Collections.newSetFromMap(new IdentityHashMap<>()); for (KeyBinding binding : list) { - KeyBindingMixin mixed = (KeyBindingMixin) (Object) binding; - - if (mixed.fabric_context.isActive(MinecraftClient.getInstance()) && (uniqueKeyBinds == null || uniqueKeyBinds.addAll(mixed.fabric_conflictingKeyBinds))) { + if (fabric_isActiveAndHasNoConflict(conflictingKeyBinds, binding)) { binding.setPressed(pressed); } }