Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigate keybind conflicts #36

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import net.minecraftforge.client.event.MouseEvent;

import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent;
Expand Down Expand Up @@ -103,4 +105,25 @@ public static void onKeyPressed(KeyInputEvent event) {

return;
}

/**
* For some reason, the key bindings will be phantom pressed if you change your hotbar while they're pressed.
* If you do the following, it will act up.
* <ol>
* <li>Bind hotbar 1 to C</li>
* <li>Press C</li>
* <li>Scroll back to the manipulator</li>
* <li>Press control</li>
* <li>Magically presses C somehow</li>
* </ol>
*/
@SubscribeEvent
public static void onMouseScroll(MouseEvent event) {
if (event.dwheel == 0) return;

CUT.unpressKey();
COPY.unpressKey();
PASTE.unpressKey();
RESET.unpressKey();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public enum Mixin {
BlockDropCapturing(
new Builder("Expose mechanism to capture non-standard block drops")
.addMixinClasses("MixinBlockDropCapturing")
.setPhase(Phase.EARLY)
),
DireAutoCraftDrops(
new Builder("Change dire autocrafting table to use getDrops instead of breakBlock")
.addMixinClasses("MixinBlockExtremeAutoCrafter")
.addTargetedMod(TargetedMod.AVARITIA_ADDONS)
.setPhase(Phase.LATE)
),
KeyCancelling(
new Builder("Cancel non-mm key presses")
.addMixinClasses("MixinKeyBinding")
.setSide(Side.CLIENT)
)

;

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

private final String name;
private final List<String> mixinClasses = new ArrayList<>();
private final List<TargetedMod> targetedMods = new ArrayList<>();
private final List<TargetedMod> targetedMods = new ArrayList<>(Arrays.asList(TargetedMod.VANILLA));
private final List<TargetedMod> excludedMods = new ArrayList<>();
private Supplier<Boolean> applyIf = () -> true;
private Phase phase = null;
private Side side = Side.BOTH;
private boolean defaultMods = true;

public Builder(String name) {
this.name = name;
Expand All @@ -151,13 +155,11 @@ public Builder addMixinClasses(String... mixinClasses) {
}

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

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

public Builder addTargetedMod(TargetedMod mod) {
if (defaultMods) {
targetedMods.clear();
defaultMods = false;
}
this.targetedMods.add(mod);
return this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/mm_at.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ public net.minecraft.client.particle.EffectRenderer field_78876_b # fxLayers
public net.minecraft.tileentity.TileEntitySkull field_145910_i # rotation

public net.minecraft.block.Block func_149642_a(Lnet/minecraft/world/World;IIILnet/minecraft/item/ItemStack;)V # dropBlockAsItem

public net.minecraft.client.settings.KeyBinding func_74505_d()V # unpressKey
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.recursive_pineapple.matter_manipulator.mixin.mixins.early;

import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.recursive_pineapple.matter_manipulator.MMMod;
import com.recursive_pineapple.matter_manipulator.common.items.manipulator.ItemMatterManipulator;
import com.recursive_pineapple.matter_manipulator.common.items.manipulator.MMKeyInputs;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

/**
* Cancel mm key presses for conflicting keybinds when the player is holding a manipulator.
*/
@Mixin(KeyBinding.class)
public class MixinKeyBinding {

@ModifyReturnValue(method = "getIsKeyPressed", at = @At("RETURN"))
public boolean mm$cancelGetIsKeyPressed(boolean pressed) {
KeyBinding self = (KeyBinding) (Object) this;

if (self == MMKeyInputs.CONTROL) return pressed;
if (self == MMKeyInputs.CUT) return pressed;
if (self == MMKeyInputs.COPY) return pressed;
if (self == MMKeyInputs.PASTE) return pressed;
if (self == MMKeyInputs.RESET) return pressed;

if (MMKeyInputs.CONTROL.getKeyCode() == 0 || MMKeyInputs.CONTROL.getIsKeyPressed()) {
EntityPlayer player = MMMod.proxy.getThePlayer();

if (player != null) {
ItemStack held = player.getHeldItem();

if (held != null && held.getItem() instanceof ItemMatterManipulator) {
if (self.getKeyCode() == MMKeyInputs.CUT.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.COPY.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.PASTE.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.RESET.getKeyCode()) return false;
}
}
}

return pressed;
}

@ModifyReturnValue(method = "isPressed", at = @At("RETURN"))
public boolean mm$cancelIsPressed(boolean pressed) {
KeyBinding self = (KeyBinding) (Object) this;

if (self == MMKeyInputs.CONTROL) return pressed;
if (self == MMKeyInputs.CUT) return pressed;
if (self == MMKeyInputs.COPY) return pressed;
if (self == MMKeyInputs.PASTE) return pressed;
if (self == MMKeyInputs.RESET) return pressed;

if (MMKeyInputs.CONTROL.getKeyCode() == 0 || MMKeyInputs.CONTROL.getIsKeyPressed()) {
EntityPlayer player = MMMod.proxy.getThePlayer();

if (player != null) {
ItemStack held = player.getHeldItem();

if (held != null && held.getItem() instanceof ItemMatterManipulator) {
if (self.getKeyCode() == MMKeyInputs.CUT.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.COPY.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.PASTE.getKeyCode()) return false;
if (self.getKeyCode() == MMKeyInputs.RESET.getKeyCode()) return false;
}
}
}

return pressed;
}
}