Skip to content

Commit ea45f7c

Browse files
Mitigate keybind conflicts (#36)
This will prevent conflicting keybinds from activating when holding a manipulator. MM keybinds take priority over all others. While this will fix 99% of conflicts, some mods may use a lower-level mechanism for checking keypresses, which can't be intercepted without a much more invasive change. (cherry picked from commit 2041cb2)
1 parent 3086c14 commit ea45f7c

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/MMKeyInputs.java

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import net.minecraft.entity.player.EntityPlayer;
66
import net.minecraft.item.ItemStack;
77

8+
import net.minecraftforge.client.event.MouseEvent;
9+
810
import cpw.mods.fml.client.registry.ClientRegistry;
911
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
1012
import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent;
@@ -103,4 +105,25 @@ public static void onKeyPressed(KeyInputEvent event) {
103105

104106
return;
105107
}
108+
109+
/**
110+
* For some reason, the key bindings will be phantom pressed if you change your hotbar while they're pressed.
111+
* If you do the following, it will act up.
112+
* <ol>
113+
* <li>Bind hotbar 1 to C</li>
114+
* <li>Press C</li>
115+
* <li>Scroll back to the manipulator</li>
116+
* <li>Press control</li>
117+
* <li>Magically presses C somehow</li>
118+
* </ol>
119+
*/
120+
@SubscribeEvent
121+
public static void onMouseScroll(MouseEvent event) {
122+
if (event.dwheel == 0) return;
123+
124+
CUT.unpressKey();
125+
COPY.unpressKey();
126+
PASTE.unpressKey();
127+
RESET.unpressKey();
128+
}
106129
}

src/main/java/com/recursive_pineapple/matter_manipulator/mixin/Mixin.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ public enum Mixin {
1717
BlockDropCapturing(
1818
new Builder("Expose mechanism to capture non-standard block drops")
1919
.addMixinClasses("MixinBlockDropCapturing")
20-
.setPhase(Phase.EARLY)
2120
),
2221
DireAutoCraftDrops(
2322
new Builder("Change dire autocrafting table to use getDrops instead of breakBlock")
2423
.addMixinClasses("MixinBlockExtremeAutoCrafter")
2524
.addTargetedMod(TargetedMod.AVARITIA_ADDONS)
26-
.setPhase(Phase.LATE)
2725
),
26+
KeyCancelling(
27+
new Builder("Cancel non-mm key presses")
28+
.addMixinClasses("MixinKeyBinding")
29+
.setSide(Side.CLIENT)
30+
)
2831

2932
;
3033

@@ -42,7 +45,7 @@ public enum Mixin {
4245
this.targetedMods = builder.targetedMods;
4346
this.excludedMods = builder.excludedMods;
4447
this.applyIf = builder.applyIf;
45-
this.phase = builder.phase;
48+
this.phase = builder.phase == null ? (builder.defaultMods ? Phase.EARLY : Phase.LATE) : builder.phase;
4649
this.side = builder.side;
4750
if (this.mixinClasses.isEmpty()) throw new RuntimeException("No mixin class specified for Mixin : " + this.name());
4851
if (this.targetedMods.isEmpty()) {
@@ -135,11 +138,12 @@ private static class Builder {
135138

136139
private final String name;
137140
private final List<String> mixinClasses = new ArrayList<>();
138-
private final List<TargetedMod> targetedMods = new ArrayList<>();
141+
private final List<TargetedMod> targetedMods = new ArrayList<>(Arrays.asList(TargetedMod.VANILLA));
139142
private final List<TargetedMod> excludedMods = new ArrayList<>();
140143
private Supplier<Boolean> applyIf = () -> true;
141144
private Phase phase = null;
142145
private Side side = Side.BOTH;
146+
private boolean defaultMods = true;
143147

144148
public Builder(String name) {
145149
this.name = name;
@@ -151,13 +155,11 @@ public Builder addMixinClasses(String... mixinClasses) {
151155
}
152156

153157
public Builder setPhase(Phase phase) {
154-
if (this.phase != null) throw new RuntimeException("Trying to define Phase twice for " + this.name);
155158
this.phase = phase;
156159
return this;
157160
}
158161

159162
public Builder setSide(Side side) {
160-
if (this.side != null) throw new RuntimeException("Trying to define Side twice for " + this.name);
161163
this.side = side;
162164
return this;
163165
}
@@ -168,6 +170,10 @@ public Builder setApplyIf(Supplier<Boolean> applyIf) {
168170
}
169171

170172
public Builder addTargetedMod(TargetedMod mod) {
173+
if (defaultMods) {
174+
targetedMods.clear();
175+
defaultMods = false;
176+
}
171177
this.targetedMods.add(mod);
172178
return this;
173179
}

src/main/resources/META-INF/mm_at.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ public net.minecraft.client.particle.EffectRenderer field_78876_b # fxLayers
99
public net.minecraft.tileentity.TileEntitySkull field_145910_i # rotation
1010

1111
public net.minecraft.block.Block func_149642_a(Lnet/minecraft/world/World;IIILnet/minecraft/item/ItemStack;)V # dropBlockAsItem
12+
13+
public net.minecraft.client.settings.KeyBinding func_74505_d()V # unpressKey
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.recursive_pineapple.matter_manipulator.mixin.mixins.early;
2+
3+
import net.minecraft.client.settings.KeyBinding;
4+
import net.minecraft.entity.player.EntityPlayer;
5+
import net.minecraft.item.ItemStack;
6+
7+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
8+
import com.recursive_pineapple.matter_manipulator.MMMod;
9+
import com.recursive_pineapple.matter_manipulator.common.items.manipulator.ItemMatterManipulator;
10+
import com.recursive_pineapple.matter_manipulator.common.items.manipulator.MMKeyInputs;
11+
12+
import org.spongepowered.asm.mixin.Mixin;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
15+
/**
16+
* Cancel mm key presses for conflicting keybinds when the player is holding a manipulator.
17+
*/
18+
@Mixin(KeyBinding.class)
19+
public class MixinKeyBinding {
20+
21+
@ModifyReturnValue(method = "getIsKeyPressed", at = @At("RETURN"))
22+
public boolean mm$cancelGetIsKeyPressed(boolean pressed) {
23+
KeyBinding self = (KeyBinding) (Object) this;
24+
25+
if (self == MMKeyInputs.CONTROL) return pressed;
26+
if (self == MMKeyInputs.CUT) return pressed;
27+
if (self == MMKeyInputs.COPY) return pressed;
28+
if (self == MMKeyInputs.PASTE) return pressed;
29+
if (self == MMKeyInputs.RESET) return pressed;
30+
31+
if (MMKeyInputs.CONTROL.getKeyCode() == 0 || MMKeyInputs.CONTROL.getIsKeyPressed()) {
32+
EntityPlayer player = MMMod.proxy.getThePlayer();
33+
34+
if (player != null) {
35+
ItemStack held = player.getHeldItem();
36+
37+
if (held != null && held.getItem() instanceof ItemMatterManipulator) {
38+
if (self.getKeyCode() == MMKeyInputs.CUT.getKeyCode()) return false;
39+
if (self.getKeyCode() == MMKeyInputs.COPY.getKeyCode()) return false;
40+
if (self.getKeyCode() == MMKeyInputs.PASTE.getKeyCode()) return false;
41+
if (self.getKeyCode() == MMKeyInputs.RESET.getKeyCode()) return false;
42+
}
43+
}
44+
}
45+
46+
return pressed;
47+
}
48+
49+
@ModifyReturnValue(method = "isPressed", at = @At("RETURN"))
50+
public boolean mm$cancelIsPressed(boolean pressed) {
51+
KeyBinding self = (KeyBinding) (Object) this;
52+
53+
if (self == MMKeyInputs.CONTROL) return pressed;
54+
if (self == MMKeyInputs.CUT) return pressed;
55+
if (self == MMKeyInputs.COPY) return pressed;
56+
if (self == MMKeyInputs.PASTE) return pressed;
57+
if (self == MMKeyInputs.RESET) return pressed;
58+
59+
if (MMKeyInputs.CONTROL.getKeyCode() == 0 || MMKeyInputs.CONTROL.getIsKeyPressed()) {
60+
EntityPlayer player = MMMod.proxy.getThePlayer();
61+
62+
if (player != null) {
63+
ItemStack held = player.getHeldItem();
64+
65+
if (held != null && held.getItem() instanceof ItemMatterManipulator) {
66+
if (self.getKeyCode() == MMKeyInputs.CUT.getKeyCode()) return false;
67+
if (self.getKeyCode() == MMKeyInputs.COPY.getKeyCode()) return false;
68+
if (self.getKeyCode() == MMKeyInputs.PASTE.getKeyCode()) return false;
69+
if (self.getKeyCode() == MMKeyInputs.RESET.getKeyCode()) return false;
70+
}
71+
}
72+
}
73+
74+
return pressed;
75+
}
76+
}

0 commit comments

Comments
 (0)