Mekanism-tilera-Edition/src/main/java/mekanism/common/transporter/InvStack.java

86 lines
1.6 KiB
Java
Raw Normal View History

2013-11-06 22:04:34 +01:00
package mekanism.common.transporter;
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;
public ArrayList<ItemStack> itemStacks;
public ArrayList<Integer> slotIDs;
public InvStack(IInventory inv)
{
inventory = inv;
itemStacks = new ArrayList<ItemStack>();
slotIDs = new ArrayList<Integer>();
}
public InvStack(IInventory inv, int id, ItemStack stack)
2013-11-06 22:04:34 +01:00
{
inventory = inv;
itemStacks = new ArrayList<ItemStack>();
slotIDs = new ArrayList<Integer>();
appendStack(id, stack);
2013-11-06 22:04:34 +01:00
}
2013-11-06 22:04:34 +01:00
public ItemStack getStack()
{
int size = 0;
2013-11-06 22:04:34 +01:00
for(ItemStack stack : itemStacks)
{
size += stack.stackSize;
}
if(!itemStacks.isEmpty())
2013-11-06 22:04:34 +01:00
{
ItemStack ret = itemStacks.get(0).copy();
2013-11-06 22:04:34 +01:00
ret.stackSize = size;
2013-11-06 22:04:34 +01:00
return ret;
}
2013-11-06 22:04:34 +01:00
return null;
}
public void appendStack(int id, ItemStack stack)
{
slotIDs.add(id);
itemStacks.add(stack);
}
public void use(int amount)
{
for(int i = 0; i < slotIDs.size(); i++)
{
ItemStack stack = itemStacks.get(i);
2013-11-25 22:34:59 +01:00
if(inventory.getStackInSlot(slotIDs.get(i)).stackSize == stack.stackSize && stack.stackSize <= amount)
{
inventory.setInventorySlotContents(slotIDs.get(i), null);
2013-11-25 22:34:59 +01:00
amount -= stack.stackSize;
}
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);
inventory.setInventorySlotContents(slotIDs.get(i), ret);
2013-11-25 22:34:59 +01:00
amount -= ret.stackSize;
}
2013-11-25 22:34:59 +01:00
if(amount == 0)
{
return;
}
}
}
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
}
}