Added moving back from crafting table feature, some fixes, chat message,

dropping. Nice things!
This commit is contained in:
0xSeb
2015-02-25 00:59:28 +01:00
parent 629d824efe
commit 75e5dfe1f2
2 changed files with 113 additions and 12 deletions
@@ -4,6 +4,8 @@ import net.minecraft.client.gui.inventory.GuiCrafting;
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 net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
/** /**
* Managing Class to move Items in Inventory Containers. * Managing Class to move Items in Inventory Containers.
@@ -58,9 +60,6 @@ public class ContainerManager {
*/ */
public void move(int srcIndex, int destIndex, int amount) { 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 // Stacks
ItemStack source = getItemStack(srcIndex); ItemStack source = getItemStack(srcIndex);
ItemStack destination = getItemStack(destIndex); ItemStack destination = getItemStack(destIndex);
@@ -127,7 +126,7 @@ public class ContainerManager {
*/ */
public void clickOnCraftingOutput(boolean isCraftingGUI) { public void clickOnCraftingOutput(boolean isCraftingGUI) {
// TODO: Put current Item away putItemAway(isCraftingGUI);
if (isCraftingGUI) { if (isCraftingGUI) {
@@ -135,6 +134,8 @@ public class ContainerManager {
Helper.debugPrint("clickOnCraftingOutput(): Clicked on Crafing Output."); Helper.debugPrint("clickOnCraftingOutput(): Clicked on Crafing Output.");
leftClick(0); 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 * Takes all items from a slot and moves them to the next empty slot or
* drops them. * drops them.
* *
* @param index * @param sourceIndex
* The index of the slot to move items from * 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;
}
} }
/** /**
@@ -4,6 +4,7 @@ import org.lwjgl.input.Keyboard;
import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import net.minecraft.util.ChatComponentText;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.EventHandler;
@@ -75,6 +76,11 @@ public class CraftingKeys {
public void postInit(FMLPostInitializationEvent event) { 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. * This method will be executed every Ingame Tick.
* *
@@ -83,6 +89,19 @@ public class CraftingKeys {
@SubscribeEvent @SubscribeEvent
public void onTick(TickEvent.ClientTickEvent tick) { 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 // Case 1: Classic GUI Screen
if (Helper.isCraftingGUI(Helper.client.currentScreen)) { if (Helper.isCraftingGUI(Helper.client.currentScreen)) {
@@ -90,11 +109,11 @@ public class CraftingKeys {
Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting); Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting);
int keyDown = Helper.craftingKeyDownToSlotNumber(); int keyDown = Helper.craftingKeyDownToSlotNumber();
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots);
// Block Key Interval (avoid multiple Runs) // Block Key Interval (avoid multiple Runs)
if (!Helper.isSameKey(keyDown)) { if (!Helper.isSameKey(keyDown)) {
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots);
// Moving item to crafting table // Moving item to crafting table
if (keyDown > 0 && currentHoveredSlot != null) { if (keyDown > 0 && currentHoveredSlot != null) {
@@ -109,9 +128,14 @@ public class CraftingKeys {
if (keyDown == -2) { if (keyDown == -2) {
// TODO: Space = Move all back // Space = Move all back
Helper.debugPrint("onTick(): [TODO] Move all items back or drop them."); 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 // Strg = Take the output
@@ -119,7 +143,7 @@ public class CraftingKeys {
if (guiCrafting.isShiftKeyDown()) { if (guiCrafting.isShiftKeyDown()) {
//Strg + Shift = Move all (resp. faster!) // Strg + Shift = Move all (resp. faster!)
con.clickOnCraftingOutput(true); con.clickOnCraftingOutput(true);
} else { } else {