From 4f773bbbf94736710ecd76c728fd711b0e1f147b Mon Sep 17 00:00:00 2001 From: "Aidan C. Brady" Date: Tue, 22 Oct 2013 22:29:25 -0400 Subject: [PATCH] Began adding inventory algorithms. --- .../common/util/TransporterUtils.java | 137 +++++++++++++++++- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/common/mekanism/common/util/TransporterUtils.java b/common/mekanism/common/util/TransporterUtils.java index d78f070fe..db52c0087 100644 --- a/common/mekanism/common/util/TransporterUtils.java +++ b/common/mekanism/common/util/TransporterUtils.java @@ -128,8 +128,137 @@ public final class TransporterUtils return true; } - public static ItemStack putInInventory(IInventory inventory, ItemStack itemStack) - { - return null;//TODO - } + public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, int side) + { + if (!(inventory instanceof ISidedInventory)) { + for (int i = 0; i <= inventory.getSizeInventory() - 1; i++) + { + if (inventory.isItemValidForSlot(i, itemStack)) { + ItemStack inSlot = inventory.getStackInSlot(i); + + if (inSlot == null) { + inventory.setInventorySlotContents(i, itemStack); + return null; + } + else if (inSlot.isItemEqual(itemStack) + && inSlot.stackSize < inSlot.getMaxStackSize()) + { + if (inSlot.stackSize + itemStack.stackSize <= inSlot + .getMaxStackSize()) { + ItemStack toSet = itemStack.copy(); + toSet.stackSize += inSlot.stackSize; + + inventory.setInventorySlotContents(i, toSet); + return null; + } + else { + int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize(); + + ItemStack toSet = itemStack.copy(); + toSet.stackSize = inSlot.getMaxStackSize(); + + ItemStack remains = itemStack.copy(); + remains.stackSize = rejects; + + inventory.setInventorySlotContents(i, toSet); + return remains; + } + } + } + } + } + else { + ISidedInventory sidedInventory = (ISidedInventory) inventory; + int[] slots = sidedInventory.getAccessibleSlotsFromSide(side); + + for (int get = 0; get <= slots.length - 1; get++) + { + int slotID = slots[get]; + + if (sidedInventory.isItemValidForSlot(slotID, itemStack) + && sidedInventory + .canInsertItem(slotID, itemStack, side)) + { + ItemStack inSlot = inventory.getStackInSlot(slotID); + + if (inSlot == null) { + inventory.setInventorySlotContents(slotID, itemStack); + return null; + } + else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize()) + { + if (inSlot.stackSize + itemStack.stackSize <= inSlot + .getMaxStackSize()) { + ItemStack toSet = itemStack.copy(); + toSet.stackSize += inSlot.stackSize; + + inventory.setInventorySlotContents(slotID, toSet); + return null; + } + else { + int rejects = (inSlot.stackSize + itemStack.stackSize) + - inSlot.getMaxStackSize(); + + ItemStack toSet = itemStack.copy(); + toSet.stackSize = inSlot.getMaxStackSize(); + + ItemStack remains = itemStack.copy(); + remains.stackSize = rejects; + + inventory.setInventorySlotContents(slotID, toSet); + return remains; + } + } + } + } + } + + return itemStack; + } + + public static ItemStack takeTopItemFromInventory(IInventory inventory, int side) + { + if (!(inventory instanceof ISidedInventory)) + { + for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) + { + if (inventory.getStackInSlot(i) != null) { + ItemStack toSend = inventory.getStackInSlot(i).copy(); + toSend.stackSize = 1; + + inventory.decrStackSize(i, 1); + + return toSend; + } + } + } + else { + ISidedInventory sidedInventory = (ISidedInventory) inventory; + int[] slots = sidedInventory.getAccessibleSlotsFromSide(side); + + if (slots != null) + { + for (int get = slots.length - 1; get >= 0; get--) + { + int slotID = slots[get]; + + if (sidedInventory.getStackInSlot(slotID) != null) + { + ItemStack toSend = sidedInventory + .getStackInSlot(slotID); + toSend.stackSize = 1; + + if (sidedInventory.canExtractItem(slotID, toSend, side)) + { + sidedInventory.decrStackSize(slotID, 1); + + return toSend; + } + } + } + } + } + + return null; + } } \ No newline at end of file