Skip to content

Commit

Permalink
fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Aug 24, 2024
1 parent 6014619 commit e7eafa7
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/main/java/folk/sisby/inventory_tabs/mixin/MixinKeyBinding.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package folk.sisby.inventory_tabs.mixin;

import java.util.Map;
import java.util.Objects;

import com.llamalad7.mixinextras.sugar.Local;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import folk.sisby.inventory_tabs.InventoryTabs;
import folk.sisby.inventory_tabs.duck.InventoryTabsScreen;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -14,42 +13,45 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

@Mixin(KeyBinding.class)
public abstract class MixinKeyBinding {
@Shadow @Final private static Map<InputUtil.Key, KeyBinding> KEY_TO_BINDINGS;

public class MixinKeyBinding {
@Shadow private int timesPressed;

@Shadow private InputUtil.Key boundKey;

@Shadow @Final private static Map<String, KeyBinding> KEYS_BY_ID;
@Unique private static final Multimap<InputUtil.Key, KeyBinding> KEYS_TO_BINDINGS = ArrayListMultimap.create();

@Inject(method = "<init>(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL"))
private void saveConflictedBinds(String translationKey, InputUtil.Type type, int code, String category, CallbackInfo ci) {
KEYS_TO_BINDINGS.put(boundKey, (KeyBinding) (Object) this);
}

@Inject(method = "onKeyPressed", at = @At("HEAD"), cancellable = true)
private static void onKeyPressed(InputUtil.Key key, CallbackInfo ci) {
MixinKeyBinding alternative = (MixinKeyBinding) (Object) findAlternative(key, KEY_TO_BINDINGS.get(key), InventoryTabs.NEXT_TAB);
if(alternative != null) {
alternative.timesPressed++;
ci.cancel();
private static void allowTabConflictedOnKeyPressed(InputUtil.Key key, CallbackInfo ci) {
if (!key.equals(((MixinKeyBinding) (Object) InventoryTabs.NEXT_TAB).boundKey)) return;
for (KeyBinding bind : KEYS_TO_BINDINGS.get(key)) {
((MixinKeyBinding) (Object) bind).timesPressed++;
}
ci.cancel();
}

@Inject(method = "setKeyPressed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/KeyBinding;setPressed(Z)V"), cancellable = true)
private static void keyPressed(InputUtil.Key key, boolean pressed, CallbackInfo ci, @Local KeyBinding KeyBind) {
KeyBinding alternative = findAlternative(key, KeyBind, InventoryTabs.NEXT_TAB);
if(alternative != null) {
alternative.setPressed(pressed);
ci.cancel();

@Inject(method = "setKeyPressed", at = @At("HEAD"), cancellable = true)
private static void allowTabConflictedSetKeyPressed(InputUtil.Key key, boolean pressed$, CallbackInfo ci) {
if (!key.equals(((MixinKeyBinding) (Object) InventoryTabs.NEXT_TAB).boundKey)) return;
for (KeyBinding bind : KEYS_TO_BINDINGS.get(key)) {
bind.setPressed(pressed$);
}
ci.cancel();
}

@Unique private static KeyBinding findAlternative(InputUtil.Key key, KeyBinding binding, KeyBinding alternativeTo) {
if(binding == alternativeTo && (!(MinecraftClient.getInstance().currentScreen instanceof InventoryTabsScreen its) || !its.inventoryTabs$allowTabs())) {
for(Map.Entry<InputUtil.Key, KeyBinding> entry : KEY_TO_BINDINGS.entrySet()) {
if(Objects.equals(entry.getKey(), key) && entry.getValue() != alternativeTo) {
return entry.getValue();
}
}

@Inject(method = "updateKeysByCode", at = @At("HEAD"))
private static void updateConflictedBinds(CallbackInfo ci) {
KEYS_TO_BINDINGS.clear();
for (KeyBinding bind : KEYS_BY_ID.values()) {
KEYS_TO_BINDINGS.put(((MixinKeyBinding) (Object) bind).boundKey, bind);
}
return null;
}
}

0 comments on commit e7eafa7

Please sign in to comment.