2013-11-06 22:04:34 +01:00
|
|
|
package mekanism.common.transporter;
|
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
public final class InvStack
|
|
|
|
{
|
|
|
|
public IInventory inventory;
|
2013-11-06 23:19:02 +01:00
|
|
|
public ArrayList<ItemStack> itemStacks;
|
|
|
|
public ArrayList<Integer> slotIDs;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
public InvStack(IInventory inv)
|
|
|
|
{
|
|
|
|
inventory = inv;
|
|
|
|
itemStacks = new ArrayList<ItemStack>();
|
|
|
|
slotIDs = new ArrayList<Integer>();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
public InvStack(IInventory inv, int id, ItemStack stack)
|
2013-11-06 22:04:34 +01:00
|
|
|
{
|
|
|
|
inventory = inv;
|
2013-11-06 23:19:02 +01:00
|
|
|
itemStacks = new ArrayList<ItemStack>();
|
|
|
|
slotIDs = new ArrayList<Integer>();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
appendStack(id, stack);
|
2013-11-06 22:04:34 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
public ItemStack getStack()
|
|
|
|
{
|
|
|
|
int size = 0;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
for(ItemStack stack : itemStacks)
|
|
|
|
{
|
|
|
|
size += stack.stackSize;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
if(!itemStacks.isEmpty())
|
2013-11-06 22:04:34 +01:00
|
|
|
{
|
2013-11-06 23:19:02 +01:00
|
|
|
ItemStack ret = itemStacks.get(0).copy();
|
2013-11-06 22:04:34 +01:00
|
|
|
ret.stackSize = size;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 23:19:02 +01:00
|
|
|
public void appendStack(int id, ItemStack stack)
|
|
|
|
{
|
|
|
|
slotIDs.add(id);
|
|
|
|
itemStacks.add(stack);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 19:33:42 +01:00
|
|
|
public void use(int amount)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < slotIDs.size(); i++)
|
|
|
|
{
|
|
|
|
ItemStack stack = itemStacks.get(i);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 22:34:59 +01:00
|
|
|
if(inventory.getStackInSlot(slotIDs.get(i)).stackSize == stack.stackSize && stack.stackSize <= amount)
|
2013-11-25 19:33:42 +01:00
|
|
|
{
|
|
|
|
inventory.setInventorySlotContents(slotIDs.get(i), null);
|
2013-11-25 22:34:59 +01:00
|
|
|
amount -= stack.stackSize;
|
2013-11-25 19:33:42 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ItemStack ret = stack.copy();
|
2013-11-25 22:34:59 +01:00
|
|
|
ret.stackSize = inventory.getStackInSlot(slotIDs.get(i)).stackSize - Math.min(stack.stackSize, amount);
|
2013-11-25 19:33:42 +01:00
|
|
|
inventory.setInventorySlotContents(slotIDs.get(i), ret);
|
2013-11-25 22:34:59 +01:00
|
|
|
amount -= ret.stackSize;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-25 22:34:59 +01:00
|
|
|
if(amount == 0)
|
|
|
|
{
|
|
|
|
return;
|
2013-11-25 19:33:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-06 22:04:34 +01:00
|
|
|
public void use()
|
|
|
|
{
|
2013-11-25 22:34:59 +01:00
|
|
|
use(getStack().stackSize);
|
2013-11-06 22:04:34 +01:00
|
|
|
}
|
|
|
|
}
|