From 01f992e58afb4137e62b6c7d5953699493289b24 Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Mon, 2 Nov 2015 22:17:51 +0100 Subject: [PATCH] Refactored all the gui inventory managing. Was a lot. Now adding new GUIs should be fun! --- README.md | 2 - .../skate702/craftingkeys/config/Config.java | 52 ++--- .../manager/ContainerManager.java | 192 +++++++++++++++--- .../craftingkeys/manager/CraftingManager.java | 140 ++----------- .../craftingkeys/manager/FurnaceManager.java | 134 +++--------- 5 files changed, 245 insertions(+), 275 deletions(-) diff --git a/README.md b/README.md index 41c9ee5..c40e8ce 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ Crafting Keys is a modification which adds a lot of new key bindings to minecraf ##Development Progress The whole project had been refactored and is now almost ready to be release. But there are missing a few things: -- Refactor Config -- Refactor managers - Implementing all vanilla guis - Better, all new GUI diff --git a/src/main/java/de/skate702/craftingkeys/config/Config.java b/src/main/java/de/skate702/craftingkeys/config/Config.java index c1498dc..6f54cbc 100644 --- a/src/main/java/de/skate702/craftingkeys/config/Config.java +++ b/src/main/java/de/skate702/craftingkeys/config/Config.java @@ -36,7 +36,7 @@ public class Config { /** * Defines all 11 Keys you can use with Crafting Keys. */ - private static Property keyTopLeft, keyTopCenter, keyTopRight, + protected static Property keyTopLeft, keyTopCenter, keyTopRight, keyCenterLeft, keyCenterCenter, keyCenterRight, keyLowerLeft, keyLowerCenter, keyLowerRight, keyStack, keyInteract, keyDrop; @@ -44,54 +44,54 @@ public class Config { /** * Defines, if NumPad is always active for crafting. */ - private static Property enableNumPad; + protected static Property enableNumPad; - public static int getKeyTopLeft() { - return keyTopLeft.getInt(retDefKey); + public static boolean isKeyTopLeftPressed() { + return Keyboard.isKeyDown(keyTopLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(71); } - public static int getKeyTopCenter() { - return keyTopCenter.getInt(retDefKey); + public static boolean isKeyTopCenterPressed() { + return Keyboard.isKeyDown(keyTopCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(72); } - public static int getKeyTopRight() { - return keyTopRight.getInt(retDefKey); + public static boolean isKeyTopRightPressed() { + return Keyboard.isKeyDown(keyTopRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(73); } - public static int getKeyCenterLeft() { - return keyCenterLeft.getInt(retDefKey); + public static boolean isKeyCenterLeftPressed() { + return Keyboard.isKeyDown(keyCenterLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(75); } - public static int getKeyCenterCenter() { - return keyCenterCenter.getInt(retDefKey); + public static boolean isKeyCenterCenterPressed() { + return Keyboard.isKeyDown(keyCenterCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(76); } - public static int getKeyCenterRight() { - return keyCenterRight.getInt(retDefKey); + public static boolean isKeyCenterRightPressed() { + return Keyboard.isKeyDown(keyCenterRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(77); } - public static int getKeyLowerLeft() { - return keyLowerLeft.getInt(retDefKey); + public static boolean isKeyLowerLeftPressed() { + return Keyboard.isKeyDown(keyLowerLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(79); } - public static int getKeyLowerCenter() { - return keyLowerCenter.getInt(retDefKey); + public static boolean isKeyLowerCenterPressed() { + return Keyboard.isKeyDown(keyLowerCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(80); } - public static int getKeyLowerRight() { - return keyLowerRight.getInt(retDefKey); + public static boolean isKeyLowerRightPressed() { + return Keyboard.isKeyDown(keyLowerRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(81); } - public static int getKeyStack() { - return keyStack.getInt(retDefKey); + public static boolean isKeyStackPressed() { + return Keyboard.isKeyDown(keyStack.getInt(retDefKey)); } - public static int getKeyInteract() { - return keyInteract.getInt(retDefKey); + public static boolean isKeyInteractPressed() { + return Keyboard.isKeyDown(keyInteract.getInt(retDefKey)); } - public static int getKeyDrop() { - return keyDrop.getInt(retDefKey); + public static boolean isKeyDropPressed() { + return Keyboard.isKeyDown(keyDrop.getInt(retDefKey)); } public static boolean isNumPadEnabled() { diff --git a/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java index 59ab1a5..d955ee0 100644 --- a/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java @@ -2,13 +2,18 @@ package de.skate702.craftingkeys.manager; import de.skate702.craftingkeys.CraftingKeys; import de.skate702.craftingkeys.config.Config; +import de.skate702.craftingkeys.util.InputUtil; import de.skate702.craftingkeys.util.Logger; import de.skate702.craftingkeys.util.Util; +import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import org.lwjgl.input.Keyboard; +/** + * Provides all needed methods to handle and manage a gui inventory. Does also provide frames for own implementations. + */ public abstract class ContainerManager { /** @@ -27,9 +32,123 @@ public abstract class ContainerManager { /** * Checks the current keyDown-Value and does the work! + * Use the implementation in ContainerManager if possible. Or define your own. */ - public abstract void acceptKey(); + public void acceptKey() { + // Get hovered slot and goal slotIndex of pressed key (if pressed) + Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiContainer) Util.client.currentScreen); + int slotIndex = specificKeyToSlotIndex(); + + // Handle accepted key + if (!InputUtil.isSameKey(slotIndex)) { + + + if (Config.isKeyDropPressed()) { // DROP + + Logger.info("acceptKey()", "Drop Key pressed."); + onDropKeyPressed(); + + } else if (Config.isKeyInteractPressed()) { // INTERACT + + Logger.info("acceptKey()", "Interaction Key pressed."); + onInteractionKeyPressed(); + + + } else if (slotIndex >= 0 && currentHoveredSlot != null && + !Util.isHoldingStack()) { // MOVE FROM SLOT + + Logger.info("acceptKey()", "Key for index " + slotIndex + " pressed."); + onSpecificKeyPressed(currentHoveredSlot.slotNumber, slotIndex); + + + } else if (Util.isHoldingStack()) { // MOVE FROM HAND + + onHolding(slotIndex); + handleNumKey(); + + } + + } + + } + + /** + * Handles what to do when the DropKey is pressed in acceptKey(). + */ + protected void onDropKeyPressed() { + + // Drop every defined dropSlot-Item + for (int i : getDropSlots()) { + moveStackToInventory(i); + } + + } + + /** + * Handles what to do when the InteractionKey is pressed in acceptKey(). + */ + protected void onInteractionKeyPressed() { + + // Handles Interaction with items held + // Stack up on hand if equal or small enough, else throw held stack away + if (Util.isHoldingStack() && getItemStack(getInteractionSlotIndex()) != null && ( + !Util.getHeldStack().isItemEqual(getItemStack(getInteractionSlotIndex())) + || Util.getHeldStack().stackSize + getItemStack(getInteractionSlotIndex()).stackSize + > getItemStack(getInteractionSlotIndex()).getMaxStackSize())) { + moveStackToInventory(-1); + } + + // Handle Interaction + if (Config.isKeyStackPressed()) { + + int oldStackSize = -1; + interact(); + + while (Util.isHoldingStack() && + oldStackSize != Util.getHeldStack().stackSize) { + + oldStackSize = Util.getHeldStack().stackSize; + interact(); + } + + } else { + interact(); + } + + } + + /** + * Handles what to do if a specific key is pressed in acceptKey(). + * + * @param currentHoveredSlot the slot number of the currently hovered Slot (mouse hover) + * @param slotIndex the slot index returned from the key input calculation + */ + protected void onSpecificKeyPressed(int currentHoveredSlot, int slotIndex) { + + if (Config.isKeyStackPressed()) { + moveAll(currentHoveredSlot, slotIndex); + moveStackToInventory(-1); + } else { + move(currentHoveredSlot, slotIndex, 1); + } + + } + + /** + * Handles what to do with held items in acceptKey(). + * + * @param slotIndex the slot index returned from the key input calculation + */ + protected void onHolding(int slotIndex) { + + onSpecificKeyPressed(-1, slotIndex); + + } + + /** + * Handles what to do with NumKey-Inputs while holding a item. + */ protected void handleNumKey() { // hotbar-slots are always the last 9 slots of the currently opened inventory @@ -75,33 +194,52 @@ public abstract class ContainerManager { * * @return The slot index in the currently managed inventory gui */ - protected abstract int specificKeyDownToSlotIndex(); + protected abstract int specificKeyToSlotIndex(); /** - * Returns, if the stack key is pressed + * Maps all specific keys to given indices. Can be used in specificKeyToSlotIndex() * - * @return True, if pressed + * @param topLeft top-left slot index + * @param topCenter top-center slot index + * @param topRight top-right slot index + * @param centerLeft center-left slot index + * @param centerCenter center-center slot index + * @param centerRight center-right slot index + * @param lowerLeft lower-left slot index + * @param lowerCenter lower-center slot index + * @param lowerRight lower-right slot index + * @return a slot index. wow! */ - protected boolean isStackKeyDown() { - return Keyboard.isKeyDown(Config.getKeyStack()); - } + protected int mapKeyToSlot(int topLeft, int topCenter, int topRight, + int centerLeft, int centerCenter, int centerRight, + int lowerLeft, int lowerCenter, int lowerRight) { - /** - * Returns, if the Interaction key is pressed - * - * @return True, if pressed - */ - protected boolean isInteractionKeyDown() { - return Keyboard.isKeyDown(Config.getKeyInteract()); - } + if (Config.isKeyTopLeftPressed()) { + return topLeft; + } else if (Config.isKeyTopCenterPressed()) { + return topCenter; + } else if (Config.isKeyTopRightPressed()) { + return topRight; + } else if (Config.isKeyCenterLeftPressed()) { + return centerLeft; + } else if (Config.isKeyCenterCenterPressed()) { + return centerCenter; + } else if (Config.isKeyCenterRightPressed()) { + return centerRight; + } else if (Config.isKeyLowerLeftPressed()) { + return lowerLeft; + } else if (Config.isKeyLowerCenterPressed()) { + return lowerCenter; + } else if (Config.isKeyLowerRightPressed()) { + return lowerRight; + } else if (Config.isKeyInteractPressed()) { + return -101; + } else if (Config.isKeyDropPressed()) { + return -102; + } else { + return -1; + } - /** - * Returns, if the Drop key is pressed - * - * @return True, if pressed - */ - protected boolean isDropKeyDown() { - return Keyboard.isKeyDown(Config.getKeyDrop()); } /** @@ -110,7 +248,7 @@ public abstract class ContainerManager { * @param srcIndex The Source Slot Index of the Container * @param destIndex The Destination Slot Index of the Container */ - public void moveAll(int srcIndex, int destIndex) { + protected void moveAll(int srcIndex, int destIndex) { ItemStack source = getItemStack(srcIndex); @@ -130,7 +268,7 @@ public abstract class ContainerManager { * @param destIndex The Destination Slot Index of the Container * @param amount The amount of items to move (can be bigger then Stack Size) */ - public void move(int srcIndex, int destIndex, int amount) { + protected void move(int srcIndex, int destIndex, int amount) { // Stacks ItemStack source = getItemStack(srcIndex); @@ -291,6 +429,12 @@ public abstract class ContainerManager { */ protected abstract int getInventoryStartIndex(); + protected abstract int getInteractionSlotIndex(); + + protected abstract int[] getDropSlots(); + + protected abstract void interact(); + /** * Executes a left mouse click on a slot. [Based on INVTW] * diff --git a/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java b/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java index d879ff3..5f3f9df 100644 --- a/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java +++ b/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java @@ -1,15 +1,11 @@ package de.skate702.craftingkeys.manager; -import de.skate702.craftingkeys.config.Config; -import de.skate702.craftingkeys.util.InputUtil; import de.skate702.craftingkeys.util.Logger; -import de.skate702.craftingkeys.util.Util; -import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.inventory.Container; -import net.minecraft.inventory.Slot; -import org.lwjgl.input.Keyboard; - +/** + * Manages a Crafting GUI Inventory. + */ public class CraftingManager extends ContainerManager { private static CraftingManager instance = null; @@ -39,117 +35,9 @@ public class CraftingManager extends ContainerManager { } @Override - public void acceptKey() { + protected int specificKeyToSlotIndex() { - Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiCrafting) Util.client.currentScreen); - - int slotIndex = specificKeyDownToSlotIndex(); - - if (!InputUtil.isSameKey(slotIndex)) { - - // Drop - if (isDropKeyDown()) { - - Logger.info("acceptKey()", "Drop Key down."); - - for (int i = 1; i < 10; i++) { - moveStackToInventory(i); - } - - // Get from output - } else if (isInteractionKeyDown()) { - - Logger.info("acceptKey()", "Interaction Key down."); - - // Handles Interaction with items held - if (Util.isHoldingStack() && ( - !Util.getHeldStack().isItemEqual(getItemStack(0)) - || Util.getHeldStack().stackSize + getItemStack(0).stackSize - >= getItemStack(0).getMaxStackSize())) { - moveStackToInventory(-1); - } - - if (isStackKeyDown()) { - - int oldStackSize = -1; - clickOnCraftingOutput(); - - while (Util.isHoldingStack() && - oldStackSize != Util.getHeldStack().stackSize) { - - oldStackSize = Util.getHeldStack().stackSize; - clickOnCraftingOutput(); - } - - } else { - clickOnCraftingOutput(); - } - - // Move from hovered slot - } else if (slotIndex > 0 && currentHoveredSlot != null && - Util.getHeldStack() == null) { - - Logger.info("acceptKey()", "Key for index " + slotIndex + " down."); - - if (isStackKeyDown()) { - moveAll(currentHoveredSlot.slotNumber, slotIndex); - moveStackToInventory(-1); - } else { - move(currentHoveredSlot.slotNumber, slotIndex, 1); - } - - // Handle NumKey-moving and held-moving - } else if (Util.isHoldingStack()) { - - if (isStackKeyDown()) { - moveAll(-1, slotIndex); - moveStackToInventory(-1); - } else { - move(-1, slotIndex, 1); - } - - handleNumKey(); - } - } - } - - @Override - protected int specificKeyDownToSlotIndex() { - - if (Keyboard.isKeyDown(Config.getKeyTopLeft()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(71)) { - return 1; - } else if (Keyboard.isKeyDown(Config.getKeyTopCenter()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(72)) { - return 2; - } else if (Keyboard.isKeyDown(Config.getKeyTopRight()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(73)) { - return 3; - } else if (Keyboard.isKeyDown(Config.getKeyCenterLeft()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(75)) { - return 4; - } else if (Keyboard.isKeyDown(Config.getKeyCenterCenter()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(76)) { - return 5; - } else if (Keyboard.isKeyDown(Config.getKeyCenterRight()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(77)) { - return 6; - } else if (Keyboard.isKeyDown(Config.getKeyLowerLeft()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(79)) { - return 7; - } else if (Keyboard.isKeyDown(Config.getKeyLowerCenter()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(80)) { - return 8; - } else if (Keyboard.isKeyDown(Config.getKeyLowerRight()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(81)) { - return 9; - } else if (Keyboard.isKeyDown(Config.getKeyInteract())) { - return -101; - } else if (Keyboard.isKeyDown(Config.getKeyDrop())) { - return -102; - } else { - return -1; - } + return mapKeyToSlot(1, 2, 3, 4, 5, 6, 7, 8, 9); } @@ -158,12 +46,26 @@ public class CraftingManager extends ContainerManager { return 10; } + @Override + protected int getInteractionSlotIndex() { + return 0; + } + + @Override + protected int[] getDropSlots() { + return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; + } + + @Override + protected void interact() { + clickOnCraftingOutput(); + } + /** - * Sends a click on the crafting output (craftingGUI or Inventory) + * Sends a click on the crafting output */ private void clickOnCraftingOutput() { - // Click on crafting output Logger.info("clickOnCraftingOutput()", "Clicked on Crafing Output."); leftClick(0); diff --git a/src/main/java/de/skate702/craftingkeys/manager/FurnaceManager.java b/src/main/java/de/skate702/craftingkeys/manager/FurnaceManager.java index ae0536e..9ad037f 100644 --- a/src/main/java/de/skate702/craftingkeys/manager/FurnaceManager.java +++ b/src/main/java/de/skate702/craftingkeys/manager/FurnaceManager.java @@ -1,14 +1,11 @@ package de.skate702.craftingkeys.manager; -import de.skate702.craftingkeys.config.Config; -import de.skate702.craftingkeys.util.InputUtil; import de.skate702.craftingkeys.util.Logger; -import de.skate702.craftingkeys.util.Util; -import net.minecraft.client.gui.inventory.GuiFurnace; import net.minecraft.inventory.Container; -import net.minecraft.inventory.Slot; -import org.lwjgl.input.Keyboard; +/** + * Manages a Furnace GUI Inventory. + */ public class FurnaceManager extends ContainerManager { private static FurnaceManager instance = null; @@ -38,105 +35,9 @@ public class FurnaceManager extends ContainerManager { } @Override - public void acceptKey() { + protected int specificKeyToSlotIndex() { - Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiFurnace) Util.client.currentScreen); - - int slotIndex = specificKeyDownToSlotIndex(); - - if (!InputUtil.isSameKey(slotIndex)) { - - // Drop - if (isDropKeyDown()) { - - Logger.info("acceptKey()", "Drop Key down."); - - for (int i = 0; i < 3; i++) { - moveStackToInventory(i); - } - - // Get from output - } else if (isInteractionKeyDown()) { - - Logger.info("acceptKey()", "Interaction Key down."); - - // Handles Interaction with items held - if (Util.isHoldingStack() && ( - !Util.getHeldStack().isItemEqual(getItemStack(0)) - || Util.getHeldStack().stackSize + getItemStack(0).stackSize - >= getItemStack(0).getMaxStackSize())) { - moveStackToInventory(-1); - } - - if (isStackKeyDown()) { - - int oldStackSize = -1; - clickOnFurnaceOutput(); - - while (Util.isHoldingStack() && - oldStackSize != Util.getHeldStack().stackSize) { - - oldStackSize = Util.getHeldStack().stackSize; - clickOnFurnaceOutput(); - } - - } else { - clickOnFurnaceOutput(); - } - - // Move from hovered slot - } else if (slotIndex >= 0 && currentHoveredSlot != null && - !Util.isHoldingStack()) { - - Logger.info("acceptKey()", "Key for index " + slotIndex + " down."); - - if (isStackKeyDown()) { - moveAll(currentHoveredSlot.slotNumber, slotIndex); - moveStackToInventory(-1); - } else { - move(currentHoveredSlot.slotNumber, slotIndex, 1); - } - - // Handle NumKey-moving and held-moving - } else if (Util.isHoldingStack()) { - - if (isStackKeyDown()) { - moveAll(-1, slotIndex); - moveStackToInventory(-1); - } else { - move(-1, slotIndex, 1); - } - - handleNumKey(); - } - } - - } - - private void clickOnFurnaceOutput() { - - // Click on furnace output - Logger.info("clickOnCraftingOutput()", "Clicked on Crafing Output."); - rightClick(2); - - } - - @Override - protected int specificKeyDownToSlotIndex() { - - if (Keyboard.isKeyDown(Config.getKeyTopCenter()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(72)) { - return 0; - } else if (Keyboard.isKeyDown(Config.getKeyCenterCenter()) || - Config.isNumPadEnabled() && Keyboard.isKeyDown(76)) { - return 1; - } else if (Keyboard.isKeyDown(Config.getKeyInteract())) { - return -101; - } else if (Keyboard.isKeyDown(Config.getKeyDrop())) { - return -102; - } else { - return -1; - } + return mapKeyToSlot(-1, 0, -1, -1, 1, -1, -1, -1, -1); } @@ -144,4 +45,29 @@ public class FurnaceManager extends ContainerManager { protected int getInventoryStartIndex() { return 3; } + + @Override + protected int getInteractionSlotIndex() { + return 2; + } + + @Override + protected int[] getDropSlots() { + return new int[]{0, 1, 2}; + } + + @Override + protected void interact() { + clickOnFurnaceOutput(); + } + + /** + * Sends a click on the furnace output + */ + private void clickOnFurnaceOutput() { + + Logger.info("clickOnFurnaceOutput()", "Clicked on Crafing Output."); + rightClick(2); + + } }