Skip to content

Commit

Permalink
keybind related cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Sep 17, 2021
1 parent ca2f2a3 commit 5ba4f44
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import gregtech.api.util.AnnotatedMaterialHandlerLoader;
import gregtech.api.util.GTLog;
import gregtech.api.util.NBTUtil;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import gregtech.api.worldgen.config.WorldGenRegistry;
import gregtech.common.*;
import gregtech.common.blocks.MetaBlocks;
Expand Down Expand Up @@ -116,7 +116,7 @@ public void onPreInit(FMLPreInitializationEvent event) {
AnnotatedComponentHandlerLoader.discoverAndLoadAnnotatedComponentHandlers(event.getAsmData());

proxy.onPreLoad();
Keybinds.register();
KeyBinds.register();
}

@Mod.EventHandler
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/gregtech/api/items/armor/ArmorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import gregtech.api.util.ItemStackKey;
import gregtech.api.util.input.EnumKey;
import gregtech.api.util.input.Key;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import gregtech.common.ConfigHolder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
Expand Down Expand Up @@ -135,10 +135,10 @@ public static ActionResult<ItemStack> canEat(EntityPlayer player, ItemStack food
*/
public static boolean isKeyDown(EntityPlayer player, EnumKey type) {
if (SIDE.isClient()) {
return Keybinds.REGISTERY.get(type.getID()).state;
return KeyBinds.REGISTRY.get(type.getID()).state;
} else {
if (Keybinds.PLAYER_KEYS.get(player) == null) return false;
List<Key> playerKeys = Keybinds.PLAYER_KEYS.get(player);
if (KeyBinds.PLAYER_KEYS.get(player) == null) return false;
List<Key> playerKeys = KeyBinds.PLAYER_KEYS.get(player);
if (playerKeys.isEmpty()) return false;
return playerKeys.get(type.getID()).state;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/net/KeysPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import gregtech.api.util.input.EnumKey;
import gregtech.api.util.input.Key;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

Expand Down Expand Up @@ -35,7 +35,7 @@ public void toBytes(ByteBuf buf) {
@Override
public void fromBytes(ByteBuf buf) {
keys = new ArrayList<>();
for (int i = 0; i < Keybinds.REGISTERY.size(); i++) {
for (int i = 0; i < KeyBinds.REGISTRY.size(); i++) {
int id = buf.readByte();
Key current = new Key(EnumKey.getKeyByID(id));
current.state = buf.readBoolean();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/gregtech/api/net/KeysUpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import gregtech.api.util.input.Key;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
Expand All @@ -17,10 +17,10 @@ public IMessage onMessage(KeysPacket message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().player;
List<Key> keys = message.getList();
if (keys.isEmpty()) return null;
if (Keybinds.PLAYER_KEYS.containsKey(player)) {
Keybinds.PLAYER_KEYS.replace(player, keys);
if (KeyBinds.PLAYER_KEYS.containsKey(player)) {
KeyBinds.PLAYER_KEYS.replace(player, keys);
} else {
Keybinds.PLAYER_KEYS.put(player, keys);
KeyBinds.PLAYER_KEYS.put(player, keys);
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/util/input/EnumKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.annotation.Nullable;

public enum EnumKey {

MENU("ALT Menu", Keyboard.KEY_LMENU, Key.KEYS_CATEGORY),
MODE_SWITCH("Mode Switch", Keyboard.KEY_M, Key.KEYS_CATEGORY),
FORWARD(null, 0, null),
Expand Down Expand Up @@ -41,5 +42,4 @@ private void setID() {
public int getID() {
return this.ID;
}

}
6 changes: 4 additions & 2 deletions src/main/java/gregtech/api/util/input/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import net.minecraftforge.fml.relauncher.SideOnly;

public class Key {
@SideOnly(Side.CLIENT)
private KeyBinding binding;

public final EnumKey KEY;
public boolean state;
public static final String KEYS_CATEGORY = "gregtech";

@SideOnly(Side.CLIENT)
private KeyBinding binding;

public Key(EnumKey type) {
this.KEY = type;
this.state = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import java.util.*;

public class KeyBinds {

public class Keybinds {
// Just registery of all used keys
public static final List<Key> REGISTERY = new ArrayList<>();
// Registry of all used keys
public static final List<Key> REGISTRY = new ArrayList<>();
// Logical server-sided variable, where is state of any keys from players
public static final Map<EntityPlayer, List<Key>> PLAYER_KEYS = new HashMap<>();

Expand All @@ -34,7 +34,7 @@ public static void initBinds() {

@SideOnly(Side.CLIENT)
public static void registerClient() {
for (Key key : REGISTERY) {
for (Key key : REGISTRY) {
if (!ArrayUtils.contains(Minecraft.getMinecraft().gameSettings.keyBindings, key.getBind()))
ClientRegistry.registerKeyBinding(key.getBind());
}
Expand All @@ -43,9 +43,9 @@ public static void registerClient() {
public static void register() {
for (EnumKey type : EnumKey.values()) {
if (ArmorUtils.SIDE.isClient()) {
REGISTERY.add(new Key(type, bindings.get(type.getID())));
REGISTRY.add(new Key(type, bindings.get(type.getID())));
} else {
REGISTERY.add(new Key(type));
REGISTRY.add(new Key(type));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gregtech/common/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import gregtech.api.util.FluidTooltipUtil;
import gregtech.api.util.GTLog;
import gregtech.api.util.ModCompatibility;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import gregtech.common.blocks.*;
import gregtech.common.covers.facade.FacadeRenderer;
import gregtech.common.items.MetaItems;
Expand Down Expand Up @@ -103,7 +103,7 @@ public class ClientProxy extends CommonProxy {

public void onPreLoad() {
super.onPreLoad();
Keybinds.initBinds();
KeyBinds.initBinds();
MetaTileEntityRenderer.preInit();
CableRenderer.preInit();
FluidPipeRenderer.preInit();
Expand All @@ -117,7 +117,7 @@ public void onPreLoad() {

@Override
public void onLoad() {
Keybinds.registerClient();
KeyBinds.registerClient();
super.onLoad();
registerColors();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gregtech/common/EventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import gregtech.api.net.KeysPacket;
import gregtech.api.net.NetworkHandler;
import gregtech.api.util.input.Key;
import gregtech.api.util.input.Keybinds;
import gregtech.api.util.input.KeyBinds;
import gregtech.common.items.armor.PowerlessJetpack;
import gregtech.common.items.MetaItems;
import net.minecraft.entity.EntityLivingBase;
Expand Down Expand Up @@ -118,14 +118,14 @@ public static void onRender(final TickEvent.RenderTickEvent event) {
public static void onKeyInput(InputEvent.KeyInputEvent event) {
if (ArmorUtils.SIDE.isClient()) {
boolean needNewPacket = false;
for (Key key : Keybinds.REGISTERY) {
for (Key key : KeyBinds.REGISTRY) {
boolean keyState = key.getBind().isKeyDown();
if (key.state != keyState) {
key.state = keyState;
needNewPacket = true;
}
}
if (needNewPacket) NetworkHandler.INSTANCE.sendToServer(new KeysPacket(Keybinds.REGISTERY));
if (needNewPacket) NetworkHandler.INSTANCE.sendToServer(new KeysPacket(KeyBinds.REGISTRY));
}
}

Expand Down

0 comments on commit 5ba4f44

Please sign in to comment.