Skip to content

Commit e7eafa7

Browse files
committed
fix #53
1 parent 6014619 commit e7eafa7

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package folk.sisby.inventory_tabs.mixin;
22

33
import java.util.Map;
4-
import java.util.Objects;
54

6-
import com.llamalad7.mixinextras.sugar.Local;
5+
import com.google.common.collect.ArrayListMultimap;
6+
import com.google.common.collect.Multimap;
77
import folk.sisby.inventory_tabs.InventoryTabs;
8-
import folk.sisby.inventory_tabs.duck.InventoryTabsScreen;
98
import org.spongepowered.asm.mixin.Final;
109
import org.spongepowered.asm.mixin.Mixin;
1110
import org.spongepowered.asm.mixin.Shadow;
@@ -14,42 +13,45 @@
1413
import org.spongepowered.asm.mixin.injection.Inject;
1514
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1615

17-
import net.minecraft.client.MinecraftClient;
1816
import net.minecraft.client.option.KeyBinding;
1917
import net.minecraft.client.util.InputUtil;
2018

2119
@Mixin(KeyBinding.class)
22-
public abstract class MixinKeyBinding {
23-
@Shadow @Final private static Map<InputUtil.Key, KeyBinding> KEY_TO_BINDINGS;
24-
20+
public class MixinKeyBinding {
2521
@Shadow private int timesPressed;
26-
22+
@Shadow private InputUtil.Key boundKey;
23+
24+
@Shadow @Final private static Map<String, KeyBinding> KEYS_BY_ID;
25+
@Unique private static final Multimap<InputUtil.Key, KeyBinding> KEYS_TO_BINDINGS = ArrayListMultimap.create();
26+
27+
@Inject(method = "<init>(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL"))
28+
private void saveConflictedBinds(String translationKey, InputUtil.Type type, int code, String category, CallbackInfo ci) {
29+
KEYS_TO_BINDINGS.put(boundKey, (KeyBinding) (Object) this);
30+
}
31+
2732
@Inject(method = "onKeyPressed", at = @At("HEAD"), cancellable = true)
28-
private static void onKeyPressed(InputUtil.Key key, CallbackInfo ci) {
29-
MixinKeyBinding alternative = (MixinKeyBinding) (Object) findAlternative(key, KEY_TO_BINDINGS.get(key), InventoryTabs.NEXT_TAB);
30-
if(alternative != null) {
31-
alternative.timesPressed++;
32-
ci.cancel();
33+
private static void allowTabConflictedOnKeyPressed(InputUtil.Key key, CallbackInfo ci) {
34+
if (!key.equals(((MixinKeyBinding) (Object) InventoryTabs.NEXT_TAB).boundKey)) return;
35+
for (KeyBinding bind : KEYS_TO_BINDINGS.get(key)) {
36+
((MixinKeyBinding) (Object) bind).timesPressed++;
3337
}
38+
ci.cancel();
3439
}
35-
36-
@Inject(method = "setKeyPressed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/KeyBinding;setPressed(Z)V"), cancellable = true)
37-
private static void keyPressed(InputUtil.Key key, boolean pressed, CallbackInfo ci, @Local KeyBinding KeyBind) {
38-
KeyBinding alternative = findAlternative(key, KeyBind, InventoryTabs.NEXT_TAB);
39-
if(alternative != null) {
40-
alternative.setPressed(pressed);
41-
ci.cancel();
40+
41+
@Inject(method = "setKeyPressed", at = @At("HEAD"), cancellable = true)
42+
private static void allowTabConflictedSetKeyPressed(InputUtil.Key key, boolean pressed$, CallbackInfo ci) {
43+
if (!key.equals(((MixinKeyBinding) (Object) InventoryTabs.NEXT_TAB).boundKey)) return;
44+
for (KeyBinding bind : KEYS_TO_BINDINGS.get(key)) {
45+
bind.setPressed(pressed$);
4246
}
47+
ci.cancel();
4348
}
44-
45-
@Unique private static KeyBinding findAlternative(InputUtil.Key key, KeyBinding binding, KeyBinding alternativeTo) {
46-
if(binding == alternativeTo && (!(MinecraftClient.getInstance().currentScreen instanceof InventoryTabsScreen its) || !its.inventoryTabs$allowTabs())) {
47-
for(Map.Entry<InputUtil.Key, KeyBinding> entry : KEY_TO_BINDINGS.entrySet()) {
48-
if(Objects.equals(entry.getKey(), key) && entry.getValue() != alternativeTo) {
49-
return entry.getValue();
50-
}
51-
}
49+
50+
@Inject(method = "updateKeysByCode", at = @At("HEAD"))
51+
private static void updateConflictedBinds(CallbackInfo ci) {
52+
KEYS_TO_BINDINGS.clear();
53+
for (KeyBinding bind : KEYS_BY_ID.values()) {
54+
KEYS_TO_BINDINGS.put(((MixinKeyBinding) (Object) bind).boundKey, bind);
5255
}
53-
return null;
5456
}
5557
}

0 commit comments

Comments
 (0)