Skip to content

Commit c844479

Browse files
KosmosPrimemezz
authored andcommitted
Fix keybinds not being configurable for Fabric (#3385)
Co-authored-by: mezz
1 parent 4bb0930 commit c844479

10 files changed

+281
-65
lines changed

Fabric/build.gradle.kts

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ repositories {
1919
exclusiveMaven("https://maven.parchmentmc.org") {
2020
includeGroupByRegex("org\\.parchmentmc.*")
2121
}
22+
maven("https://maven.siphalor.de/") {
23+
// for optional AMECS integration
24+
content {
25+
includeGroup("de.siphalor")
26+
}
27+
}
2228
}
2329

2430
// gradle.properties
@@ -34,6 +40,8 @@ val modJavaVersion: String by extra
3440
val parchmentMinecraftVersion: String by extra
3541
val parchmentVersionFabric: String by extra
3642
val modrinthId: String by extra
43+
val amecsVersionFabric: String by extra
44+
val amecsMinecraftVersion: String by extra
3745

3846
// set by ORG_GRADLE_PROJECT_modrinthToken in Jenkinsfile
3947
val modrinthToken: String? by project
@@ -100,6 +108,11 @@ dependencies {
100108
name = "jsr305",
101109
version = "3.0.1"
102110
)
111+
modImplementation(
112+
group = "de.siphalor",
113+
name = "amecsapi-${amecsMinecraftVersion}",
114+
version = amecsVersionFabric
115+
)
103116
dependencyProjects.forEach {
104117
implementation(it)
105118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package mezz.jei.fabric.input;
2+
3+
import com.mojang.blaze3d.platform.InputConstants;
4+
import java.util.function.Consumer;
5+
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
6+
import mezz.jei.common.input.keys.JeiKeyConflictContext;
7+
import net.minecraft.client.KeyMapping;
8+
import net.minecraft.network.chat.Component;
9+
10+
public abstract class AbstractJeiKeyMapping implements IJeiKeyMappingInternal {
11+
protected final JeiKeyConflictContext context;
12+
13+
public AbstractJeiKeyMapping(JeiKeyConflictContext context) {
14+
this.context = context;
15+
}
16+
17+
protected abstract KeyMapping getMapping();
18+
19+
protected abstract InputConstants.Key getMappedKey();
20+
21+
@Override
22+
public boolean isActiveAndMatches(InputConstants.Key key) {
23+
if (isUnbound()) {
24+
return false;
25+
}
26+
if (!this.getMappedKey().equals(key)) {
27+
return false;
28+
}
29+
return context.isActive();
30+
}
31+
32+
@Override
33+
public boolean isUnbound() {
34+
return this.getMapping().isUnbound();
35+
}
36+
37+
@Override
38+
public Component getTranslatedKeyMessage() {
39+
return this.getMapping().getTranslatedKeyMessage();
40+
}
41+
42+
@Override
43+
public IJeiKeyMappingInternal register(Consumer<KeyMapping> registerMethod) {
44+
registerMethod.accept(this.getMapping());
45+
return this;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package mezz.jei.fabric.input;
2+
3+
import de.siphalor.amecs.api.AmecsKeyBinding;
4+
import de.siphalor.amecs.api.KeyBindingUtils;
5+
import de.siphalor.amecs.api.KeyModifiers;
6+
import com.mojang.blaze3d.platform.InputConstants;
7+
import mezz.jei.common.input.keys.JeiKeyConflictContext;
8+
import mezz.jei.common.input.keys.JeiKeyModifier;
9+
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
10+
11+
public class AmecsJeiKeyMapping extends AbstractJeiKeyMapping {
12+
protected final AmecsKeyBinding amecsMapping;
13+
14+
public AmecsJeiKeyMapping(AmecsKeyBinding amecsMapping, JeiKeyConflictContext context) {
15+
super(context);
16+
this.amecsMapping = amecsMapping;
17+
}
18+
19+
protected AmecsKeyBinding getMapping() {
20+
return this.amecsMapping;
21+
}
22+
23+
protected InputConstants.Key getMappedKey() {
24+
return KeyBindingHelper.getBoundKeyOf(this.amecsMapping);
25+
}
26+
27+
@Override
28+
public boolean isActiveAndMatches(InputConstants.Key key) {
29+
if (isUnbound()) {
30+
return false;
31+
}
32+
if (!this.getMappedKey().equals(key)) {
33+
return false;
34+
}
35+
if (!context.isActive()) {
36+
return false;
37+
}
38+
39+
KeyModifiers modifier = KeyBindingUtils.getBoundModifiers(this.amecsMapping);
40+
if (modifier.getControl() && !JeiKeyModifier.CONTROL_OR_COMMAND.isActive(context)) {
41+
return false;
42+
}
43+
if (modifier.getShift() && !JeiKeyModifier.SHIFT.isActive(context)) {
44+
return false;
45+
}
46+
if (modifier.getAlt() && !JeiKeyModifier.ALT.isActive(context)) {
47+
return false;
48+
}
49+
if (modifier.isUnset() && !JeiKeyModifier.NONE.isActive(context)) {
50+
return false;
51+
}
52+
return true;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package mezz.jei.fabric.input;
2+
3+
import de.siphalor.amecs.api.AmecsKeyBinding;
4+
import de.siphalor.amecs.api.KeyModifiers;
5+
import com.mojang.blaze3d.platform.InputConstants;
6+
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
7+
import mezz.jei.common.input.keys.IJeiKeyMappingBuilder;
8+
import mezz.jei.common.input.keys.JeiKeyModifier;
9+
10+
public class AmecsJeiKeyMappingBuilder extends FabricJeiKeyMappingBuilder {
11+
protected KeyModifiers modifier = new KeyModifiers();
12+
13+
public AmecsJeiKeyMappingBuilder(String category, String description) {
14+
super(category, description);
15+
this.modifier = new KeyModifiers();
16+
}
17+
18+
@Override
19+
public IJeiKeyMappingBuilder setModifier(JeiKeyModifier modifier) {
20+
this.modifier.unset();
21+
switch (modifier) {
22+
case CONTROL_OR_COMMAND:
23+
this.modifier.setControl(true);
24+
break;
25+
case SHIFT:
26+
this.modifier.setShift(true);
27+
break;
28+
case ALT:
29+
this.modifier.setAlt(true);
30+
break;
31+
}
32+
return this;
33+
}
34+
35+
@Override
36+
protected IJeiKeyMappingInternal buildMouse(int mouseButton) {
37+
AmecsKeyBinding keyMapping = new AmecsKeyBinding(description, InputConstants.Type.MOUSE, mouseButton, category, modifier);
38+
return new AmecsJeiKeyMapping(keyMapping, context);
39+
}
40+
41+
@Override
42+
public IJeiKeyMappingInternal buildKeyboardKey(int key) {
43+
AmecsKeyBinding keyMapping = new AmecsKeyBinding(description, InputConstants.Type.KEYSYM, key, category, modifier);
44+
return new AmecsJeiKeyMapping(keyMapping, context);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,21 @@
11
package mezz.jei.fabric.input;
22

33
import com.mojang.blaze3d.platform.InputConstants;
4-
import java.util.function.Consumer;
5-
import mezz.jei.common.input.keys.IJeiKeyMappingInternal;
64
import mezz.jei.common.input.keys.JeiKeyConflictContext;
7-
import mezz.jei.common.input.keys.JeiKeyModifier;
8-
import net.minecraft.client.KeyMapping;
9-
import net.minecraft.network.chat.Component;
105

11-
public class FabricJeiKeyMapping implements IJeiKeyMappingInternal {
12-
private final String category;
13-
private final String description;
14-
private final JeiKeyConflictContext context;
15-
private final JeiKeyModifier modifier;
16-
private final InputConstants.Type type;
17-
private final InputConstants.Key key;
6+
public class FabricJeiKeyMapping extends AbstractJeiKeyMapping {
7+
protected final FabricKeyMapping fabricMapping;
188

19-
public FabricJeiKeyMapping(
20-
String category,
21-
String description,
22-
JeiKeyConflictContext context,
23-
JeiKeyModifier modifier,
24-
InputConstants.Type type,
25-
int keyCode
26-
) {
27-
this.category = category;
28-
this.description = description;
29-
this.context = context;
30-
this.modifier = modifier;
31-
this.type = type;
32-
this.key = type.getOrCreate(keyCode);
9+
public FabricJeiKeyMapping(FabricKeyMapping fabricMapping, JeiKeyConflictContext context) {
10+
super(context);
11+
this.fabricMapping = fabricMapping;
3312
}
3413

35-
@Override
36-
public boolean isActiveAndMatches(InputConstants.Key key) {
37-
if (isUnbound()) {
38-
return false;
39-
}
40-
if (!this.key.equals(key)) {
41-
return false;
42-
}
43-
return context.isActive() && modifier.isActive(context);
14+
protected FabricKeyMapping getMapping() {
15+
return this.fabricMapping;
4416
}
4517

46-
@Override
47-
public boolean isUnbound() {
48-
return this.key.equals(InputConstants.UNKNOWN);
49-
}
50-
51-
@Override
52-
public Component getTranslatedKeyMessage() {
53-
return modifier.getCombinedName(key);
54-
}
55-
56-
@Override
57-
public IJeiKeyMappingInternal register(Consumer<KeyMapping> registerMethod) {
58-
return this;
18+
protected InputConstants.Key getMappedKey() {
19+
return this.fabricMapping.realKey;
5920
}
6021
}

Fabric/src/main/java/mezz/jei/fabric/input/FabricJeiKeyMappingBuilder.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import mezz.jei.common.input.keys.JeiKeyModifier;
99

1010
public class FabricJeiKeyMappingBuilder extends AbstractJeiKeyMappingBuilder {
11-
private final String category;
12-
private final String description;
13-
private JeiKeyConflictContext context = JeiKeyConflictContext.UNIVERSAL;
14-
private JeiKeyModifier modifier = JeiKeyModifier.NONE;
11+
protected final String category;
12+
protected final String description;
13+
protected JeiKeyConflictContext context = JeiKeyConflictContext.UNIVERSAL;
1514

1615
public FabricJeiKeyMappingBuilder(String category, String description) {
1716
this.category = category;
@@ -26,31 +25,30 @@ public IJeiKeyMappingBuilder setContext(JeiKeyConflictContext context) {
2625

2726
@Override
2827
public IJeiKeyMappingBuilder setModifier(JeiKeyModifier modifier) {
29-
this.modifier = modifier;
3028
return this;
3129
}
3230

3331
@Override
3432
protected IJeiKeyMappingInternal buildMouse(int mouseButton) {
35-
return new FabricJeiKeyMapping(
36-
category,
33+
FabricKeyMapping keyMapping = new FabricKeyMapping(
3734
description,
38-
context,
39-
modifier,
4035
InputConstants.Type.MOUSE,
41-
mouseButton
36+
mouseButton,
37+
category,
38+
context
4239
);
40+
return new FabricJeiKeyMapping(keyMapping, context);
4341
}
4442

4543
@Override
4644
public IJeiKeyMappingInternal buildKeyboardKey(int key) {
47-
return new FabricJeiKeyMapping(
48-
category,
45+
FabricKeyMapping keyMapping = new FabricKeyMapping(
4946
description,
50-
context,
51-
modifier,
5247
InputConstants.Type.KEYSYM,
53-
key
48+
key,
49+
category,
50+
context
5451
);
52+
return new FabricJeiKeyMapping(keyMapping, context);
5553
}
5654
}

Fabric/src/main/java/mezz/jei/fabric/input/FabricJeiKeyMappingCategoryBuilder.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import mezz.jei.common.input.keys.IJeiKeyMappingBuilder;
44
import mezz.jei.common.input.keys.IJeiKeyMappingCategoryBuilder;
5+
import net.fabricmc.loader.api.FabricLoader;
56

67
public class FabricJeiKeyMappingCategoryBuilder implements IJeiKeyMappingCategoryBuilder {
78
private final String category;
@@ -12,6 +13,10 @@ public FabricJeiKeyMappingCategoryBuilder(String category) {
1213

1314
@Override
1415
public IJeiKeyMappingBuilder createMapping(String description) {
15-
return new FabricJeiKeyMappingBuilder(category, description);
16+
if (FabricLoader.getInstance().isModLoaded("amecsapi")) {
17+
return new AmecsJeiKeyMappingBuilder(category, description);
18+
} else {
19+
return new FabricJeiKeyMappingBuilder(category, description);
20+
}
1621
}
1722
}

0 commit comments

Comments
 (0)