Refactored all the gui inventory managing. Was a lot. Now adding new GUIs should be fun!

This commit is contained in:
0xSeb
2015-11-02 22:17:51 +01:00
parent a428346b69
commit 01f992e58a
5 changed files with 245 additions and 275 deletions
-2
View File
@@ -6,8 +6,6 @@ Crafting Keys is a modification which adds a lot of new key bindings to minecraf
##Development Progress ##Development Progress
The whole project had been refactored and is now almost ready to be release. But there are missing a few things: 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 - Implementing all vanilla guis
- Better, all new GUI - Better, all new GUI
@@ -36,7 +36,7 @@ public class Config {
/** /**
* Defines all 11 Keys you can use with Crafting Keys. * 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, keyCenterLeft, keyCenterCenter, keyCenterRight,
keyLowerLeft, keyLowerCenter, keyLowerRight, keyLowerLeft, keyLowerCenter, keyLowerRight,
keyStack, keyInteract, keyDrop; keyStack, keyInteract, keyDrop;
@@ -44,54 +44,54 @@ public class Config {
/** /**
* Defines, if NumPad is always active for crafting. * Defines, if NumPad is always active for crafting.
*/ */
private static Property enableNumPad; protected static Property enableNumPad;
public static int getKeyTopLeft() { public static boolean isKeyTopLeftPressed() {
return keyTopLeft.getInt(retDefKey); return Keyboard.isKeyDown(keyTopLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(71);
} }
public static int getKeyTopCenter() { public static boolean isKeyTopCenterPressed() {
return keyTopCenter.getInt(retDefKey); return Keyboard.isKeyDown(keyTopCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(72);
} }
public static int getKeyTopRight() { public static boolean isKeyTopRightPressed() {
return keyTopRight.getInt(retDefKey); return Keyboard.isKeyDown(keyTopRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(73);
} }
public static int getKeyCenterLeft() { public static boolean isKeyCenterLeftPressed() {
return keyCenterLeft.getInt(retDefKey); return Keyboard.isKeyDown(keyCenterLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(75);
} }
public static int getKeyCenterCenter() { public static boolean isKeyCenterCenterPressed() {
return keyCenterCenter.getInt(retDefKey); return Keyboard.isKeyDown(keyCenterCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(76);
} }
public static int getKeyCenterRight() { public static boolean isKeyCenterRightPressed() {
return keyCenterRight.getInt(retDefKey); return Keyboard.isKeyDown(keyCenterRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(77);
} }
public static int getKeyLowerLeft() { public static boolean isKeyLowerLeftPressed() {
return keyLowerLeft.getInt(retDefKey); return Keyboard.isKeyDown(keyLowerLeft.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(79);
} }
public static int getKeyLowerCenter() { public static boolean isKeyLowerCenterPressed() {
return keyLowerCenter.getInt(retDefKey); return Keyboard.isKeyDown(keyLowerCenter.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(80);
} }
public static int getKeyLowerRight() { public static boolean isKeyLowerRightPressed() {
return keyLowerRight.getInt(retDefKey); return Keyboard.isKeyDown(keyLowerRight.getInt(retDefKey)) || isNumPadEnabled() && Keyboard.isKeyDown(81);
} }
public static int getKeyStack() { public static boolean isKeyStackPressed() {
return keyStack.getInt(retDefKey); return Keyboard.isKeyDown(keyStack.getInt(retDefKey));
} }
public static int getKeyInteract() { public static boolean isKeyInteractPressed() {
return keyInteract.getInt(retDefKey); return Keyboard.isKeyDown(keyInteract.getInt(retDefKey));
} }
public static int getKeyDrop() { public static boolean isKeyDropPressed() {
return keyDrop.getInt(retDefKey); return Keyboard.isKeyDown(keyDrop.getInt(retDefKey));
} }
public static boolean isNumPadEnabled() { public static boolean isNumPadEnabled() {
@@ -2,13 +2,18 @@ package de.skate702.craftingkeys.manager;
import de.skate702.craftingkeys.CraftingKeys; import de.skate702.craftingkeys.CraftingKeys;
import de.skate702.craftingkeys.config.Config; import de.skate702.craftingkeys.config.Config;
import de.skate702.craftingkeys.util.InputUtil;
import de.skate702.craftingkeys.util.Logger; import de.skate702.craftingkeys.util.Logger;
import de.skate702.craftingkeys.util.Util; import de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard; 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 { public abstract class ContainerManager {
/** /**
@@ -27,9 +32,123 @@ public abstract class ContainerManager {
/** /**
* Checks the current keyDown-Value and does the work! * 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() { protected void handleNumKey() {
// hotbar-slots are always the last 9 slots of the currently opened inventory // 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 * @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() { protected int mapKeyToSlot(int topLeft, int topCenter, int topRight,
return Keyboard.isKeyDown(Config.getKeyStack()); int centerLeft, int centerCenter, int centerRight,
int lowerLeft, int lowerCenter, int lowerRight) {
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 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());
} }
/** /**
@@ -110,7 +248,7 @@ public abstract class ContainerManager {
* @param srcIndex The Source Slot Index of the Container * @param srcIndex The Source Slot Index of the Container
* @param destIndex The Destination 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); ItemStack source = getItemStack(srcIndex);
@@ -130,7 +268,7 @@ public abstract class ContainerManager {
* @param destIndex The Destination 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) * @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 // Stacks
ItemStack source = getItemStack(srcIndex); ItemStack source = getItemStack(srcIndex);
@@ -291,6 +429,12 @@ public abstract class ContainerManager {
*/ */
protected abstract int getInventoryStartIndex(); 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] * Executes a left mouse click on a slot. [Based on INVTW]
* *
@@ -1,15 +1,11 @@
package de.skate702.craftingkeys.manager; 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.Logger;
import de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Container; 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 { public class CraftingManager extends ContainerManager {
private static CraftingManager instance = null; private static CraftingManager instance = null;
@@ -39,117 +35,9 @@ public class CraftingManager extends ContainerManager {
} }
@Override @Override
public void acceptKey() { protected int specificKeyToSlotIndex() {
Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiCrafting) Util.client.currentScreen); return mapKeyToSlot(1, 2, 3, 4, 5, 6, 7, 8, 9);
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;
}
} }
@@ -158,12 +46,26 @@ public class CraftingManager extends ContainerManager {
return 10; 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() { private void clickOnCraftingOutput() {
// Click on crafting output
Logger.info("clickOnCraftingOutput()", "Clicked on Crafing Output."); Logger.info("clickOnCraftingOutput()", "Clicked on Crafing Output.");
leftClick(0); leftClick(0);
@@ -1,14 +1,11 @@
package de.skate702.craftingkeys.manager; 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.Logger;
import de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiFurnace;
import net.minecraft.inventory.Container; 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 { public class FurnaceManager extends ContainerManager {
private static FurnaceManager instance = null; private static FurnaceManager instance = null;
@@ -38,105 +35,9 @@ public class FurnaceManager extends ContainerManager {
} }
@Override @Override
public void acceptKey() { protected int specificKeyToSlotIndex() {
Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition((GuiFurnace) Util.client.currentScreen); return mapKeyToSlot(-1, 0, -1, -1, 1, -1, -1, -1, -1);
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;
}
} }
@@ -144,4 +45,29 @@ public class FurnaceManager extends ContainerManager {
protected int getInventoryStartIndex() { protected int getInventoryStartIndex() {
return 3; 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);
}
} }