diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java index e17d7b1..ffaaa5a 100644 --- a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -13,12 +13,10 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import de.skate702.craftingkeys.config.Config; import de.skate702.craftingkeys.manager.ContainerManager; +import de.skate702.craftingkeys.manager.CraftingManager; import de.skate702.craftingkeys.proxies.CraftingKeysProxy; -import de.skate702.craftingkeys.util.Helper; -import de.skate702.craftingkeys.util.InputUtil; import de.skate702.craftingkeys.util.Util; import net.minecraft.client.gui.inventory.GuiCrafting; -import net.minecraft.inventory.Slot; /** * The Main Class of the Mod with the important onTick-Method. Some Methods are @@ -69,9 +67,6 @@ public class CraftingKeys { proxy.registerRenderers(); FMLCommonHandler.instance().bus().register(this); - // TODO: Helper! - Helper.debugPrint("load(): Loaded CraftingKeys successful"); - } /** @@ -102,74 +97,20 @@ public class CraftingKeys { Util.printWarning(); } - - // Case 1: Classic GUI Screen if (Util.isCraftingGUI(Util.client.currentScreen)) { - GuiCrafting guiCrafting = (GuiCrafting) Util.client.currentScreen; - Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition(guiCrafting); - int keyDown = Helper.craftingKeyDownToSlotNumber(); + ContainerManager con = new CraftingManager( + ((GuiCrafting) Util.client.currentScreen).inventorySlots); + con.acceptKey(); - ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); - - // Block Key Interval (avoid multiple Runs) - if (!InputUtil.isSameKey(keyDown)) { - - // Moving item to crafting table - if (keyDown > 0 && currentHoveredSlot != null) { - - // Shift = Move all - if (guiCrafting.isShiftKeyDown()) { - con.moveAll(currentHoveredSlot.slotNumber, keyDown); - } else { - con.move(currentHoveredSlot.slotNumber, keyDown, 1); - } - - } - - if (keyDown == -2) { - - // Space = Move all back - Helper.debugPrint("onTick(): Move all items back or drop them."); - - for (int i = 1; i < 10; i++) { - - con.putStackToNextEmptySlot(i, true, false); - - } - } - - // Strg = Take the output - if (guiCrafting.isCtrlKeyDown()) { - - if (guiCrafting.isShiftKeyDown()) { - - // Strg + Shift = Move all (resp. faster!) - con.clickOnCraftingOutput(true); - - } else { - - // Send mouse click on crafting output (accept also - // holding) - int ticksdown = Helper.getStrgTimesDown(true); - if (ticksdown == 2 || ticksdown % 15 == 0 || (ticksdown > 60 && ticksdown % 8 == 0)) { - con.clickOnCraftingOutput(true); - } - - } - - } else { - - // Reset Strg - Helper.getStrgTimesDown(false); - } - - } + } else if (Util.isInventoryGUI(Util.client.currentScreen)) { + // do do do + System.out.println("Inventory"); + } else if (Util.isVillagerGUI(Util.client.currentScreen)) { + // do do do + //((GuiMerchant) Util.client.currentScreen).inventorySlots + System.out.println("Villager"); } - // Case 2: Inventory (2x2 Crafting, Quick-Armor) - - // TODO: Case 2 - } } \ No newline at end of file diff --git a/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java index 3a07133..ac8fc90 100644 --- a/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/manager/ContainerManager.java @@ -1,28 +1,67 @@ package de.skate702.craftingkeys.manager; import de.skate702.craftingkeys.CraftingKeys; -import de.skate702.craftingkeys.util.Helper; +import de.skate702.craftingkeys.config.Config; import de.skate702.craftingkeys.util.Util; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import org.lwjgl.input.Keyboard; -/** - * Managing Class to move Items in Inventory Containers. - * - * @author skate702 - */ -public class ContainerManager { +public abstract class ContainerManager { /** * The Container to work with. */ - private Container container; + protected Container container; + /** + * Creates a new ContainerManager with the given container. + * + * @param container The container to work with + */ public ContainerManager(Container container) { - this.container = container; + } + /** + * Checks the current keyDown-Value and does the work! + */ + public abstract void acceptKey(); + + /** + * Converts the first specific pressed Key to the slot in a given Inventory. + * Does also accept Interaction (mapped to -101) and drop key (mapped to -102). + * + * @return The slot index in the currently managed inventory gui + */ + protected abstract int specificKeyDownToSlotIndex(); + + /** + * Returns, if the stack key is pressed + * + * @return True, if pressed + */ + protected boolean isStackKeyDown() { + return Keyboard.isKeyDown(Config.getKeyStack()); + } + + /** + * Returns, if the Interaction key is pressed + * + * @return True, if pressed + */ + protected boolean isInteractionKeyDown() { + return Keyboard.isKeyDown(Config.getKeyInteract()); + } + + /** + * Returns, if the Drop key is pressed + * + * @return True, if pressed + */ + protected boolean isDropKeyDown() { + return Keyboard.isKeyDown(Config.getKeyDrop()); } /** @@ -36,9 +75,9 @@ public class ContainerManager { ItemStack source = getItemStack(srcIndex); if (source == null) { - Helper.debugPrint("moveAll(): source == null"); + System.out.println("moveAll(): source == null"); } else { - Helper.debugPrint("moveAll(): Redirected to move()"); + System.out.println("moveAll(): Redirected to move()"); move(srcIndex, destIndex, source.stackSize); } @@ -59,8 +98,11 @@ public class ContainerManager { ItemStack destination = getItemStack(destIndex); // Same Location? - if (source == null || srcIndex == destIndex) { - Helper.debugPrint("Move(): srcIndex == destIndex OR source == null"); + if (source == null) { + System.out.println("Move(): srcIndex == destIndex OR source == null"); + return; + } else if (srcIndex == destIndex) { + System.out.println("Move(): srcIndex == destIndex OR source == null"); return; } @@ -81,12 +123,11 @@ public class ContainerManager { leftClick(srcIndex); } - Helper.debugPrint("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!"); + System.out.println("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!"); } else { - Helper.debugPrint("Move(): Not the same block type!"); + System.out.println("Move(): Not the same block type!"); } - } /** @@ -104,34 +145,13 @@ public class ContainerManager { } else { - Helper.debugPrint("getItemStack(): Invalid index"); + System.out.println("getItemStack(): Invalid index"); return null; } } - /** - * Sends a click on the crafting output (craftingGUI or Inventory) - * - * @param isCraftingGUI true, if the craftingGUI is opened - */ - public void clickOnCraftingOutput(boolean isCraftingGUI) { - - putItemAway(isCraftingGUI); - - if (isCraftingGUI) { - - // Click on crafting output - Helper.debugPrint("clickOnCraftingOutput(): Clicked on Crafing Output."); - leftClick(0); - - } else { - // TODO: Same for inventory - } - - } - /** * Takes all items from a slot and moves them to the next empty slot or * drops them. @@ -139,7 +159,7 @@ public class ContainerManager { * @param sourceIndex The index of the slot to move items from * @param isCraftingGUI true, if the craftingGUI is opened */ - public void putStackToNextEmptySlot(int sourceIndex, boolean isCraftingGUI, boolean isHeld) { + protected void putStackToNextEmptySlot(int sourceIndex, boolean isCraftingGUI, boolean isHeld) { // Check for Item-Type ItemStack stackToMove; @@ -153,7 +173,7 @@ public class ContainerManager { // Test for empty crafting table slot if (!isHeld && getItemStack(sourceIndex) == null) { - Helper.debugPrint("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + "."); + System.out.println("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + "."); return; } @@ -176,7 +196,7 @@ public class ContainerManager { } - private void putItemAway(boolean isCraftingGUI) { + protected void putItemAway(boolean isCraftingGUI) { // Put current Item away if (Util.client.thePlayer.inventory.getItemStack() != null) { putStackToNextEmptySlot(-1, isCraftingGUI, true); @@ -189,7 +209,7 @@ public class ContainerManager { * @param isCraftingGUI true, if the craftingGUI is opened * @return a slot index */ - private int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) { + protected int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) { if (isCraftingGUI) { @@ -210,7 +230,7 @@ public class ContainerManager { return i; } } - Helper.debugPrint("getFirstProperSlotIndex(): No Propper / Empty Slot found!"); + System.out.println("getFirstProperSlotIndex(): No Propper / Empty Slot found!"); return -1; } else { // TODO: Same for inventory @@ -223,7 +243,7 @@ public class ContainerManager { * * @param index The index of the slot in the container */ - private void leftClick(int index) { + protected void leftClick(int index) { slotClick(index, false); } @@ -232,7 +252,7 @@ public class ContainerManager { * * @param index The index of the slot in the container */ - private void rightClick(int index) { + protected void rightClick(int index) { slotClick(index, true); } @@ -242,15 +262,15 @@ public class ContainerManager { * @param index The index of the slot in the container * @param rightClick True, if the click is with the right mouse button */ - private void slotClick(int index, boolean rightClick) { + protected void slotClick(int index, boolean rightClick) { - Helper.debugPrint("slotClick(): Clicked @ Slot " + index + " with data " + rightClick + "."); + System.out.println("slotClick(): Clicked @ Slot " + index + " with data " + rightClick + "."); int rightClickData = (rightClick) ? 1 : 0; - CraftingKeys.instance.proxy.sendSlotClick(Util.client.playerController, container.windowId, index, + CraftingKeys.proxy.sendSlotClick(Util.client.playerController, container.windowId, index, rightClickData, 0, Util.client.thePlayer); } -} \ No newline at end of file +} diff --git a/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java b/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java new file mode 100644 index 0000000..9939c6d --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/manager/CraftingManager.java @@ -0,0 +1,105 @@ +package de.skate702.craftingkeys.manager; + +import de.skate702.craftingkeys.config.Config; +import de.skate702.craftingkeys.util.InputUtil; +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; + + +public class CraftingManager extends ContainerManager { + + /** + * Creates a new Crafting Manager with the given container. + * + * @param container The container from a crafting GUI + */ + public CraftingManager(Container container) { + super(container); + } + + @Override + public void acceptKey() { + + Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiCrafting) Util.client.currentScreen); + + int slotIndex = specificKeyDownToSlotIndex(); + + if (!InputUtil.isSameKey(slotIndex)) { + + if (isDropKeyDown()) { + + for (int i = 1; i < 10; i++) { + putStackToNextEmptySlot(i, true, false); + } + + } else if (isInteractionKeyDown()) { + + if (isStackKeyDown()) { + // TODO: Get for all + System.out.println("Blabla"); + } else { + clickOnCraftingOutput(); // TODO: Change that shit! + } + + } else if (slotIndex > 0 && currentHoveredSlot != null) { + + if (isStackKeyDown()) { + moveAll(currentHoveredSlot.slotNumber, slotIndex); + } else { + move(currentHoveredSlot.slotNumber, slotIndex, 1); + } + + } + + } + + } + + @Override + protected int specificKeyDownToSlotIndex() { + + if (Keyboard.isKeyDown(Config.getKeyTopLeft())) { + return 1; + } else if (Keyboard.isKeyDown(Config.getkeyTopCenter())) { + return 2; + } else if (Keyboard.isKeyDown(Config.getKeyTopRight())) { + return 3; + } else if (Keyboard.isKeyDown(Config.getKeyCenterLeft())) { + return 4; + } else if (Keyboard.isKeyDown(Config.getKeyCenterCenter())) { + return 5; + } else if (Keyboard.isKeyDown(Config.getKeyCenterRight())) { + return 6; + } else if (Keyboard.isKeyDown(Config.getKeyLowerLeft())) { + return 7; + } else if (Keyboard.isKeyDown(Config.getKeyLowerCenter())) { + return 8; + } else if (Keyboard.isKeyDown(Config.getKeyLowerRight())) { + return 9; + } else if (Keyboard.isKeyDown(Config.getKeyInteract())) { + return -101; + } else if (Keyboard.isKeyDown(Config.getKeyDrop())) { + return -102; + } else { + return -1; + } + + } + + /** + * Sends a click on the crafting output (craftingGUI or Inventory) + */ + public void clickOnCraftingOutput() { + + //putItemAway(); // TODO! + + // Click on crafting output + System.out.println("clickOnCraftingOutput(): Clicked on Crafing Output."); + leftClick(0); + + + } +} diff --git a/src/main/java/de/skate702/craftingkeys/util/Helper.java b/src/main/java/de/skate702/craftingkeys/util/Helper.java deleted file mode 100644 index 6db44a2..0000000 --- a/src/main/java/de/skate702/craftingkeys/util/Helper.java +++ /dev/null @@ -1,92 +0,0 @@ -package de.skate702.craftingkeys.util; - -import org.lwjgl.input.Keyboard; - -/** - * This helper class provides some static helping Methods and Constants. - * - * @author sebastian - */ -@Deprecated -public class Helper { - - /** - * If true, there are Debug-Output-Prints from debugPrint(msg). - */ - public static final boolean DEBUG = true; - - - /** - * Saves the times strg was pressed before reseting - */ - private static int strgTimesDown = 0; - - /** - * Standart Output for Debug-Messages - Depends on DEBUG-Constant. - * - * @param message The Debug-Message. Syntax: "methodname(): Message" - */ - @Deprecated - public static void debugPrint(String message) { - - if (DEBUG) { - System.out.println("CK-DEBUG: " + message); - } - - } - - /** - * Reads the current Keyboard-Input and converts it to a Inventory-Slot. - * - * @return A Inventory-Slot (based on CraftingGUI), -1 for wrong input, -2 - * for space - */ - @Deprecated - public static int craftingKeyDownToSlotNumber() { - - // TODO: Make this dynamic! // Use Settings - // TODO: This has to be the job of the manager - - int returnValue = -1; - - if (Keyboard.isKeyDown(Keyboard.KEY_Q)) { - returnValue = 1; - } else if (Keyboard.isKeyDown(Keyboard.KEY_W)) { - returnValue = 2; - } else if (Keyboard.isKeyDown(Keyboard.KEY_E)) { - returnValue = 3; - } else if (Keyboard.isKeyDown(Keyboard.KEY_A)) { - returnValue = 4; - } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) { - returnValue = 5; - } else if (Keyboard.isKeyDown(Keyboard.KEY_D)) { - returnValue = 6; - } else if (Keyboard.isKeyDown(Keyboard.KEY_Y)) { - returnValue = 7; - } else if (Keyboard.isKeyDown(Keyboard.KEY_X)) { - returnValue = 8; - } else if (Keyboard.isKeyDown(Keyboard.KEY_C)) { - returnValue = 9; - } else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { - returnValue = -2; - } - - return returnValue; - } - - /** - * Return the times Strg was pressed before reseting - * - * @param strgDown false, if reset - * @return A number of tick strg was down before - */ - @Deprecated - public static int getStrgTimesDown(boolean strgDown) { - - if (!strgDown) - strgTimesDown = 0; - - return ++strgTimesDown; - - } -} \ No newline at end of file diff --git a/src/main/java/de/skate702/craftingkeys/util/InputUtil.java b/src/main/java/de/skate702/craftingkeys/util/InputUtil.java index cf4497e..bac8eac 100644 --- a/src/main/java/de/skate702/craftingkeys/util/InputUtil.java +++ b/src/main/java/de/skate702/craftingkeys/util/InputUtil.java @@ -41,7 +41,7 @@ public class InputUtil { } return null; } else { - Helper.debugPrint("getSlotAtMousePosition(): guiContainer == null"); + System.out.println("getSlotAtMousePosition(): guiContainer == null"); return null; } } diff --git a/src/main/java/de/skate702/craftingkeys/util/Util.java b/src/main/java/de/skate702/craftingkeys/util/Util.java index 886a48c..fbd6186 100644 --- a/src/main/java/de/skate702/craftingkeys/util/Util.java +++ b/src/main/java/de/skate702/craftingkeys/util/Util.java @@ -4,6 +4,7 @@ import cpw.mods.fml.client.FMLClientHandler; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiCrafting; +import net.minecraft.client.gui.inventory.GuiInventory; import net.minecraft.util.ChatComponentTranslation; /** @@ -66,7 +67,6 @@ public class Util { * @return True, if GuiScreen; False if null */ public static boolean isCraftingGUI(GuiScreen screen) { - if (screen != null) { if (screen instanceof GuiCrafting) { return true; @@ -74,4 +74,23 @@ public class Util { } return false; } + + public static boolean isInventoryGUI(GuiScreen screen) { + if (screen != null) { + if (screen instanceof GuiInventory) { + return true; + } + } + return false; + } + + public static boolean isVillagerGUI(GuiScreen screen) { + if (screen != null) { + if (screen instanceof net.minecraft.client.gui.GuiMerchant) { + return true; + } + } + return false; + } + }