Skip to content

Commit

Permalink
#3774 Clean up AMECS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 20, 2024
1 parent d8ad227 commit cecea72
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mezz.jei.fabric.input;

import com.mojang.blaze3d.platform.InputConstants;
import java.util.function.Consumer;
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
import mezz.jei.common.input.keys.JeiKeyConflictContext;
import net.minecraft.client.KeyMapping;
import net.minecraft.network.chat.Component;

import java.util.function.Consumer;

public abstract class AbstractJeiKeyMapping implements IJeiKeyMappingInternal {
protected final JeiKeyConflictContext context;

Expand All @@ -16,19 +16,6 @@ public AbstractJeiKeyMapping(JeiKeyConflictContext context) {

protected abstract KeyMapping getMapping();

protected abstract InputConstants.Key getMappedKey();

@Override
public boolean isActiveAndMatches(InputConstants.Key key) {
if (isUnbound()) {
return false;
}
if (!this.getMappedKey().equals(key)) {
return false;
}
return context.isActive();
}

@Override
public boolean isUnbound() {
return this.getMapping().isUnbound();
Expand Down
38 changes: 38 additions & 0 deletions Fabric/src/main/java/mezz/jei/fabric/input/AmecsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mezz.jei.fabric.input;

import de.siphalor.amecs.api.KeyModifier;
import de.siphalor.amecs.api.KeyModifiers;
import mezz.jei.common.input.keys.JeiKeyModifier;

import java.util.ArrayList;
import java.util.List;

public class AmecsHelper {
private AmecsHelper() {}

public static KeyModifier getJeiModifier(JeiKeyModifier modifier) {
return switch (modifier) {
case CONTROL_OR_COMMAND -> KeyModifier.CONTROL;
case SHIFT -> KeyModifier.SHIFT;
case ALT -> KeyModifier.ALT;
case NONE -> KeyModifier.NONE;
};
}

public static List<JeiKeyModifier> getJeiModifiers(KeyModifiers modifiers) {
if (modifiers.isUnset()) {
return List.of(JeiKeyModifier.NONE);
}
List<JeiKeyModifier> modifiersList = new ArrayList<>();
if (modifiers.getAlt()) {
modifiersList.add(JeiKeyModifier.ALT);
}
if (modifiers.getControl()) {
modifiersList.add(JeiKeyModifier.CONTROL_OR_COMMAND);
}
if (modifiers.getShift()) {
modifiersList.add(JeiKeyModifier.SHIFT);
}
return modifiersList;
}
}
37 changes: 15 additions & 22 deletions Fabric/src/main/java/mezz/jei/fabric/input/AmecsJeiKeyMapping.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
package mezz.jei.fabric.input;

import de.siphalor.amecs.api.AmecsKeyBinding;
import com.mojang.blaze3d.platform.InputConstants;
import de.siphalor.amecs.api.KeyBindingUtils;
import de.siphalor.amecs.api.KeyModifiers;
import com.mojang.blaze3d.platform.InputConstants;
import mezz.jei.common.input.keys.JeiKeyConflictContext;
import mezz.jei.common.input.keys.JeiKeyModifier;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.KeyMapping;

import java.util.List;

public class AmecsJeiKeyMapping extends AbstractJeiKeyMapping {
protected final AmecsKeyBinding amecsMapping;
protected final KeyMapping amecsMapping;

public AmecsJeiKeyMapping(AmecsKeyBinding amecsMapping, JeiKeyConflictContext context) {
public AmecsJeiKeyMapping(AmecsKeyBindingWithContext amecsMapping, JeiKeyConflictContext context) {
super(context);
this.amecsMapping = amecsMapping;
}

protected AmecsKeyBinding getMapping() {
@Override
protected KeyMapping getMapping() {
return this.amecsMapping;
}

protected InputConstants.Key getMappedKey() {
return KeyBindingHelper.getBoundKeyOf(this.amecsMapping);
}

@Override
public boolean isActiveAndMatches(InputConstants.Key key) {
if (isUnbound()) {
return false;
}
if (!this.getMappedKey().equals(key)) {
if (!KeyBindingHelper.getBoundKeyOf(this.amecsMapping).equals(key)) {
return false;
}
if (!context.isActive()) {
return false;
}

KeyModifiers modifier = KeyBindingUtils.getBoundModifiers(this.amecsMapping);
if (modifier.getControl() && !JeiKeyModifier.CONTROL_OR_COMMAND.isActive(context)) {
return false;
}
if (modifier.getShift() && !JeiKeyModifier.SHIFT.isActive(context)) {
return false;
}
if (modifier.getAlt() && !JeiKeyModifier.ALT.isActive(context)) {
return false;
}
if (modifier.isUnset() && !JeiKeyModifier.NONE.isActive(context)) {
return false;
KeyModifiers modifiers = KeyBindingUtils.getBoundModifiers(this.amecsMapping);
List<JeiKeyModifier> jeiKeyModifiers = AmecsHelper.getJeiModifiers(modifiers);
for (JeiKeyModifier jeiKeyModifier : jeiKeyModifiers) {
if (!jeiKeyModifier.isActive(context)) {
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
package mezz.jei.fabric.input;

import de.siphalor.amecs.api.AmecsKeyBinding;
import de.siphalor.amecs.api.KeyModifiers;
import com.mojang.blaze3d.platform.InputConstants;
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
import de.siphalor.amecs.api.KeyModifiers;
import mezz.jei.common.input.keys.IJeiKeyMappingBuilder;
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
import mezz.jei.common.input.keys.JeiKeyModifier;

public class AmecsJeiKeyMappingBuilder extends FabricJeiKeyMappingBuilder {
protected KeyModifiers modifier = new KeyModifiers();
private final KeyModifiers modifier = new KeyModifiers();

public AmecsJeiKeyMappingBuilder(String category, String description) {
super(category, description);
this.modifier = new KeyModifiers();
}

@Override
public IJeiKeyMappingBuilder setModifier(JeiKeyModifier modifier) {
this.modifier.unset();
switch (modifier) {
case CONTROL_OR_COMMAND:
this.modifier.setControl(true);
break;
case SHIFT:
this.modifier.setShift(true);
break;
case ALT:
this.modifier.setAlt(true);
break;
}
var amecsModifier = AmecsHelper.getJeiModifier(modifier);
this.modifier.set(amecsModifier, true);
return this;
}

@Override
protected IJeiKeyMappingInternal buildMouse(int mouseButton) {
AmecsKeyBinding keyMapping = new AmecsKeyBinding(description, InputConstants.Type.MOUSE, mouseButton, category, modifier);
var keyMapping = new AmecsKeyBindingWithContext(description, InputConstants.Type.MOUSE, mouseButton, category, modifier, context);
return new AmecsJeiKeyMapping(keyMapping, context);
}

@Override
public IJeiKeyMappingInternal buildKeyboardKey(int key) {
AmecsKeyBinding keyMapping = new AmecsKeyBinding(description, InputConstants.Type.KEYSYM, key, category, modifier);
var keyMapping = new AmecsKeyBindingWithContext(description, InputConstants.Type.KEYSYM, key, category, modifier, context);
return new AmecsJeiKeyMapping(keyMapping, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mezz.jei.fabric.input;

import com.mojang.blaze3d.platform.InputConstants;
import de.siphalor.amecs.api.AmecsKeyBinding;
import de.siphalor.amecs.api.KeyModifiers;
import mezz.jei.common.input.keys.JeiKeyConflictContext;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.KeyMapping;

public class AmecsKeyBindingWithContext extends AmecsKeyBinding {
private final JeiKeyConflictContext context;

public AmecsKeyBindingWithContext(String id, InputConstants.Type type, int code, String category, KeyModifiers defaultModifiers, JeiKeyConflictContext context) {
super(id, type, code, category, defaultModifiers);
this.context = context;
}

@Override
public boolean same(KeyMapping binding) {
// Special implementation which is aware of the key conflict context.
if (binding instanceof AmecsKeyBindingWithContext other) {
return KeyBindingHelper.getBoundKeyOf(this).equals(KeyBindingHelper.getBoundKeyOf(other)) &&
(context.conflicts(other.context) || other.context.conflicts(context));
} else {
// This ensures symmetry between conflicts, as regular keybinds see this one as
// being unbound and not conflicting.
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ public FabricJeiKeyMapping(FabricKeyMapping fabricMapping, JeiKeyConflictContext
this.fabricMapping = fabricMapping;
}

@Override
protected FabricKeyMapping getMapping() {
return this.fabricMapping;
}

protected InputConstants.Key getMappedKey() {
return this.fabricMapping.realKey;
@Override
public boolean isActiveAndMatches(InputConstants.Key key) {
if (isUnbound()) {
return false;
}
if (!this.fabricMapping.realKey.equals(key)) {
return false;
}
return context.isActive();
}
}

0 comments on commit cecea72

Please sign in to comment.