Skip to content

Commit 89071b4

Browse files
authored
New Keybind API fixes + port wrapped keybinds (#3561)
1 parent 9018483 commit 89071b4

5 files changed

Lines changed: 66 additions & 20 deletions

File tree

src/main/java/com/gregtechceu/gtceu/common/item/armor/IJetpack.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.gregtechceu.gtceu.common.item.armor;
22

33
import com.gregtechceu.gtceu.api.item.armor.ArmorUtils;
4-
import com.gregtechceu.gtceu.utils.input.KeyBind;
4+
import com.gregtechceu.gtceu.utils.input.SyncedKeyMappings;
55

66
import net.minecraft.core.particles.ParticleOptions;
77
import net.minecraft.core.particles.ParticleTypes;
@@ -76,8 +76,8 @@ default void performFlying(@NotNull Player player, boolean flightEnabled, boolea
7676
return;
7777
}
7878

79-
boolean flyKeyDown = KeyBind.VANILLA_JUMP.isKeyDown(player);
80-
boolean descendKeyDown = KeyBind.VANILLA_SNEAK.isKeyDown(player);
79+
boolean flyKeyDown = SyncedKeyMappings.VANILLA_JUMP.isKeyDown(player);
80+
boolean descendKeyDown = SyncedKeyMappings.VANILLA_SNEAK.isKeyDown(player);
8181
double currentAccel = getVerticalAcceleration() * (deltaY < 0.3D ? 2.5D : 1.0D);
8282

8383
if (!player.onGround() && player.getSleepingPos().isEmpty() && canUseEnergy(stack, getEnergyPerUse())) {
@@ -121,10 +121,18 @@ default void performFlying(@NotNull Player player, boolean flightEnabled, boolea
121121
// Make sure they aren't using elytra movement
122122
if (!player.isFallFlying()) {
123123
Vec3 movement = new Vec3(0, 0, 0);
124-
if (KeyBind.VANILLA_FORWARD.isKeyDown(player)) movement = movement.add(0, 0, speedForward);
125-
if (KeyBind.VANILLA_BACKWARD.isKeyDown(player)) movement = movement.add(0, 0, -speedSideways * 0.8f);
126-
if (KeyBind.VANILLA_LEFT.isKeyDown(player)) movement = movement.add(speedSideways, 0, 0);
127-
if (KeyBind.VANILLA_RIGHT.isKeyDown(player)) movement = movement.add(-speedSideways, 0, 0);
124+
if (SyncedKeyMappings.VANILLA_FORWARD.isKeyDown(player)) {
125+
movement = movement.add(0, 0, speedForward);
126+
}
127+
if (SyncedKeyMappings.VANILLA_BACKWARD.isKeyDown(player)) {
128+
movement = movement.add(0, 0, -speedSideways * 0.8f);
129+
}
130+
if (SyncedKeyMappings.VANILLA_LEFT.isKeyDown(player)) {
131+
movement = movement.add(speedSideways, 0, 0);
132+
}
133+
if (SyncedKeyMappings.VANILLA_RIGHT.isKeyDown(player)) {
134+
movement = movement.add(-speedSideways, 0, 0);
135+
}
128136

129137
var dist = movement.length();
130138
if (dist >= 1.0E-7) {

src/main/java/com/gregtechceu/gtceu/common/item/armor/QuarkTechSuite.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.gregtechceu.gtceu.common.data.GTItems;
99
import com.gregtechceu.gtceu.core.IFireImmuneEntity;
1010
import com.gregtechceu.gtceu.utils.input.KeyBind;
11+
import com.gregtechceu.gtceu.utils.input.SyncedKeyMappings;
1112

1213
import net.minecraft.client.Minecraft;
1314
import net.minecraft.client.gui.GuiGraphics;
@@ -127,9 +128,9 @@ public void onArmorTick(Level world, Player player, ItemStack itemStack) {
127128
if (player.isOnFire()) player.extinguishFire();
128129
} else if (type == ArmorItem.Type.LEGGINGS) {
129130
boolean canUseEnergy = item.canUse(energyPerUse / 100);
130-
boolean sprinting = KeyBind.VANILLA_FORWARD.isKeyDown(player) && player.isSprinting();
131-
boolean jumping = KeyBind.VANILLA_JUMP.isKeyDown(player);
132-
boolean sneaking = KeyBind.VANILLA_SNEAK.isKeyDown(player);
131+
boolean sprinting = SyncedKeyMappings.VANILLA_FORWARD.isKeyDown(player) && player.isSprinting();
132+
boolean jumping = SyncedKeyMappings.VANILLA_JUMP.isKeyDown(player);
133+
boolean sneaking = SyncedKeyMappings.VANILLA_SNEAK.isKeyDown(player);
133134

134135
if (canUseEnergy && sprinting) {
135136
if (runningTimer == 0) {
@@ -158,7 +159,7 @@ public void onArmorTick(Level world, Player player, ItemStack itemStack) {
158159
data.putByte("runningTimer", runningTimer);
159160
} else if (type == ArmorItem.Type.BOOTS) {
160161
boolean canUseEnergy = item.canUse(energyPerUse / 100);
161-
boolean jumping = KeyBind.VANILLA_JUMP.isKeyDown(player);
162+
boolean jumping = SyncedKeyMappings.VANILLA_JUMP.isKeyDown(player);
162163
boolean boostedJump = data.contains("boostedJump") && data.getBoolean("boostedJump");
163164
if (boostedJumpTimer == 0 && KeyBind.BOOTS_ENABLE.isKeyDown(player)) {
164165
boostedJump = !boostedJump;

src/main/java/com/gregtechceu/gtceu/utils/input/KeyBind.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.*;
2525
import java.util.function.Supplier;
2626

27+
/**
28+
* @deprecated Use {@link SyncedKeyMappings} instead
29+
*/
2730
@Deprecated
2831
@Mod.EventBusSubscriber(modid = GTCEu.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.FORGE)
2932
public enum KeyBind {

src/main/java/com/gregtechceu/gtceu/utils/input/SyncedKeyMapping.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class SyncedKeyMapping {
3434

3535
@OnlyIn(Dist.CLIENT)
3636
private KeyMapping keyMapping;
37+
private final boolean needsRegister;
3738
@OnlyIn(Dist.CLIENT)
3839
private int keyCode;
3940
@OnlyIn(Dist.CLIENT)
@@ -49,20 +50,27 @@ private SyncedKeyMapping(Supplier<Supplier<KeyMapping>> mcKeyMapping) {
4950
if (GTCEu.isClientSide()) {
5051
this.keyMapping = mcKeyMapping.get().get();
5152
}
53+
// Does not need to be registered, will be registered by MC
54+
this.needsRegister = false;
55+
5256
KEYMAPPINGS.put(syncIndex++, this);
5357
}
5458

5559
private SyncedKeyMapping(int keyCode) {
5660
if (GTCEu.isClientSide()) {
5761
this.keyCode = keyCode;
5862
}
63+
// Does not need to be registered, is not a configurable key mapping
64+
this.needsRegister = false;
65+
5966
KEYMAPPINGS.put(syncIndex++, this);
6067
}
6168

62-
private SyncedKeyMapping(String nameKey, IKeyConflictContext ctx, int keyCode) {
69+
private SyncedKeyMapping(String nameKey, IKeyConflictContext ctx, int keyCode, String category) {
6370
if (GTCEu.isClientSide()) {
64-
this.keyMapping = (KeyMapping) createKeyMapping(nameKey, ctx, keyCode);
71+
this.keyMapping = (KeyMapping) createKeyMapping(nameKey, ctx, keyCode, category);
6572
}
73+
this.needsRegister = true;
6674
KEYMAPPINGS.put(syncIndex++, this);
6775
}
6876

@@ -87,19 +95,33 @@ public static SyncedKeyMapping create(int keyCode) {
8795

8896
/**
8997
* Create a new SyncedKeyMapping with server held and pressed syncing to server.<br>
90-
* Will automatically create a keymapping entry in the MC options page.
98+
* Will automatically create a keymapping entry in the MC options page under the GregTechCEu category.
9199
*
92100
* @param nameKey Translation key for the keymapping name.
93101
* @param ctx Conflict context for the keymapping options category.
94102
* @param keyCode The key code, from {@link InputConstants}.
95103
*/
96104
public static SyncedKeyMapping createConfigurable(String nameKey, IKeyConflictContext ctx, int keyCode) {
97-
return new SyncedKeyMapping(nameKey, ctx, keyCode);
105+
return createConfigurable(nameKey, ctx, keyCode, GTCEu.NAME);
106+
}
107+
108+
/**
109+
* Create a new SyncedKeyMapping with server held and pressed syncing to server.<br>
110+
* Will automatically create a keymapping entry in the MC options page under the specified category.
111+
*
112+
* @param nameKey Translation key for the keymapping name.
113+
* @param ctx Conflict context for the keymapping options category.
114+
* @param keyCode The key code, from {@link InputConstants}.
115+
* @param category The category in the MC options page.
116+
*/
117+
public static SyncedKeyMapping createConfigurable(String nameKey, IKeyConflictContext ctx, int keyCode,
118+
String category) {
119+
return new SyncedKeyMapping(nameKey, ctx, keyCode, category);
98120
}
99121

100122
@OnlyIn(Dist.CLIENT)
101-
private Object createKeyMapping(String nameKey, IKeyConflictContext ctx, int keyCode) {
102-
return new KeyMapping(nameKey, ctx, InputConstants.Type.KEYSYM, keyCode, GTCEu.NAME);
123+
private Object createKeyMapping(String nameKey, IKeyConflictContext ctx, int keyCode, String category) {
124+
return new KeyMapping(nameKey, ctx, InputConstants.Type.KEYSYM, keyCode, category);
103125
}
104126

105127
/**
@@ -137,11 +159,9 @@ public SyncedKeyMapping registerPlayerListener(ServerPlayer player, IKeyPressedL
137159

138160
public static void onRegisterKeyBinds(RegisterKeyMappingsEvent event) {
139161
for (SyncedKeyMapping value : KEYMAPPINGS.values()) {
140-
if (value.keyMapping != null) {
162+
if (value.keyMapping != null && value.needsRegister) {
141163
event.register(value.keyMapping);
142164
}
143-
// no need to register keycode only keybinds?
144-
// dont think they would show up in the binds menu
145165
}
146166
}
147167

src/main/java/com/gregtechceu/gtceu/utils/input/SyncedKeyMappings.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22

33
import com.gregtechceu.gtceu.GTCEu;
44

5+
import net.minecraft.client.Minecraft;
56
import net.minecraftforge.common.MinecraftForge;
67
import net.minecraftforge.fml.ModLoader;
78

89
public class SyncedKeyMappings {
910

11+
public static final SyncedKeyMapping VANILLA_JUMP = SyncedKeyMapping
12+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyJump);
13+
public static final SyncedKeyMapping VANILLA_SNEAK = SyncedKeyMapping
14+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyShift);
15+
public static final SyncedKeyMapping VANILLA_FORWARD = SyncedKeyMapping
16+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyUp);
17+
public static final SyncedKeyMapping VANILLA_BACKWARD = SyncedKeyMapping
18+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyDown);
19+
public static final SyncedKeyMapping VANILLA_LEFT = SyncedKeyMapping
20+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyLeft);
21+
public static final SyncedKeyMapping VANILLA_RIGHT = SyncedKeyMapping
22+
.createFromMC(() -> () -> Minecraft.getInstance().options.keyRight);
23+
1024
public static void init() {
1125
if (GTCEu.isClientSide()) {
1226
MinecraftForge.EVENT_BUS.register(SyncedKeyMapping.class);

0 commit comments

Comments
 (0)