Finished Refactoring. That was a lot of work. Everything is fine now. Missing: More Logger output, Packet Handling. Than we are in beta-state =D

This commit is contained in:
0xSeb
2015-10-26 01:14:00 +01:00
parent bada3c5910
commit 4ca5648148
5 changed files with 106 additions and 85 deletions
@@ -15,6 +15,7 @@ import de.skate702.craftingkeys.config.Config;
import de.skate702.craftingkeys.manager.ContainerManager; import de.skate702.craftingkeys.manager.ContainerManager;
import de.skate702.craftingkeys.manager.CraftingManager; import de.skate702.craftingkeys.manager.CraftingManager;
import de.skate702.craftingkeys.proxies.CraftingKeysProxy; import de.skate702.craftingkeys.proxies.CraftingKeysProxy;
import de.skate702.craftingkeys.util.Logger;
import de.skate702.craftingkeys.util.Util; import de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.client.gui.inventory.GuiCrafting;
@@ -87,7 +88,7 @@ public class CraftingKeys {
/** /**
* This method will be executed every Ingame Tick. * This method will be executed every Ingame Tick.
* *
* @param tick * @param tick This is a tick. What did you think about it?
*/ */
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent tick) { public void onTick(TickEvent.ClientTickEvent tick) {
@@ -105,11 +106,11 @@ public class CraftingKeys {
} else if (Util.isInventoryGUI(Util.client.currentScreen)) { } else if (Util.isInventoryGUI(Util.client.currentScreen)) {
// do do do // do do do
System.out.println("Inventory"); Logger.warn("onTick()", "Inventory");
} else if (Util.isVillagerGUI(Util.client.currentScreen)) { } else if (Util.isVillagerGUI(Util.client.currentScreen)) {
// do do do // do do do
//((GuiMerchant) Util.client.currentScreen).inventorySlots //((GuiMerchant) Util.client.currentScreen).inventorySlots
System.out.println("Villager"); Logger.warn("onTick()", "Villager");
} }
} }
@@ -92,7 +92,7 @@ public class Config {
return keyDrop.getInt(retDefKey); return keyDrop.getInt(retDefKey);
} }
public static boolean getEnableNumPad() { public static boolean isNumPadEnabled() {
return enableNumPad.getBoolean(true); return enableNumPad.getBoolean(true);
} }
@@ -2,6 +2,7 @@ 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.Logger;
import de.skate702.craftingkeys.util.Util; import de.skate702.craftingkeys.util.Util;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
@@ -75,9 +76,8 @@ public abstract class ContainerManager {
ItemStack source = getItemStack(srcIndex); ItemStack source = getItemStack(srcIndex);
if (source == null) { if (source == null) {
System.out.println("moveAll(): source == null"); Logger.debug("moveAll(i,i)", "Source ItemStack from Index == null");
} else { } else {
System.out.println("moveAll(): Redirected to move()");
move(srcIndex, destIndex, source.stackSize); move(srcIndex, destIndex, source.stackSize);
} }
@@ -99,10 +99,8 @@ public abstract class ContainerManager {
// Same Location? // Same Location?
if (source == null) { if (source == null) {
System.out.println("Move(): srcIndex == destIndex OR source == null");
return; return;
} else if (srcIndex == destIndex) { } else if (srcIndex == destIndex) {
System.out.println("Move(): srcIndex == destIndex OR source == null");
return; return;
} }
@@ -123,10 +121,10 @@ public abstract class ContainerManager {
leftClick(srcIndex); leftClick(srcIndex);
} }
System.out.println("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!"); Logger.info("move(i,i,i)", "Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!");
} else { } else {
System.out.println("Move(): Not the same block type!"); Logger.info("move(i,i,i)", "Not the same block type!");
} }
} }
@@ -145,7 +143,7 @@ public abstract class ContainerManager {
} else { } else {
System.out.println("getItemStack(): Invalid index"); Logger.debug("getItemStack(i)", "Invalid index");
return null; return null;
} }
@@ -153,42 +151,45 @@ public abstract class ContainerManager {
} }
/** /**
* Takes all items from a slot and moves them to the next empty slot or * Moves a stack (held or not) to the next fitting inventory slot.
* drops them.
* *
* @param sourceIndex The index of the slot to move items from * @param sourceIndex A slot index of the source items
* @param isCraftingGUI true, if the craftingGUI is opened
*/ */
protected void putStackToNextEmptySlot(int sourceIndex, boolean isCraftingGUI, boolean isHeld) { protected void moveStackToInventory(int sourceIndex) {
// Check for Item-Type // Moving Stack
ItemStack stackToMove; ItemStack stackToMove = null;
if (isHeld) {
// Get the stack, index or held, cleanup held stack
if (sourceIndex == -1) {
if (Util.client.thePlayer.inventory.getItemStack() != null) {
stackToMove = Util.client.thePlayer.inventory.getItemStack(); stackToMove = Util.client.thePlayer.inventory.getItemStack();
}
} else { } else {
putItemAway(isCraftingGUI);
stackToMove = getItemStack(sourceIndex); stackToMove = getItemStack(sourceIndex);
// Is there a currently held stack?
if (Util.client.thePlayer.inventory.getItemStack() != null) {
moveStackToInventory(-1);
}
} }
// Test for empty crafting table slot // Test stack
if (!isHeld && getItemStack(sourceIndex) == null) { if (stackToMove == null) {
Logger.debug("moveStackToInvetory(i)", "Stack at sourceIndex not found.");
System.out.println("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + ".");
return; return;
} }
// Get Destination Index // Get destination index
int destIndex = getFirstPropperSlotIndex(isCraftingGUI, stackToMove); int destIndex = calcInventoryDestination(stackToMove);
// Additional click on source index, if not held // Additional click on source index, if not held
if (!isHeld) { if (sourceIndex != -1) {
leftClick(sourceIndex); leftClick(sourceIndex);
} }
// TODO: Optional (beta-like): Fill the items up. Let's become INVTW! // Move the item
if (destIndex == -1) { // -1 means: Found none, drop item
// destIndex = -1 -> drop item
if (destIndex == -1) {
leftClick(-999); // Nice one, InvTweaks! leftClick(-999); // Nice one, InvTweaks!
} else { } else {
leftClick(destIndex); leftClick(destIndex);
@@ -196,47 +197,47 @@ public abstract class ContainerManager {
} }
protected void putItemAway(boolean isCraftingGUI) {
// Put current Item away
if (Util.client.thePlayer.inventory.getItemStack() != null) {
putStackToNextEmptySlot(-1, isCraftingGUI, true);
}
}
/** /**
* Returns the first free index in a inventory * Calculates the next fitting or free inventory slot.
* *
* @param isCraftingGUI true, if the craftingGUI is opened * @param stackToMove The ItemType and sized Stack to move
* @return a slot index * @return A super cool inventory slot index! Or -1, if you are to dumb
* to keep your bloody inventory sorted! WHY U NO USE INV TWEAKS?!
*/ */
protected int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) { private int calcInventoryDestination(ItemStack stackToMove) {
if (isCraftingGUI) { // First run: Try to find a nice stack to put items on additionally
for (int i = getInventoryStartIndex(); i < container.inventorySlots.size(); i++) {
for (int i = 10; i < container.inventorySlots.size(); i++) { ItemStack potentialGoalStack = getItemStack(i);
if (getItemStack(i) != null) { if (potentialGoalStack != null && stackToMove != null) {
if (getItemStack(i).isItemEqual(movingItem)) { if (potentialGoalStack.isItemEqual(stackToMove)) {
if (getItemStack(i).stackSize + movingItem.stackSize <= movingItem.getMaxStackSize()) { if (potentialGoalStack.stackSize + stackToMove.stackSize <= stackToMove.getMaxStackSize()) {
return i; return i;
} }
} }
} }
} }
for (int i = 10; i < container.inventorySlots.size(); i++) { // Second run: Find a free slot
for (int i = getInventoryStartIndex(); i < container.inventorySlots.size(); i++) {
if (getItemStack(i) == null) { if (getItemStack(i) == null) {
return i; return i;
} }
} }
System.out.println("getFirstProperSlotIndex(): No Propper / Empty Slot found!");
return -1; // Third run: No slot found. Drop this shit!
} else {
// TODO: Same for inventory
return -1; return -1;
} }
}
/**
* Returns the start index of the user inventory in the current Gui.
*
* @return A slot index
*/
protected abstract int getInventoryStartIndex();
/** /**
* Executes a left mouse click on a slot. [Based on INVTW] * Executes a left mouse click on a slot. [Based on INVTW]
@@ -264,7 +265,7 @@ public abstract class ContainerManager {
*/ */
protected void slotClick(int index, boolean rightClick) { protected void slotClick(int index, boolean rightClick) {
System.out.println("slotClick(): Clicked @ Slot " + index + " with data " + rightClick + "."); Logger.info("slotClick(i,b)", "Clicked @ Slot " + index + " with data " + rightClick + ".");
int rightClickData = (rightClick) ? 1 : 0; int rightClickData = (rightClick) ? 1 : 0;
@@ -2,6 +2,7 @@ package de.skate702.craftingkeys.manager;
import de.skate702.craftingkeys.config.Config; import de.skate702.craftingkeys.config.Config;
import de.skate702.craftingkeys.util.InputUtil; import de.skate702.craftingkeys.util.InputUtil;
import de.skate702.craftingkeys.util.Logger;
import de.skate702.craftingkeys.util.Util; import de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
@@ -32,52 +33,68 @@ public class CraftingManager extends ContainerManager {
if (isDropKeyDown()) { if (isDropKeyDown()) {
for (int i = 1; i < 10; i++) { for (int i = 1; i < 10; i++) {
putStackToNextEmptySlot(i, true, false); moveStackToInventory(i);
} }
} else if (isInteractionKeyDown()) { } else if (isInteractionKeyDown()) {
if (isStackKeyDown()) { if (isStackKeyDown()) {
// TODO: Get for all
System.out.println("Blabla"); int oldStackSize = -1;
clickOnCraftingOutput();
while (Util.client.thePlayer.inventory.getItemStack() != null &&
oldStackSize != Util.client.thePlayer.inventory.getItemStack().stackSize) {
oldStackSize = Util.client.thePlayer.inventory.getItemStack().stackSize;
clickOnCraftingOutput();
}
} else { } else {
clickOnCraftingOutput(); // TODO: Change that shit! clickOnCraftingOutput();
} }
} else if (slotIndex > 0 && currentHoveredSlot != null) { } else if (slotIndex > 0 && currentHoveredSlot != null) {
if (isStackKeyDown()) { if (isStackKeyDown()) {
moveAll(currentHoveredSlot.slotNumber, slotIndex); moveAll(currentHoveredSlot.slotNumber, slotIndex);
moveStackToInventory(-1);
} else { } else {
move(currentHoveredSlot.slotNumber, slotIndex, 1); move(currentHoveredSlot.slotNumber, slotIndex, 1);
} }
} }
} }
} }
@Override @Override
protected int specificKeyDownToSlotIndex() { protected int specificKeyDownToSlotIndex() {
if (Keyboard.isKeyDown(Config.getKeyTopLeft())) { if (Keyboard.isKeyDown(Config.getKeyTopLeft()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(71)) {
return 1; return 1;
} else if (Keyboard.isKeyDown(Config.getkeyTopCenter())) { } else if (Keyboard.isKeyDown(Config.getkeyTopCenter()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(72)) {
return 2; return 2;
} else if (Keyboard.isKeyDown(Config.getKeyTopRight())) { } else if (Keyboard.isKeyDown(Config.getKeyTopRight()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(73)) {
return 3; return 3;
} else if (Keyboard.isKeyDown(Config.getKeyCenterLeft())) { } else if (Keyboard.isKeyDown(Config.getKeyCenterLeft()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(75)) {
return 4; return 4;
} else if (Keyboard.isKeyDown(Config.getKeyCenterCenter())) { } else if (Keyboard.isKeyDown(Config.getKeyCenterCenter()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(76)) {
return 5; return 5;
} else if (Keyboard.isKeyDown(Config.getKeyCenterRight())) { } else if (Keyboard.isKeyDown(Config.getKeyCenterRight()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(77)) {
return 6; return 6;
} else if (Keyboard.isKeyDown(Config.getKeyLowerLeft())) { } else if (Keyboard.isKeyDown(Config.getKeyLowerLeft()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(79)) {
return 7; return 7;
} else if (Keyboard.isKeyDown(Config.getKeyLowerCenter())) { } else if (Keyboard.isKeyDown(Config.getKeyLowerCenter()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(80)) {
return 8; return 8;
} else if (Keyboard.isKeyDown(Config.getKeyLowerRight())) { } else if (Keyboard.isKeyDown(Config.getKeyLowerRight()) ||
Config.isNumPadEnabled() && Keyboard.isKeyDown(81)) {
return 9; return 9;
} else if (Keyboard.isKeyDown(Config.getKeyInteract())) { } else if (Keyboard.isKeyDown(Config.getKeyInteract())) {
return -101; return -101;
@@ -89,17 +106,19 @@ public class CraftingManager extends ContainerManager {
} }
@Override
protected int getInventoryStartIndex() {
return 10;
}
/** /**
* Sends a click on the crafting output (craftingGUI or Inventory) * Sends a click on the crafting output (craftingGUI or Inventory)
*/ */
public void clickOnCraftingOutput() { private void clickOnCraftingOutput() {
//putItemAway(); // TODO!
// Click on crafting output // Click on crafting output
System.out.println("clickOnCraftingOutput(): Clicked on Crafing Output."); Logger.info("clickOnCraftingOutput()", "Clicked on Crafing Output.");
leftClick(0); leftClick(0);
} }
} }
@@ -41,7 +41,7 @@ public class InputUtil {
} }
return null; return null;
} else { } else {
System.out.println("getSlotAtMousePosition(): guiContainer == null"); Logger.debug("getSlotAtMousePosition(gui)", "guiContainer == null");
return null; return null;
} }
} }