|
1 | 1 | package folk.sisby.inventory_tabs.mixin; |
2 | 2 |
|
3 | 3 | import java.util.Map; |
4 | | -import java.util.Objects; |
5 | 4 |
|
6 | | -import com.llamalad7.mixinextras.sugar.Local; |
| 5 | +import com.google.common.collect.ArrayListMultimap; |
| 6 | +import com.google.common.collect.Multimap; |
7 | 7 | import folk.sisby.inventory_tabs.InventoryTabs; |
8 | | -import folk.sisby.inventory_tabs.duck.InventoryTabsScreen; |
9 | 8 | import org.spongepowered.asm.mixin.Final; |
10 | 9 | import org.spongepowered.asm.mixin.Mixin; |
11 | 10 | import org.spongepowered.asm.mixin.Shadow; |
|
14 | 13 | import org.spongepowered.asm.mixin.injection.Inject; |
15 | 14 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
16 | 15 |
|
17 | | -import net.minecraft.client.MinecraftClient; |
18 | 16 | import net.minecraft.client.option.KeyBinding; |
19 | 17 | import net.minecraft.client.util.InputUtil; |
20 | 18 |
|
21 | 19 | @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 { |
25 | 21 | @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 | + |
27 | 32 | @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++; |
33 | 37 | } |
| 38 | + ci.cancel(); |
34 | 39 | } |
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$); |
42 | 46 | } |
| 47 | + ci.cancel(); |
43 | 48 | } |
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); |
52 | 55 | } |
53 | | - return null; |
54 | 56 | } |
55 | 57 | } |
0 commit comments