Even more refactoring!

This commit is contained in:
0xSeb
2015-10-25 22:17:42 +01:00
parent 265d94a662
commit bada3c5910
6 changed files with 206 additions and 213 deletions
@@ -13,12 +13,10 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent;
import de.skate702.craftingkeys.config.Config; 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.proxies.CraftingKeysProxy; 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 de.skate702.craftingkeys.util.Util;
import net.minecraft.client.gui.inventory.GuiCrafting; 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 * The Main Class of the Mod with the important onTick-Method. Some Methods are
@@ -69,9 +67,6 @@ public class CraftingKeys {
proxy.registerRenderers(); proxy.registerRenderers();
FMLCommonHandler.instance().bus().register(this); FMLCommonHandler.instance().bus().register(this);
// TODO: Helper!
Helper.debugPrint("load(): Loaded CraftingKeys successful");
} }
/** /**
@@ -102,74 +97,20 @@ public class CraftingKeys {
Util.printWarning(); Util.printWarning();
} }
// Case 1: Classic GUI Screen
if (Util.isCraftingGUI(Util.client.currentScreen)) { if (Util.isCraftingGUI(Util.client.currentScreen)) {
GuiCrafting guiCrafting = (GuiCrafting) Util.client.currentScreen; ContainerManager con = new CraftingManager(
Slot currentHoveredSlot = InputUtil.getSlotAtMousePosition(guiCrafting); ((GuiCrafting) Util.client.currentScreen).inventorySlots);
int keyDown = Helper.craftingKeyDownToSlotNumber(); con.acceptKey();
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots); } else if (Util.isInventoryGUI(Util.client.currentScreen)) {
// do do do
// Block Key Interval (avoid multiple Runs) System.out.println("Inventory");
if (!InputUtil.isSameKey(keyDown)) { } else if (Util.isVillagerGUI(Util.client.currentScreen)) {
// do do do
// Moving item to crafting table //((GuiMerchant) Util.client.currentScreen).inventorySlots
if (keyDown > 0 && currentHoveredSlot != null) { System.out.println("Villager");
// 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);
}
}
} }
// Case 2: Inventory (2x2 Crafting, Quick-Armor)
// TODO: Case 2
} }
} }
@@ -1,28 +1,67 @@
package de.skate702.craftingkeys.manager; package de.skate702.craftingkeys.manager;
import de.skate702.craftingkeys.CraftingKeys; import de.skate702.craftingkeys.CraftingKeys;
import de.skate702.craftingkeys.util.Helper; import de.skate702.craftingkeys.config.Config;
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;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
/** public abstract class ContainerManager {
* Managing Class to move Items in Inventory Containers.
*
* @author skate702
*/
public class ContainerManager {
/** /**
* The Container to work with. * 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) { public ContainerManager(Container container) {
this.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); ItemStack source = getItemStack(srcIndex);
if (source == null) { if (source == null) {
Helper.debugPrint("moveAll(): source == null"); System.out.println("moveAll(): source == null");
} else { } else {
Helper.debugPrint("moveAll(): Redirected to move()"); System.out.println("moveAll(): Redirected to move()");
move(srcIndex, destIndex, source.stackSize); move(srcIndex, destIndex, source.stackSize);
} }
@@ -59,8 +98,11 @@ public class ContainerManager {
ItemStack destination = getItemStack(destIndex); ItemStack destination = getItemStack(destIndex);
// Same Location? // Same Location?
if (source == null || srcIndex == destIndex) { if (source == null) {
Helper.debugPrint("Move(): srcIndex == destIndex OR 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; return;
} }
@@ -81,12 +123,11 @@ public class ContainerManager {
leftClick(srcIndex); leftClick(srcIndex);
} }
Helper.debugPrint("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!"); System.out.println("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!");
} else { } 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 { } else {
Helper.debugPrint("getItemStack(): Invalid index"); System.out.println("getItemStack(): Invalid index");
return null; 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 * Takes all items from a slot and moves them to the next empty slot or
* drops them. * drops them.
@@ -139,7 +159,7 @@ public class ContainerManager {
* @param sourceIndex The index of the slot to move items from * @param sourceIndex The index of the slot to move items from
* @param isCraftingGUI true, if the craftingGUI is opened * @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 // Check for Item-Type
ItemStack stackToMove; ItemStack stackToMove;
@@ -153,7 +173,7 @@ public class ContainerManager {
// Test for empty crafting table slot // Test for empty crafting table slot
if (!isHeld && getItemStack(sourceIndex) == null) { if (!isHeld && getItemStack(sourceIndex) == null) {
Helper.debugPrint("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + "."); System.out.println("putStackToNextEmptySlot(): No Item Stack @ " + sourceIndex + ".");
return; return;
} }
@@ -176,7 +196,7 @@ public class ContainerManager {
} }
private void putItemAway(boolean isCraftingGUI) { protected void putItemAway(boolean isCraftingGUI) {
// Put current Item away // Put current Item away
if (Util.client.thePlayer.inventory.getItemStack() != null) { if (Util.client.thePlayer.inventory.getItemStack() != null) {
putStackToNextEmptySlot(-1, isCraftingGUI, true); putStackToNextEmptySlot(-1, isCraftingGUI, true);
@@ -189,7 +209,7 @@ public class ContainerManager {
* @param isCraftingGUI true, if the craftingGUI is opened * @param isCraftingGUI true, if the craftingGUI is opened
* @return a slot index * @return a slot index
*/ */
private int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) { protected int getFirstPropperSlotIndex(boolean isCraftingGUI, ItemStack movingItem) {
if (isCraftingGUI) { if (isCraftingGUI) {
@@ -210,7 +230,7 @@ public class ContainerManager {
return i; return i;
} }
} }
Helper.debugPrint("getFirstProperSlotIndex(): No Propper / Empty Slot found!"); System.out.println("getFirstProperSlotIndex(): No Propper / Empty Slot found!");
return -1; return -1;
} else { } else {
// TODO: Same for inventory // TODO: Same for inventory
@@ -223,7 +243,7 @@ public class ContainerManager {
* *
* @param index The index of the slot in the container * @param index The index of the slot in the container
*/ */
private void leftClick(int index) { protected void leftClick(int index) {
slotClick(index, false); slotClick(index, false);
} }
@@ -232,7 +252,7 @@ public class ContainerManager {
* *
* @param index The index of the slot in the container * @param index The index of the slot in the container
*/ */
private void rightClick(int index) { protected void rightClick(int index) {
slotClick(index, true); slotClick(index, true);
} }
@@ -242,13 +262,13 @@ public class ContainerManager {
* @param index The index of the slot in the container * @param index The index of the slot in the container
* @param rightClick True, if the click is with the right mouse button * @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; 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); rightClickData, 0, Util.client.thePlayer);
} }
@@ -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);
}
}
@@ -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;
}
}
@@ -41,7 +41,7 @@ public class InputUtil {
} }
return null; return null;
} else { } else {
Helper.debugPrint("getSlotAtMousePosition(): guiContainer == null"); System.out.println("getSlotAtMousePosition(): guiContainer == null");
return null; return null;
} }
} }
@@ -4,6 +4,7 @@ import cpw.mods.fml.client.FMLClientHandler;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.ChatComponentTranslation;
/** /**
@@ -66,7 +67,6 @@ public class Util {
* @return True, if GuiScreen; False if null * @return True, if GuiScreen; False if null
*/ */
public static boolean isCraftingGUI(GuiScreen screen) { public static boolean isCraftingGUI(GuiScreen screen) {
if (screen != null) { if (screen != null) {
if (screen instanceof GuiCrafting) { if (screen instanceof GuiCrafting) {
return true; return true;
@@ -74,4 +74,23 @@ public class Util {
} }
return false; 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;
}
} }