Skip to content

Commit 7a595aa

Browse files
authored
cleanup keybind code (#2745)
1 parent 7697651 commit 7a595aa

File tree

4 files changed

+77
-65
lines changed

4 files changed

+77
-65
lines changed

src/main/java/gregtech/api/util/input/KeyBind.java

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import gregtech.api.GTValues;
44
import gregtech.api.GregTechAPI;
5-
import gregtech.api.util.GTLog;
65
import gregtech.core.network.packets.PacketKeysPressed;
76

87
import net.minecraft.client.Minecraft;
@@ -11,20 +10,21 @@
1110
import net.minecraft.entity.player.EntityPlayerMP;
1211
import net.minecraftforge.client.settings.IKeyConflictContext;
1312
import net.minecraftforge.client.settings.KeyConflictContext;
14-
import net.minecraftforge.common.MinecraftForge;
1513
import net.minecraftforge.fml.client.registry.ClientRegistry;
1614
import net.minecraftforge.fml.common.FMLCommonHandler;
1715
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
1816
import net.minecraftforge.fml.common.gameevent.InputEvent;
1917
import net.minecraftforge.fml.relauncher.Side;
2018
import net.minecraftforge.fml.relauncher.SideOnly;
2119

22-
import org.apache.commons.lang3.tuple.MutablePair;
20+
import org.jetbrains.annotations.ApiStatus;
21+
import org.jetbrains.annotations.NotNull;
2322
import org.lwjgl.input.Keyboard;
2423
import org.lwjgl.input.Mouse;
2524

2625
import java.util.ArrayList;
2726
import java.util.List;
27+
import java.util.Map;
2828
import java.util.WeakHashMap;
2929
import java.util.function.Supplier;
3030

@@ -43,13 +43,6 @@ public enum KeyBind {
4343

4444
public static final KeyBind[] VALUES = values();
4545

46-
public static void init() {
47-
GTLog.logger.info("Registering KeyBinds");
48-
if (FMLCommonHandler.instance().getSide().isClient()) {
49-
MinecraftForge.EVENT_BUS.register(KeyBind.class);
50-
}
51-
}
52-
5346
@SubscribeEvent
5447
@SideOnly(Side.CLIENT)
5548
public static void onInputEvent(InputEvent.KeyInputEvent event) {
@@ -83,75 +76,89 @@ public static boolean scrollingDown() {
8376
return Mouse.getEventDWheel() < 0;
8477
}
8578

79+
private final Map<EntityPlayerMP, Boolean> keysPressed = new WeakHashMap<>();
80+
private final Map<EntityPlayerMP, Boolean> keysDown = new WeakHashMap<>();
81+
8682
@SideOnly(Side.CLIENT)
87-
private KeyBinding keybinding;
83+
private KeyBinding mcKeyBinding;
8884
@SideOnly(Side.CLIENT)
89-
private boolean isPressed, isKeyDown;
90-
91-
private final WeakHashMap<EntityPlayerMP, MutablePair<Boolean, Boolean>> mapping = new WeakHashMap<>();
92-
93-
// For Vanilla/Other Mod keybinds
94-
// Double Supplier to keep client classes from loading
95-
KeyBind(Supplier<Supplier<KeyBinding>> keybindingGetter) {
85+
private boolean isPressed;
86+
@SideOnly(Side.CLIENT)
87+
private boolean isKeyDown;
88+
89+
/**
90+
* For Vanilla/Other Mod keybinds
91+
* <p>
92+
* Double Supplier keeps client classes from loading
93+
*
94+
* @param keybindingSupplier supplier to the client side keybinding
95+
*/
96+
KeyBind(@NotNull Supplier<Supplier<KeyBinding>> keybindingSupplier) {
9697
if (FMLCommonHandler.instance().getSide().isClient()) {
97-
this.keybinding = keybindingGetter.get().get();
98+
this.mcKeyBinding = keybindingSupplier.get().get();
9899
}
99100
}
100101

101-
KeyBind(String langKey, int button) {
102+
KeyBind(@NotNull String langKey, int button) {
102103
if (FMLCommonHandler.instance().getSide().isClient()) {
103-
this.keybinding = new KeyBinding(langKey, button, GTValues.MOD_NAME);
104-
ClientRegistry.registerKeyBinding(this.keybinding);
104+
this.mcKeyBinding = new KeyBinding(langKey, button, GTValues.MOD_NAME);
105+
ClientRegistry.registerKeyBinding(this.mcKeyBinding);
105106
}
106107
}
107108

108-
KeyBind(String langKey, IKeyConflictContext ctx, int button) {
109+
KeyBind(@NotNull String langKey, @NotNull IKeyConflictContext ctx, int button) {
109110
if (FMLCommonHandler.instance().getSide().isClient()) {
110-
this.keybinding = new KeyBinding(langKey, ctx, button, GTValues.MOD_NAME);
111-
ClientRegistry.registerKeyBinding(this.keybinding);
111+
this.mcKeyBinding = new KeyBinding(langKey, ctx, button, GTValues.MOD_NAME);
112+
ClientRegistry.registerKeyBinding(this.mcKeyBinding);
112113
}
113114
}
114115

115116
@SideOnly(Side.CLIENT)
116117
public KeyBinding toMinecraft() {
117-
return this.keybinding;
118+
return this.mcKeyBinding;
118119
}
119120

120121
@SideOnly(Side.CLIENT)
121122
public boolean isPressed() {
122-
return this.keybinding.isPressed();
123+
return this.mcKeyBinding.isPressed();
123124
}
124125

125126
@SideOnly(Side.CLIENT)
126127
public boolean isKeyDown() {
127-
return this.keybinding.isKeyDown();
128+
return this.mcKeyBinding.isKeyDown();
128129
}
129130

130-
public void update(boolean pressed, boolean keyDown, EntityPlayerMP player) {
131-
MutablePair<Boolean, Boolean> pair = this.mapping.get(player);
132-
if (pair == null) {
133-
this.mapping.put(player, MutablePair.of(pressed, keyDown));
134-
} else {
135-
pair.left = pressed;
136-
pair.right = keyDown;
137-
}
131+
@ApiStatus.Internal
132+
public void updateServerState(@NotNull EntityPlayerMP player, boolean pressed, boolean keyDown) {
133+
this.keysPressed.put(player, pressed);
134+
this.keysDown.put(player, keyDown);
138135
}
139136

140-
public boolean isPressed(EntityPlayer player) {
137+
/**
138+
* Can call on either the {@code Server} or {@code Client} side.
139+
*
140+
* @param player the player to test
141+
* @return if the player pressed the key
142+
*/
143+
public boolean isPressed(@NotNull EntityPlayer player) {
141144
if (player.world.isRemote) {
142145
return isPressed();
143146
} else {
144-
MutablePair<Boolean, Boolean> pair = this.mapping.get((EntityPlayerMP) player);
145-
return pair != null && pair.left;
147+
return keysPressed.getOrDefault((EntityPlayerMP) player, false);
146148
}
147149
}
148150

149-
public boolean isKeyDown(EntityPlayer player) {
151+
/**
152+
* Can call on either the {@code Server} or {@code Client} side.
153+
*
154+
* @param player the player to test
155+
* @return if the player is holding the key down
156+
*/
157+
public boolean isKeyDown(@NotNull EntityPlayer player) {
150158
if (player.world.isRemote) {
151159
return isKeyDown();
152160
} else {
153-
MutablePair<Boolean, Boolean> pair = this.mapping.get((EntityPlayerMP) player);
154-
return pair != null && pair.right;
161+
return keysDown.getOrDefault((EntityPlayerMP) player, false);
155162
}
156163
}
157164
}

src/main/java/gregtech/client/ClientProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import gregtech.api.util.GTLog;
1313
import gregtech.api.util.IBlockOre;
1414
import gregtech.api.util.Mods;
15+
import gregtech.api.util.input.KeyBind;
1516
import gregtech.client.model.customtexture.CustomTextureModelHandler;
1617
import gregtech.client.model.customtexture.MetadataSectionCTM;
1718
import gregtech.client.renderer.handler.FacadeRenderer;
@@ -105,6 +106,8 @@ public void onPreLoad() {
105106
OpticalPipeRenderer.INSTANCE.preInit();
106107
LaserPipeRenderer.INSTANCE.preInit();
107108
MetaEntities.initRenderers();
109+
110+
MinecraftForge.EVENT_BUS.register(KeyBind.class);
108111
}
109112

110113
@Override

src/main/java/gregtech/core/CoreModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import gregtech.api.unification.material.registry.MarkerMaterialRegistry;
3030
import gregtech.api.util.CapesRegistry;
3131
import gregtech.api.util.Mods;
32-
import gregtech.api.util.input.KeyBind;
3332
import gregtech.api.util.oreglob.OreGlob;
3433
import gregtech.api.util.virtualregistry.VirtualEnderRegistry;
3534
import gregtech.api.worldgen.bedrockFluids.BedrockFluidVeinHandler;
@@ -228,7 +227,6 @@ public void preInit(FMLPreInitializationEvent event) {
228227
/* End API Block Registration */
229228

230229
proxy.onPreLoad();
231-
KeyBind.init();
232230
}
233231

234232
@Override

src/main/java/gregtech/core/network/packets/PacketKeysPressed.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,55 @@
77
import net.minecraft.network.NetHandlerPlayServer;
88
import net.minecraft.network.PacketBuffer;
99

10-
import org.apache.commons.lang3.tuple.Pair;
10+
import org.jetbrains.annotations.NotNull;
1111

1212
import java.util.List;
1313

1414
public class PacketKeysPressed implements IPacket, IServerExecutor {
1515

16-
private Object updateKeys;
16+
private List<KeyBind> clientKeysUpdated;
17+
private int[] serverKeysUpdated;
18+
private boolean[] serverKeysPressed;
19+
private boolean[] serverKeysDown;
1720

1821
@SuppressWarnings("unused")
1922
public PacketKeysPressed() {}
2023

21-
public PacketKeysPressed(List<KeyBind> updateKeys) {
22-
this.updateKeys = updateKeys;
24+
public PacketKeysPressed(@NotNull List<KeyBind> clientKeysUpdated) {
25+
this.clientKeysUpdated = clientKeysUpdated;
2326
}
2427

2528
@Override
26-
public void encode(PacketBuffer buf) {
27-
List<KeyBind> updateKeys = (List<KeyBind>) this.updateKeys;
28-
buf.writeVarInt(updateKeys.size());
29-
for (KeyBind keyBind : updateKeys) {
30-
buf.writeVarInt(keyBind.ordinal());
29+
public void encode(@NotNull PacketBuffer buf) {
30+
buf.writeVarInt(clientKeysUpdated.size());
31+
for (KeyBind keyBind : clientKeysUpdated) {
32+
buf.writeByte(keyBind.ordinal());
3133
buf.writeBoolean(keyBind.isPressed());
3234
buf.writeBoolean(keyBind.isKeyDown());
3335
}
3436
}
3537

3638
@Override
37-
public void decode(PacketBuffer buf) {
38-
this.updateKeys = new Pair[KeyBind.VALUES.length];
39-
Pair<Boolean, Boolean>[] updateKeys = (Pair<Boolean, Boolean>[]) this.updateKeys;
40-
int size = buf.readVarInt();
39+
public void decode(@NotNull PacketBuffer buf) {
40+
final int size = buf.readVarInt();
41+
this.serverKeysUpdated = new int[size];
42+
this.serverKeysPressed = new boolean[size];
43+
this.serverKeysDown = new boolean[size];
4144
for (int i = 0; i < size; i++) {
42-
updateKeys[buf.readVarInt()] = Pair.of(buf.readBoolean(), buf.readBoolean());
45+
serverKeysUpdated[i] = buf.readByte();
46+
serverKeysPressed[i] = buf.readBoolean();
47+
serverKeysDown[i] = buf.readBoolean();
4348
}
4449
}
4550

4651
@Override
4752
public void executeServer(NetHandlerPlayServer handler) {
48-
KeyBind[] keybinds = KeyBind.VALUES;
49-
Pair<Boolean, Boolean>[] updateKeys = (Pair<Boolean, Boolean>[]) this.updateKeys;
50-
for (int i = 0; i < updateKeys.length; i++) {
51-
Pair<Boolean, Boolean> pair = updateKeys[i];
52-
if (pair != null) {
53-
keybinds[i].update(pair.getLeft(), pair.getRight(), handler.player);
54-
}
53+
if (serverKeysUpdated == null) {
54+
throw new IllegalStateException("PacketKeysPressed called executeServer() before decode()");
55+
}
56+
57+
for (int i = 0; i < serverKeysUpdated.length; i++) {
58+
KeyBind.VALUES[i].updateServerState(handler.player, serverKeysPressed[i], serverKeysDown[i]);
5559
}
5660
}
5761
}

0 commit comments

Comments
 (0)