Skip to content

Commit

Permalink
Move redirectors of Map.put to KeyMappingLookup.put
Browse files Browse the repository at this point in the history
Remove duplicate keybindings registered by mods

Fixes #173
  • Loading branch information
Su5eD committed Oct 31, 2023
1 parent d159198 commit d1e7731
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=true

# Versions
versionConnector=1.0.0-beta.22
versionConnector=1.0.0-beta.23
versionAdapter=1.7.0-1.20.1-20231012.193807
versionAdapterDefinition=1.8.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnList;
Expand All @@ -53,6 +54,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -404,6 +406,35 @@ public class MixinPatchTransformer implements Transformer {
methodNode.instructions.insert(insns);
return Patch.Result.APPLY;
})
.build(),
// Move redirectors of Map.put to KeyMappingLookup.put
Patch.builder()
.targetClass("net/minecraft/client/KeyMapping")
.targetMethod("m_90854_()V")
.targetInjectionPoint("Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
.modifyInjectionPoint("Lnet/minecraftforge/client/settings/KeyMappingLookup;put(Lcom/mojang/blaze3d/platform/InputConstants$Key;Lnet/minecraft/client/KeyMapping;)V")
.targetMixinType(Patch.REDIRECT)
.modifyParams(builder -> builder
.replace(0, Type.getObjectType("net/minecraftforge/client/settings/KeyMappingLookup"))
.replace(1, Type.getObjectType("com/mojang/blaze3d/platform/InputConstants$Key"))
.replace(2, Type.getObjectType("net/minecraft/client/KeyMapping")))
.transform((classNode, methodNode, methodContext, patchContext) -> {
for (ListIterator<AbstractInsnNode> iterator = methodNode.instructions.iterator(); iterator.hasNext(); ) {
AbstractInsnNode insn = iterator.next();
if (insn.getOpcode() == Opcodes.ARETURN) {
methodNode.instructions.insertBefore(insn, new InsnNode(Opcodes.POP));
methodNode.instructions.set(insn, new InsnNode(Opcodes.RETURN));
}
else if (insn instanceof MethodInsnNode minsn && minsn.name.equals("put") && minsn.desc.equals("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")) {
minsn.desc = "(Lcom/mojang/blaze3d/platform/InputConstants$Key;Lnet/minecraft/client/KeyMapping;)V";
minsn.itf = false;
minsn.setOpcode(Opcodes.INVOKEVIRTUAL);
methodNode.instructions.insert(minsn, new InsnNode(Opcodes.ACONST_NULL));
}
}
methodNode.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getArgumentTypes(methodNode.desc));
return Patch.Result.APPLY;
})
.build()
);
private static final List<ClassTransform> CLASS_TRANSFORMS = List.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.su5ed.sinytra.connector.mod.mixin;

import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Options;
import org.apache.commons.lang3.ArrayUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.HashSet;
import java.util.Set;

@Mixin(Options.class)
public abstract class OptionsMixin {
@Shadow
public KeyMapping[] keyMappings;

@Inject(method = "load(Z)V", at = {
@At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V"),
@At(value = "RETURN", ordinal = 0)
}, remap = false)
private void onForgeReLoad(boolean limited, CallbackInfo ci) {
if (limited) {
// Remove duplicate keybindings
Set<String> seen = new HashSet<>();
IntSet toRemove = new IntOpenHashSet();
for (int i = 0; i < this.keyMappings.length; i++) {
KeyMapping mapping = this.keyMappings[i];
if (!seen.add(mapping.getName())) {
toRemove.add(i);
}
}
this.keyMappings = ArrayUtils.removeAll(this.keyMappings, toRemove.toIntArray());
}
}
}
1 change: 1 addition & 0 deletions src/mod/resources/connectormod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"HumanoidArmorLayerMixin",
"ItemBlockRenderTypesMixin",
"ItemOverridesMixin",
"OptionsMixin",
"boot.MinecraftMixin",
"fieldtypes.BlockColorsMixin",
"fieldtypes.ItemColorsMixin",
Expand Down

0 comments on commit d1e7731

Please sign in to comment.