From eb45b90b81b0ccc0ccd9de333ee83fa84e5ce474 Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Fri, 30 Jan 2015 14:36:30 +0100 Subject: [PATCH 1/6] Initial Commit. Alle basic Methods, based on Inventory Tweaks. --- .../craftingkeys/ContainerManager.java | 168 ++++++++++++++++++ .../skate702/craftingkeys/CraftingKeys.java | 113 ++++++++++++ .../java/de/skate702/craftingkeys/Helper.java | 124 +++++++++++++ .../proxies/CraftingKeysClientProxy.java | 30 ++++ .../proxies/CraftingKeysProxy.java | 45 +++++ src/main/resources/mcmod.info | 16 ++ 6 files changed, 496 insertions(+) create mode 100644 src/main/java/de/skate702/craftingkeys/ContainerManager.java create mode 100644 src/main/java/de/skate702/craftingkeys/CraftingKeys.java create mode 100644 src/main/java/de/skate702/craftingkeys/Helper.java create mode 100644 src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java create mode 100644 src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java create mode 100644 src/main/resources/mcmod.info diff --git a/src/main/java/de/skate702/craftingkeys/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/ContainerManager.java new file mode 100644 index 0000000..abe9c54 --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/ContainerManager.java @@ -0,0 +1,168 @@ +package de.skate702.craftingkeys; + +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +/** + * Managing Class to move Items in Inventory Containers. + * + * @author skate702 + * + */ +public class ContainerManager { + + /** + * The Container to work with. + */ + private Container container; + + public ContainerManager(Container container) { + + this.container = container; + + } + + /** + * Moves a full Stack from a slot to another. + * + * @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) { + + // TODO: Consistency - Use move()-Method + + ItemStack source = getItemStack(srcIndex); + ItemStack destination = getItemStack(destIndex); + + leftClick(srcIndex); + leftClick(destIndex); + + Helper.debugPrint("moveAll(): Moved Stack from " + srcIndex + " to " + destIndex + "!"); + + // Stack Swapping + if (Helper.client.thePlayer.inventory.getItemStack() != null) { + + Helper.debugPrint("moveAll(): Destination not empty!"); + leftClick(srcIndex); + + } + + } + + /** + * Moves a specified amount of Items from a slot to another. [Based on + * INVTW] + * + * @param srcIndex + * The Source Slot Index of the Container + * @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) { + + // TODO: What if not same type? Fallback Solution (Stack out of the way) + + // Stacks + ItemStack source = getItemStack(srcIndex); + ItemStack destination = getItemStack(destIndex); + + // Same Location? + if (source == null || srcIndex == destIndex) { + Helper.debugPrint("Move(): srcIndex == destIndex"); + return; + } + + // Test for max. moving Amount + int sourceSize = source.stackSize; + int movedAmount = Math.min(amount, sourceSize); + + // Move some + if (destination == null || source.isItemEqual(destination)) { + + leftClick(srcIndex); + for (int i = 0; i < movedAmount; i++) { + rightClick(destIndex); + } + + // Move back + if (movedAmount < sourceSize) { + leftClick(srcIndex); + } + + Helper.debugPrint("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!"); + + } else { + Helper.debugPrint("Move(): Not the same block type!"); + } + + } + + /** + * Returns the ItemStack in a slot [Based on INVTW] + * + * @param index + * The index of the slot in the container + * @return Returns the ItemStack + */ + private ItemStack getItemStack(int index) { + + if (index >= 0 && index < container.inventorySlots.size()) { + + Slot slot = (Slot) (container.inventorySlots.get(index)); + return (slot == null) ? null : slot.getStack(); + + } else { + + Helper.debugPrint("getItemStack(): Invalid index"); + return null; + + } + + } + + /** + * Executes a left mouse click on a slot. [Based on INVTW] + * + * @param index + * The index of the slot in the container + */ + private void leftClick(int index) { + slotClick(index, false); + } + + /** + * Executes a right mouse click on a slot. [Based on INVTW] + * + * @param index + * The index of the slot in the container + */ + private void rightClick(int index) { + slotClick(index, true); + } + + /** + * Executes a mouse click on a slot. [Based on INVTW] + * + * @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) { + + Helper.debugPrint("slotClick(): Clicked @ Slot " + index + " with data " + rightClick + "."); + + int rightClickData = (rightClick) ? 1 : 0; + + CraftingKeys.instance.proxy.sendSlotClick(Helper.client.playerController, container.windowId, index, + rightClickData, 0, Helper.client.thePlayer); + + } + +} diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java new file mode 100644 index 0000000..53f702f --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -0,0 +1,113 @@ +package de.skate702.craftingkeys; + +import org.lwjgl.input.Keyboard; + +import net.minecraft.client.gui.inventory.GuiCrafting; +import net.minecraft.inventory.Slot; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.Mod.Instance; +import cpw.mods.fml.common.SidedProxy; +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.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import de.skate702.craftingkeys.proxies.CraftingKeysProxy; + +/** + * The Main Class of the Mod with the important onTick-Method. Some Methods are + * base on the open-source Inventory Tweaks. Big Thanks for that! + * + * @author skate702 + * + */ +@Mod(modid = "CraftingKeysID", name = "Crafting Keys", version = "1.0.0") +public class CraftingKeys { + + /** + * Current Instance of CraftingKeys. + */ + @Instance(value = "CraftingKeysID") + public static CraftingKeys instance; + + /** + * Current Proxy (Common or Client) + */ + @SidedProxy(clientSide = "de.skate702.craftingkeys.proxies.CraftingKeysClientProxy", serverSide = "de.skate702.craftingkeys.proxies.CraftingKeysProxy") + public static CraftingKeysProxy proxy; + + /** + * This method will be executed before Init. + * + * @param event + * Input Event from FML + */ + @EventHandler + public void preInit(FMLPreInitializationEvent event) { + } + + /** + * This method will be executed while loading. + * + * @param event + * Input Event from FML + */ + @EventHandler + public void load(FMLInitializationEvent event) { + + // Regeistring + proxy.registerRenderers(); + FMLCommonHandler.instance().bus().register(this); + + Helper.debugPrint("load(): Loaded Mod successful"); + + } + + /** + * This method will be executed after Init. + * + * @param event + * Input Event from FML + */ + @EventHandler + public void postInit(FMLPostInitializationEvent event) { + } + + /** + * This method will be executed every Ingame Tick. + * + * @param tick + */ + @SubscribeEvent + public void onTick(TickEvent.ClientTickEvent tick) { + + // if (mc.currentScreen != null) { + // if (mc.currentScreen instanceof + // net.minecraft.client.gui.inventory.GuiCrafting) { + // Keyboard.isKeyDown(Keyboard.KEY_Q) + + // DEBUGGIN TEST START + + if (Helper.client.currentScreen != null) { + if (Helper.client.currentScreen instanceof GuiCrafting) { + if (Keyboard.isKeyDown(Keyboard.KEY_C)) { + + GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen; + Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting); + + ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); + if (currentHoveredSlot != null) { + con.move(currentHoveredSlot.slotNumber, 8, 1); + } else { + Helper.debugPrint("onTick(): Outside of inventory"); + } + } + } + } + + // DEBUGGIN TEST END + + } +} diff --git a/src/main/java/de/skate702/craftingkeys/Helper.java b/src/main/java/de/skate702/craftingkeys/Helper.java new file mode 100644 index 0000000..8fb0682 --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/Helper.java @@ -0,0 +1,124 @@ +package de.skate702.craftingkeys; + +import org.lwjgl.input.Mouse; + +import cpw.mods.fml.client.FMLClientHandler; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; + +/** + * This helper class provides some static helping Methods and Constants. + * + * @author sebastian + * + */ +public class Helper { + + /** + * Private Constructor. This is a helping class! + */ + private Helper() { + // Nope. + } + + /** + * If true, there are Debug-Output-Prints from debugPrint(msg). + */ + public static final boolean DEBUG = true; + + /** + * Current Instance Client, used for a lot of operations. + */ + public static Minecraft client = FMLClientHandler.instance().getClient(); + + /** + * Standart Output for Debug-Messages - Depends on DEBUG-Constant. + * + * @param message + * The Debug-Message. Syntax: "methodname(): Message" + */ + public static void debugPrint(String message) { + + if (DEBUG) { + System.out.println("CK-DEBUG: " + message); + } + + } + + /** + * Returns the Slot at the current Mouse Position. [FROM GUICONTAINER] + * + * @param guiContainer + * The (Inventory) Container to work with. + * @return Returns the slot (or null) + */ + public static Slot getSlotAtMousePosition(GuiContainer guiContainer) { + if (guiContainer != null) { + Container container = guiContainer.inventorySlots; + + int x = getMouseX(guiContainer); + int y = getMouseY(guiContainer); + for (int k = 0; k < container.inventorySlots.size(); k++) { + Slot slot = (Slot) container.inventorySlots.get(k); + if (getIsMouseOverSlot(guiContainer, slot, x, y)) { + return slot; + } + } + return null; + } else { + Helper.debugPrint("getSlotAtMousePosition(): guiContainer == null"); + return null; + } + } + + /** + * Returns, if the mouse cursor is over a specified slot [FROM GUICONTAINER] + * + * @param guiContainer + * the GuiContainer to work with + * @param slot + * The spcified slot + * @param x + * The Mouse x-Position + * @param y + * The Mouse y-Position + * @return True, if the mouse is positioned over this slot. Otherwise false + */ + private static boolean getIsMouseOverSlot(GuiContainer guiContainer, Slot slot, int x, int y) { + if (guiContainer != null) { + + // Constants from Minecraft Source Code + x -= (guiContainer.width - 176) / 2; + y -= (guiContainer.height - 166) / 2; + return x >= slot.xDisplayPosition - 1 && x < slot.xDisplayPosition + 16 + 1 + && y >= slot.yDisplayPosition - 1 && y < slot.yDisplayPosition + 16 + 1; + } else { + return false; + } + } + + /** + * Returns a calculated Mouse Position for getSlotAtMousePosition(). + * + * @param guiContainer + * the GuiContainer to work with + * @return The relative Mouse-Position + */ + private static int getMouseX(GuiContainer guiContainer) { + return (Mouse.getEventX() * guiContainer.width) / client.displayWidth; + } + + /** + * Returns a calculated Mouse Position for getSlotAtMousePosition(). + * + * @param guiContainer + * the GuiContainer to work with + * @return The relative Mouse-Position + */ + private static int getMouseY(GuiContainer guiContainer) { + return guiContainer.height - (Mouse.getEventY() * guiContainer.height) / client.displayHeight - 1; + } + +} diff --git a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java new file mode 100644 index 0000000..cf2b588 --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java @@ -0,0 +1,30 @@ +package de.skate702.craftingkeys.proxies; + +import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.entity.player.EntityPlayer; + +/** + * Client Proxy, extending Common Proxy. + * + * @author skate702 + * + */ +public class CraftingKeysClientProxy extends CraftingKeysProxy { + + @Override + public void registerRenderers() { + + } + + @Override + public void sendSlotClick(PlayerControllerMP controller, int windowId, int slot, int rightClick, int action, + EntityPlayer player) { + + // TODO: Multiplayer friendly? + + controller.windowClick(windowId, slot, rightClick, action, player); + // player.openContainer.slotClick(slot, rightClick, action, player); + + } + +} diff --git a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java new file mode 100644 index 0000000..2731fa4 --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java @@ -0,0 +1,45 @@ +package de.skate702.craftingkeys.proxies; + +import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.entity.player.EntityPlayer; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +/** + * The common proxy. + * + * @author skate702 + * + */ +public class CraftingKeysProxy { + + /** + * Register Renderers (not needed here). + */ + public void registerRenderers() { + + } + + /** + * The Minecraft call to send a mouse click to a GUI. [Based on INVTW] + * + * @param controller + * The playerController (from Client) + * @param windowId + * The current Windows ID (from GUI) + * @param slot + * The slot to click on (slot index) + * @param rightClick + * 1, if right click. Otherwise 0 + * @param action + * 0 + * @param player + * The current player (from Client) + */ + @SideOnly(Side.CLIENT) + public void sendSlotClick(PlayerControllerMP controller, int windowId, int slot, int rightClick, int action, + EntityPlayer player) { + + } + +} diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100644 index 0000000..5001c10 --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,16 @@ +[ +{ + "modid": "CraftingKeysID", + "name": "Crafting Keys", + "description": "TODO", + "version": "${version}", + "mcversion": "${mcversion}", + "url": "http://skate702.de", + "updateUrl": "http://skate702.de", + "authorList": ["skate702"], + "credits": "Thanks to Inventory-Tweaks!", + "logoFile": "", + "screenshots": [], + "dependencies": [] +} +] From 8c898c78b0600e0351c20310c51e7e19e8bd0c26 Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Mon, 2 Feb 2015 12:57:10 +0100 Subject: [PATCH 2/6] Added basic functionality. A lot of TODO's remaining. --- .../skate702/craftingkeys/CraftingKeys.java | 38 +++++---- .../java/de/skate702/craftingkeys/Helper.java | 78 +++++++++++++++++++ .../de/skate702/craftingkeys/Settings.java | 5 ++ 3 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/skate702/craftingkeys/Settings.java diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java index 53f702f..b2670ce 100644 --- a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -61,7 +61,7 @@ public class CraftingKeys { proxy.registerRenderers(); FMLCommonHandler.instance().bus().register(this); - Helper.debugPrint("load(): Loaded Mod successful"); + Helper.debugPrint("load(): Loaded CraftingKeys successful"); } @@ -83,31 +83,35 @@ public class CraftingKeys { @SubscribeEvent public void onTick(TickEvent.ClientTickEvent tick) { - // if (mc.currentScreen != null) { - // if (mc.currentScreen instanceof - // net.minecraft.client.gui.inventory.GuiCrafting) { - // Keyboard.isKeyDown(Keyboard.KEY_Q) + // Case 1: Classic GUI Screen + if (Helper.isCraftingGUI(Helper.client.currentScreen)) { - // DEBUGGIN TEST START + GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen; + Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting); + int keyDown = Helper.craftingKeyDownToSlotNumber(); - if (Helper.client.currentScreen != null) { - if (Helper.client.currentScreen instanceof GuiCrafting) { - if (Keyboard.isKeyDown(Keyboard.KEY_C)) { + // Block Key Interval (avoid multiple Runs) + if (!Helper.isSameKey(keyDown)) { - GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen; - Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting); + // Good key, Mouse over Inventory + if (keyDown != -1 && currentHoveredSlot != null) { ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); - if (currentHoveredSlot != null) { - con.move(currentHoveredSlot.slotNumber, 8, 1); - } else { - Helper.debugPrint("onTick(): Outside of inventory"); - } + con.move(currentHoveredSlot.slotNumber, keyDown, 1); + + // TODO: Move more/all! + + // TODO: Strg -> Click on Output + + // TODO: Shift -> Move all! + } } } - // DEBUGGIN TEST END + // Case 2: Inventory (2x2 Crafting, Quick-Armor) + + // TODO: Case 2 } } diff --git a/src/main/java/de/skate702/craftingkeys/Helper.java b/src/main/java/de/skate702/craftingkeys/Helper.java index 8fb0682..478a1e6 100644 --- a/src/main/java/de/skate702/craftingkeys/Helper.java +++ b/src/main/java/de/skate702/craftingkeys/Helper.java @@ -1,10 +1,13 @@ package de.skate702.craftingkeys; +import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import cpw.mods.fml.client.FMLClientHandler; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; @@ -47,6 +50,23 @@ public class Helper { } + /** + * Returns true, if the current Screen is a instance of GUI-Screen. + * + * @param screen + * The input Screen + * @return True, if GuiScreen; False if null + */ + public static boolean isCraftingGUI(GuiScreen screen) { + + if (screen != null) { + if (screen instanceof GuiCrafting) { + return true; + } + } + return false; + } + /** * Returns the Slot at the current Mouse Position. [FROM GUICONTAINER] * @@ -121,4 +141,62 @@ public class Helper { return guiContainer.height - (Mouse.getEventY() * guiContainer.height) / client.displayHeight - 1; } + /** + * Reads the current Keyboard-Input and converts it to a Inventory-Slot. + * + * @return A Inventory-Slot (based on CraftingGUI) + */ + public static int craftingKeyDownToSlotNumber() { + + // TODO: Make this dynamic! // Use Settings + + 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; + } + + return returnValue; + } + + /** + * Saves the last Key that was Pressed + */ + private static int lastKeyDown = -1; + + /** + * Returns, if the current KeyValue is the same + * + * @param currentKey + * the new input value, saved in the method + * @return True, if these are the same keys + */ + public static boolean isSameKey(int currentKey) { + + boolean returnValue = false; + + if (lastKeyDown == currentKey && currentKey != -1) { + returnValue = true; + } + + lastKeyDown = currentKey; + return returnValue; + + } } diff --git a/src/main/java/de/skate702/craftingkeys/Settings.java b/src/main/java/de/skate702/craftingkeys/Settings.java new file mode 100644 index 0000000..6e68fd6 --- /dev/null +++ b/src/main/java/de/skate702/craftingkeys/Settings.java @@ -0,0 +1,5 @@ +package de.skate702.craftingkeys; + +public class Settings { + +} From 934ee4b81627534e1c28f4c80c2c18d141a13ace Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Mon, 2 Feb 2015 21:18:33 +0100 Subject: [PATCH 3/6] Added more stuff to the main method (strg, shift clicking). --- .../craftingkeys/ContainerManager.java | 56 ++++++++++++++----- .../skate702/craftingkeys/CraftingKeys.java | 36 +++++++++--- .../java/de/skate702/craftingkeys/Helper.java | 2 +- .../proxies/CraftingKeysClientProxy.java | 2 +- 4 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/skate702/craftingkeys/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/ContainerManager.java index abe9c54..85c7719 100644 --- a/src/main/java/de/skate702/craftingkeys/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/ContainerManager.java @@ -1,5 +1,6 @@ package de.skate702.craftingkeys; +import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; @@ -33,22 +34,13 @@ public class ContainerManager { */ public void moveAll(int srcIndex, int destIndex) { - // TODO: Consistency - Use move()-Method - ItemStack source = getItemStack(srcIndex); - ItemStack destination = getItemStack(destIndex); - - leftClick(srcIndex); - leftClick(destIndex); - - Helper.debugPrint("moveAll(): Moved Stack from " + srcIndex + " to " + destIndex + "!"); - - // Stack Swapping - if (Helper.client.thePlayer.inventory.getItemStack() != null) { - - Helper.debugPrint("moveAll(): Destination not empty!"); - leftClick(srcIndex); + if (source == null) { + Helper.debugPrint("moveAll(): source == null"); + } else { + Helper.debugPrint("moveAll(): Redirected to move()"); + move(srcIndex, destIndex, source.stackSize); } } @@ -67,6 +59,7 @@ public class ContainerManager { public void move(int srcIndex, int destIndex, int amount) { // TODO: What if not same type? Fallback Solution (Stack out of the way) + // or swap // if (Helper.client.thePlayer.inventory.getItemStack() != // Stacks ItemStack source = getItemStack(srcIndex); @@ -74,7 +67,7 @@ public class ContainerManager { // Same Location? if (source == null || srcIndex == destIndex) { - Helper.debugPrint("Move(): srcIndex == destIndex"); + Helper.debugPrint("Move(): srcIndex == destIndex OR source == null"); return; } @@ -126,6 +119,39 @@ public class ContainerManager { } + /** + * Sends a click on the crafting output (craftingGUI or Inventory) + * + * @param isCraftingGUI + * true, if the craftingGUI is opened + */ + public void clickOnCraftingOutput(boolean isCraftingGUI) { + + // TODO: Put current Item away + + if (isCraftingGUI) { + + // Click on crafting output + Helper.debugPrint("clickOnCraftingOutput(): Clicked on Crafing Output."); + leftClick(0); + + } + + } + + /** + * Takes all items from a slot and moves them to the next empty slot or + * drops them. + * + * @param index + * The index of the slot to move items from + */ + private void putStackToNextEmptySlot(int index) { + + // TODO: Method (maybe from INVTW...?) + + } + /** * Executes a left mouse click on a slot. [Based on INVTW] * diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java index b2670ce..9b1af7c 100644 --- a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -93,19 +93,37 @@ public class CraftingKeys { // Block Key Interval (avoid multiple Runs) if (!Helper.isSameKey(keyDown)) { - // Good key, Mouse over Inventory + ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); + + // Moving item to crafting table if (keyDown != -1 && currentHoveredSlot != null) { - ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); - con.move(currentHoveredSlot.slotNumber, keyDown, 1); - - // TODO: Move more/all! - - // TODO: Strg -> Click on Output - - // TODO: Shift -> Move all! + // Shift = Move all + if (guiCrafting.isShiftKeyDown()) { + con.moveAll(currentHoveredSlot.slotNumber, keyDown); + } else { + con.move(currentHoveredSlot.slotNumber, keyDown, 1); + } } + + // Strg = Take the output + if (guiCrafting.isCtrlKeyDown()) { + + if (guiCrafting.isShiftKeyDown()) { + + // TODO: Strg + Shift = Empty Crafting Table + Helper.debugPrint("onTick(): [TODO] Move all items back or drop them."); + + } else { + + // Send mouse click on crafting output + con.clickOnCraftingOutput(true); + + } + + } + } } diff --git a/src/main/java/de/skate702/craftingkeys/Helper.java b/src/main/java/de/skate702/craftingkeys/Helper.java index 478a1e6..ee49371 100644 --- a/src/main/java/de/skate702/craftingkeys/Helper.java +++ b/src/main/java/de/skate702/craftingkeys/Helper.java @@ -171,7 +171,7 @@ public class Helper { } else if (Keyboard.isKeyDown(Keyboard.KEY_C)) { returnValue = 9; } - + return returnValue; } diff --git a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java index cf2b588..4970649 100644 --- a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java +++ b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java @@ -20,7 +20,7 @@ public class CraftingKeysClientProxy extends CraftingKeysProxy { public void sendSlotClick(PlayerControllerMP controller, int windowId, int slot, int rightClick, int action, EntityPlayer player) { - // TODO: Multiplayer friendly? + // TODO: Make this *** multiplayer friendly controller.windowClick(windowId, slot, rightClick, action, player); // player.openContainer.slotClick(slot, rightClick, action, player); From 629d824efe61360ba675a0c4cc522b7330e8c6fd Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Mon, 23 Feb 2015 17:57:59 +0100 Subject: [PATCH 4/6] Added full Ctrl-Functionality. --- .../craftingkeys/ContainerManager.java | 2 +- .../skate702/craftingkeys/CraftingKeys.java | 25 +++++++++++++---- .../java/de/skate702/craftingkeys/Helper.java | 28 +++++++++++++++++-- .../de/skate702/craftingkeys/Settings.java | 1 + .../proxies/CraftingKeysClientProxy.java | 2 +- .../proxies/CraftingKeysProxy.java | 2 +- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/skate702/craftingkeys/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/ContainerManager.java index 85c7719..6d26c67 100644 --- a/src/main/java/de/skate702/craftingkeys/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/ContainerManager.java @@ -127,7 +127,7 @@ public class ContainerManager { */ public void clickOnCraftingOutput(boolean isCraftingGUI) { - // TODO: Put current Item away + // TODO: Put current Item away if (isCraftingGUI) { diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java index 9b1af7c..fa8add7 100644 --- a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -96,7 +96,7 @@ public class CraftingKeys { ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); // Moving item to crafting table - if (keyDown != -1 && currentHoveredSlot != null) { + if (keyDown > 0 && currentHoveredSlot != null) { // Shift = Move all if (guiCrafting.isShiftKeyDown()) { @@ -107,21 +107,36 @@ public class CraftingKeys { } + if (keyDown == -2) { + + // TODO: Space = Move all back + Helper.debugPrint("onTick(): [TODO] Move all items back or drop them."); + + } + // Strg = Take the output if (guiCrafting.isCtrlKeyDown()) { if (guiCrafting.isShiftKeyDown()) { - // TODO: Strg + Shift = Empty Crafting Table - Helper.debugPrint("onTick(): [TODO] Move all items back or drop them."); + //Strg + Shift = Move all (resp. faster!) + con.clickOnCraftingOutput(true); } else { - // Send mouse click on crafting output - con.clickOnCraftingOutput(true); + // 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); } } diff --git a/src/main/java/de/skate702/craftingkeys/Helper.java b/src/main/java/de/skate702/craftingkeys/Helper.java index ee49371..182d1da 100644 --- a/src/main/java/de/skate702/craftingkeys/Helper.java +++ b/src/main/java/de/skate702/craftingkeys/Helper.java @@ -144,7 +144,8 @@ public class Helper { /** * Reads the current Keyboard-Input and converts it to a Inventory-Slot. * - * @return A Inventory-Slot (based on CraftingGUI) + * @return A Inventory-Slot (based on CraftingGUI), -1 for wrong input, -2 + * for space */ public static int craftingKeyDownToSlotNumber() { @@ -170,8 +171,10 @@ public class Helper { returnValue = 8; } else if (Keyboard.isKeyDown(Keyboard.KEY_C)) { returnValue = 9; + } else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { + returnValue = -2; } - + return returnValue; } @@ -199,4 +202,25 @@ public class Helper { return returnValue; } + + /** + * Saves the times strg was pressed before reseting + */ + private static int strgTimesDown = 0; + + /** + * Return the times Strg was pressed before reseting + * + * @param strgDown + * false, if reset + * @return A number of tick strg was down before + */ + public static int getStrgTimesDown(boolean strgDown) { + + if (!strgDown) + strgTimesDown = 0; + + return ++strgTimesDown; + + } } diff --git a/src/main/java/de/skate702/craftingkeys/Settings.java b/src/main/java/de/skate702/craftingkeys/Settings.java index 6e68fd6..6c89ac2 100644 --- a/src/main/java/de/skate702/craftingkeys/Settings.java +++ b/src/main/java/de/skate702/craftingkeys/Settings.java @@ -3,3 +3,4 @@ package de.skate702.craftingkeys; public class Settings { } + \ No newline at end of file diff --git a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java index 4970649..1d295db 100644 --- a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java +++ b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysClientProxy.java @@ -13,7 +13,7 @@ public class CraftingKeysClientProxy extends CraftingKeysProxy { @Override public void registerRenderers() { - + } @Override diff --git a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java index 2731fa4..140f492 100644 --- a/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java +++ b/src/main/java/de/skate702/craftingkeys/proxies/CraftingKeysProxy.java @@ -19,7 +19,7 @@ public class CraftingKeysProxy { public void registerRenderers() { } - + /** * The Minecraft call to send a mouse click to a GUI. [Based on INVTW] * From 75e5dfe1f2882fd996abb2ba4942a411c24118a4 Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Wed, 25 Feb 2015 00:59:28 +0100 Subject: [PATCH 5/6] Added moving back from crafting table feature, some fixes, chat message, dropping. Nice things! --- .../craftingkeys/ContainerManager.java | 91 +++++++++++++++++-- .../skate702/craftingkeys/CraftingKeys.java | 34 ++++++- 2 files changed, 113 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/skate702/craftingkeys/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/ContainerManager.java index 6d26c67..9230a11 100644 --- a/src/main/java/de/skate702/craftingkeys/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/ContainerManager.java @@ -4,6 +4,8 @@ import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.IChatComponent; /** * Managing Class to move Items in Inventory Containers. @@ -58,9 +60,6 @@ public class ContainerManager { */ public void move(int srcIndex, int destIndex, int amount) { - // TODO: What if not same type? Fallback Solution (Stack out of the way) - // or swap // if (Helper.client.thePlayer.inventory.getItemStack() != - // Stacks ItemStack source = getItemStack(srcIndex); ItemStack destination = getItemStack(destIndex); @@ -127,7 +126,7 @@ public class ContainerManager { */ public void clickOnCraftingOutput(boolean isCraftingGUI) { - // TODO: Put current Item away + putItemAway(isCraftingGUI); if (isCraftingGUI) { @@ -135,6 +134,8 @@ public class ContainerManager { Helper.debugPrint("clickOnCraftingOutput(): Clicked on Crafing Output."); leftClick(0); + } else { + // TODO: Same for inventory } } @@ -143,13 +144,89 @@ public class ContainerManager { * Takes all items from a slot and moves them to the next empty slot or * drops them. * - * @param index + * @param sourceIndex * The index of the slot to move items from + * @param isCraftingGUI + * true, if the craftingGUI is opened */ - private void putStackToNextEmptySlot(int index) { + public void putStackToNextEmptySlot(int sourceIndex, boolean isCraftingGUI, boolean isHeld) { - // TODO: Method (maybe from INVTW...?) + // Check for Item-Type + ItemStack stackToMove; + if (isHeld) { + stackToMove = Helper.client.thePlayer.inventory.getItemStack(); + } else { + putItemAway(isCraftingGUI); + stackToMove = getItemStack(sourceIndex); + } + // Test for empty crafting table slot + if (!isHeld && getItemStack(sourceIndex) == null) { + + Helper.debugPrint("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + "."); + return; + } + + // Get Destination Index + int destIndex = getFirstPropperSlotIndex(isCraftingGUI, stackToMove); + + // Additional click on source index, if not held + if (!isHeld) { + leftClick(sourceIndex); + } + + // TODO: Optional (beta-like): Fill the items up. Let's become INVTW! + + // destIndex = -1 -> drop item + if (destIndex == -1) { + leftClick(-999); // Nice one, InvTweaks! + } else { + leftClick(destIndex); + } + + } + + private void putItemAway(boolean isCraftingGUI) { + // Put current Item away + if (Helper.client.thePlayer.inventory.getItemStack() != null) { + putStackToNextEmptySlot(-1, isCraftingGUI, true); + } + } + + /** + * Returns the first free index in a inventory + * + * @param isCraftingGUI + * true, if the craftingGUI is opened + * @return a slot index + */ + private int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) { + + if (isCraftingGUI) { + + for (int i = 10; i < container.inventorySlots.size(); i++) { + + if (getItemStack(i) != null) { + if (getItemStack(i).isItemEqual(movingItem)) { + if (getItemStack(i).stackSize + movingItem.stackSize <= movingItem.getMaxStackSize()) { + return i; + } + } + } + } + + for (int i = 10; i < container.inventorySlots.size(); i++) { + + if (getItemStack(i) == null) { + return i; + } + } + Helper.debugPrint("getFirstProperSlotIndex(): No Propper / Empty Slot found!"); + return -1; + } else { + // TODO: Same for inventory + return -1; + } } /** diff --git a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java index fa8add7..d26efa3 100644 --- a/src/main/java/de/skate702/craftingkeys/CraftingKeys.java +++ b/src/main/java/de/skate702/craftingkeys/CraftingKeys.java @@ -4,6 +4,7 @@ import org.lwjgl.input.Keyboard; import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.inventory.Slot; +import net.minecraft.util.ChatComponentText; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; @@ -75,6 +76,11 @@ public class CraftingKeys { public void postInit(FMLPostInitializationEvent event) { } + /** + * To show a chat message at first start in minecraft. + */ + boolean firstInWorldTick = true; + /** * This method will be executed every Ingame Tick. * @@ -83,6 +89,19 @@ public class CraftingKeys { @SubscribeEvent public void onTick(TickEvent.ClientTickEvent tick) { + // Message + if (Helper.client.theWorld != null && firstInWorldTick) { + + Helper.client.thePlayer.addChatMessage(new ChatComponentText( + "Achtung: Crafting-Keys befindet sich in der Pre-Alpha!")); + Helper.client.thePlayer.addChatMessage(new ChatComponentText( + "Es gibt noch viele Fehler, und es fehlen noch einige Funktionen.")); + Helper.client.thePlayer.addChatMessage(new ChatComponentText( + "Mehr Info's findest du auf: http://craftingkeys.codeplex.com!")); + + firstInWorldTick = !firstInWorldTick; + } + // Case 1: Classic GUI Screen if (Helper.isCraftingGUI(Helper.client.currentScreen)) { @@ -90,11 +109,11 @@ public class CraftingKeys { Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting); int keyDown = Helper.craftingKeyDownToSlotNumber(); + ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); + // Block Key Interval (avoid multiple Runs) if (!Helper.isSameKey(keyDown)) { - ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); - // Moving item to crafting table if (keyDown > 0 && currentHoveredSlot != null) { @@ -109,9 +128,14 @@ public class CraftingKeys { if (keyDown == -2) { - // TODO: Space = Move all back - Helper.debugPrint("onTick(): [TODO] Move all items back or drop them."); + // 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 @@ -119,7 +143,7 @@ public class CraftingKeys { if (guiCrafting.isShiftKeyDown()) { - //Strg + Shift = Move all (resp. faster!) + // Strg + Shift = Move all (resp. faster!) con.clickOnCraftingOutput(true); } else { From a0d280d55d2de0248247d3c9b4751d806594f03b Mon Sep 17 00:00:00 2001 From: 0xSeb Date: Sun, 21 Jun 2015 20:24:45 +0200 Subject: [PATCH 6/6] Some simple test commit without changes --- .../craftingkeys/ContainerManager.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/skate702/craftingkeys/ContainerManager.java b/src/main/java/de/skate702/craftingkeys/ContainerManager.java index 9230a11..96a9ff7 100644 --- a/src/main/java/de/skate702/craftingkeys/ContainerManager.java +++ b/src/main/java/de/skate702/craftingkeys/ContainerManager.java @@ -9,7 +9,7 @@ import net.minecraft.util.IChatComponent; /** * Managing Class to move Items in Inventory Containers. - * + * * @author skate702 * */ @@ -28,7 +28,7 @@ public class ContainerManager { /** * Moves a full Stack from a slot to another. - * + * * @param srcIndex * The Source Slot Index of the Container * @param destIndex @@ -50,7 +50,7 @@ public class ContainerManager { /** * Moves a specified amount of Items from a slot to another. [Based on * INVTW] - * + * * @param srcIndex * The Source Slot Index of the Container * @param destIndex @@ -97,7 +97,7 @@ public class ContainerManager { /** * Returns the ItemStack in a slot [Based on INVTW] - * + * * @param index * The index of the slot in the container * @return Returns the ItemStack @@ -120,7 +120,7 @@ public class ContainerManager { /** * Sends a click on the crafting output (craftingGUI or Inventory) - * + * * @param isCraftingGUI * true, if the craftingGUI is opened */ @@ -143,7 +143,7 @@ public class ContainerManager { /** * Takes all items from a slot and moves them to the next empty slot or * drops them. - * + * * @param sourceIndex * The index of the slot to move items from * @param isCraftingGUI @@ -195,7 +195,7 @@ public class ContainerManager { /** * Returns the first free index in a inventory - * + * * @param isCraftingGUI * true, if the craftingGUI is opened * @return a slot index @@ -231,7 +231,7 @@ public class ContainerManager { /** * Executes a left mouse click on a slot. [Based on INVTW] - * + * * @param index * The index of the slot in the container */ @@ -241,7 +241,7 @@ public class ContainerManager { /** * Executes a right mouse click on a slot. [Based on INVTW] - * + * * @param index * The index of the slot in the container */ @@ -251,7 +251,7 @@ public class ContainerManager { /** * Executes a mouse click on a slot. [Based on INVTW] - * + * * @param index * The index of the slot in the container * @param rightClick