diff --git a/src/main/java/com/github/matt159/dws/DoubleWideSurprise.java b/src/main/java/com/github/matt159/dws/DoubleWideSurprise.java index 1b5e6bee..4c18129b 100644 --- a/src/main/java/com/github/matt159/dws/DoubleWideSurprise.java +++ b/src/main/java/com/github/matt159/dws/DoubleWideSurprise.java @@ -46,13 +46,11 @@ public class DoubleWideSurprise { @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { proxy.preInit(event); - proxy.registerHandlers(); } @Mod.EventHandler public void init(FMLInitializationEvent event) { proxy.init(event); - proxy.registerKeyBindings(); } @Mod.EventHandler diff --git a/src/main/java/com/github/matt159/dws/events/keybinds/DWSSwapKeyHandler.java b/src/main/java/com/github/matt159/dws/events/keybinds/DWSSwapKeyHandler.java deleted file mode 100644 index 35f00905..00000000 --- a/src/main/java/com/github/matt159/dws/events/keybinds/DWSSwapKeyHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.github.matt159.dws.events.keybinds; - -import com.github.matt159.dws.network.DWSInventorySwapPacket; -import com.github.matt159.dws.network.PacketHandler; -import cpw.mods.fml.client.FMLClientHandler; -import cpw.mods.fml.client.registry.ClientRegistry; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.TickEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import net.minecraft.client.settings.KeyBinding; -import net.minecraft.util.StatCollector; -import org.lwjgl.input.Keyboard; - -@SideOnly(Side.CLIENT) -public class DWSSwapKeyHandler { - public KeyBinding swapKey = new KeyBinding(StatCollector.translateToLocal("keybind.inventoryswap"), - Keyboard.KEY_H, - "key.categories.inventory"); - - public DWSSwapKeyHandler() { - ClientRegistry.registerKeyBinding(swapKey); - FMLCommonHandler.instance().bus().register(this); - } - - // Have to do it like this so that key presses inside a gui get captured - @SubscribeEvent - public void playerTick(TickEvent.PlayerTickEvent event) { - if (event.phase == TickEvent.Phase.START && - swapKey.isPressed() && - FMLClientHandler.instance().getClient().inGameHasFocus) { - PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(false)); - } - } -} diff --git a/src/main/java/com/github/matt159/dws/events/keybinds/HotbarKeyhandler.java b/src/main/java/com/github/matt159/dws/events/keybinds/HotbarKey.java similarity index 88% rename from src/main/java/com/github/matt159/dws/events/keybinds/HotbarKeyhandler.java rename to src/main/java/com/github/matt159/dws/events/keybinds/HotbarKey.java index b98c256a..8229026e 100644 --- a/src/main/java/com/github/matt159/dws/events/keybinds/HotbarKeyhandler.java +++ b/src/main/java/com/github/matt159/dws/events/keybinds/HotbarKey.java @@ -6,8 +6,8 @@ import net.minecraft.client.Minecraft; -public class HotbarKeyhandler extends KeyHandler { - public HotbarKeyhandler(String description, int keyCode, String category, int index) { +public class HotbarKey extends KeyHandler { + public HotbarKey(String description, int keyCode, String category, int index) { super(description, keyCode, category, new HotbarKeyhandlerCallback(index)); } diff --git a/src/main/java/com/github/matt159/dws/events/keybinds/InventorySwapKey.java b/src/main/java/com/github/matt159/dws/events/keybinds/InventorySwapKey.java new file mode 100644 index 00000000..9abf51e3 --- /dev/null +++ b/src/main/java/com/github/matt159/dws/events/keybinds/InventorySwapKey.java @@ -0,0 +1,37 @@ +package com.github.matt159.dws.events.keybinds; + +import com.github.matt159.dws.Tags; +import com.github.matt159.dws.network.DWSInventorySwapPacket; +import com.github.matt159.dws.network.PacketHandler; +import cpw.mods.fml.client.FMLClientHandler; +import cpw.mods.fml.client.registry.ClientRegistry; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.settings.KeyBinding; +import net.minecraft.util.StatCollector; +import org.lwjgl.input.Keyboard; + +@SideOnly(Side.CLIENT) +public class InventorySwapKey extends KeyHandler { + public InventorySwapKey() { + super(StatCollector.translateToLocal("dws.keybind.inventoryswap.name"), + Keyboard.KEY_H, + Tags.MODNAME, + new SwapKeyCallback()); + } + + private static class SwapKeyCallback implements KeyHandler.CallBack { + @Override + public void onPress() { + PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(false)); + } + + @Override + public void tick() { + + } + } +} diff --git a/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/GameSettingsMixin.java b/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/GameSettingsMixin.java index fd2da8b7..f0a9e6ad 100644 --- a/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/GameSettingsMixin.java +++ b/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/GameSettingsMixin.java @@ -1,6 +1,6 @@ package com.github.matt159.dws.mixin.mixins.client.minecraft; -import com.github.matt159.dws.events.keybinds.HotbarKeyhandler; +import com.github.matt159.dws.events.keybinds.HotbarKey; import lombok.val; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; @@ -25,10 +25,10 @@ private KeyBinding[] redirectHotbarKeybind(GameSettings instance) { for (int index = 0; index < hotbarKeybinds.length; index++) { val hotbarKeybind = hotbarKeybinds[index]; - val hotbarKeyhandler = new HotbarKeyhandler(hotbarKeybind.getKeyDescription(), - hotbarKeybind.getKeyCode(), - hotbarKeybind.getKeyCategory(), - index); + val hotbarKeyhandler = new HotbarKey(hotbarKeybind.getKeyDescription(), + hotbarKeybind.getKeyCode(), + hotbarKeybind.getKeyCategory(), + index); hotbarKeyhandlers[index] = hotbarKeyhandler; } diff --git a/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/gui/GuiContainerMixin.java b/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/gui/GuiContainerMixin.java index 207e610d..b5d79774 100644 --- a/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/gui/GuiContainerMixin.java +++ b/src/main/java/com/github/matt159/dws/mixin/mixins/client/minecraft/gui/GuiContainerMixin.java @@ -1,6 +1,7 @@ package com.github.matt159.dws.mixin.mixins.client.minecraft.gui; import com.github.matt159.dws.config.DWSConfig; +import com.github.matt159.dws.events.keybinds.InventorySwapKey; import com.github.matt159.dws.interfaces.IDWSGui; import com.github.matt159.dws.inventory.slots.SlotDWS; import com.github.matt159.dws.network.DWSInventorySwapPacket; @@ -15,6 +16,8 @@ import net.minecraft.util.IIcon; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; + +import com.github.matt159.dws.registry.Keybindings; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -47,21 +50,9 @@ private int modifyDefaultXSize(int constant) { @Inject(method = "keyTyped", at = @At(value = "TAIL"), require = 1) - private void injectDWSHotkey(char p_73869_1_, int key, CallbackInfo ci) { - KeyBinding swapKey; - try { - String description = StatCollector.translateToLocal("keybind.inventoryswap"); - - swapKey = Arrays.stream(Minecraft.getMinecraft().gameSettings.keyBindings) - .filter(keyBind -> keyBind.getKeyDescription().equals(description)) - .findFirst() - .get(); - } catch (NoSuchElementException e) { - swapKey = null; - } - - if (swapKey != null && swapKey.getKeyCode() == key) { - PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(!(this instanceof IDWSGui))); + private void injectDWSHotkey(char typedChar, int key, CallbackInfo ci) { + if (Keybindings.SwapKey.getKeyCode() == key && !(this instanceof IDWSGui)) { + PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(true)); } } diff --git a/src/main/java/com/github/matt159/dws/proxy/ClientProxy.java b/src/main/java/com/github/matt159/dws/proxy/ClientProxy.java index 9560d029..8e0e83ab 100644 --- a/src/main/java/com/github/matt159/dws/proxy/ClientProxy.java +++ b/src/main/java/com/github/matt159/dws/proxy/ClientProxy.java @@ -1,6 +1,5 @@ package com.github.matt159.dws.proxy; -import com.github.matt159.dws.events.keybinds.DWSSwapKeyHandler; import com.github.matt159.dws.inventory.slots.SlotType; import com.github.matt159.dws.registry.Keybindings; @@ -8,15 +7,8 @@ import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerAboutToStartEvent; -import cpw.mods.fml.common.event.FMLServerStartedEvent; -import cpw.mods.fml.common.event.FMLServerStartingEvent; -import cpw.mods.fml.common.event.FMLServerStoppedEvent; -import cpw.mods.fml.common.event.FMLServerStoppingEvent; public class ClientProxy extends CommonProxy { - public DWSSwapKeyHandler keyHandler; - public void preInit(FMLPreInitializationEvent event) { super.preInit(event); } @@ -31,11 +23,6 @@ public void postInit(FMLPostInitializationEvent event) { super.postInit(event); } - @Override - public void registerKeyBindings() { - keyHandler = new DWSSwapKeyHandler(); - } - @Override public ResourceLocation getSlotHintTexture(SlotType type) { return type.getSlotHintTexture(); diff --git a/src/main/java/com/github/matt159/dws/proxy/CommonProxy.java b/src/main/java/com/github/matt159/dws/proxy/CommonProxy.java index 9f6b36f6..efd15e7b 100644 --- a/src/main/java/com/github/matt159/dws/proxy/CommonProxy.java +++ b/src/main/java/com/github/matt159/dws/proxy/CommonProxy.java @@ -31,20 +31,13 @@ public void preInit(FMLPreInitializationEvent event) { } public void init(FMLInitializationEvent event) { - this.registerHandlers(); + MinecraftForge.EVENT_BUS.register(new PlayerOpenContainerEventHandler()); } public void postInit(FMLPostInitializationEvent event) { this.disableOtherInventoryButtons(); } - public void registerHandlers() { - MinecraftForge.EVENT_BUS.register(new PlayerOpenContainerEventHandler()); - } - - public void registerKeyBindings() { - } - @SuppressWarnings("unchecked") public void disableOtherInventoryButtons() { try { diff --git a/src/main/java/com/github/matt159/dws/registry/Keybindings.java b/src/main/java/com/github/matt159/dws/registry/Keybindings.java index ad289b96..26b2dbe3 100644 --- a/src/main/java/com/github/matt159/dws/registry/Keybindings.java +++ b/src/main/java/com/github/matt159/dws/registry/Keybindings.java @@ -1,5 +1,6 @@ package com.github.matt159.dws.registry; +import com.github.matt159.dws.events.keybinds.InventorySwapKey; import com.github.matt159.dws.events.keybinds.KeyHandler; import com.github.matt159.dws.events.keybinds.ModifierKey; import lombok.experimental.UtilityClass; @@ -9,8 +10,10 @@ @UtilityClass public class Keybindings { public static KeyHandler ModifierKey; + public static KeyHandler SwapKey; public static void register() { ClientRegistry.registerKeyBinding(ModifierKey = new ModifierKey()); + ClientRegistry.registerKeyBinding(SwapKey = new InventorySwapKey()); } }