Completely refactor inventory management to allow IItemHandler support. Likely many bugs to fix, but it's a start

This commit is contained in:
aidancbrady committed 2017-04-19 16:16:09 -04:00
1 parent 840de86d11
commit b97b7184c1
15 files changed
+813 -525

No files matched your search

@@ -5,81 +5,110 @@ import mekanism.common.util.StackUtils;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.IItemHandler;
public class StackSearcher
{
public int i;
public int i = -1;
public int[] slots;
public IInventory theInventory;
public TileEntity tileEntity;
public EnumFacing side;
public StackSearcher(IInventory inventory, EnumFacing direction)
public StackSearcher(TileEntity tile, EnumFacing direction)
{
theInventory = InventoryUtils.checkChestInv(inventory);
tileEntity = tile;
side = direction;
if(!(theInventory instanceof ISidedInventory))
if(InventoryUtils.isItemHandler(tile, direction.getOpposite()))
{
i = inventory.getSizeInventory();
}
else {
slots = ((ISidedInventory)theInventory).getSlotsForFace(side.getOpposite());
i = InventoryUtils.getItemHandler(tile, direction.getOpposite()).getSlots();
}
else if(tile instanceof ISidedInventory)
{
slots = ((ISidedInventory)tile).getSlotsForFace(side.getOpposite());
if(slots != null)
{
i = slots.length;
}
}
else if(tile instanceof IInventory)
{
i = ((IInventory)tile).getSizeInventory();
}
}
public InvStack takeTopStack(Finder id)
{
if(!(theInventory instanceof ISidedInventory))
if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite()))
{
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
for(i = i - 1; i >= 0; i--)
{
if(!theInventory.getStackInSlot(i).isEmpty() && id.modifies(theInventory.getStackInSlot(i)))
ItemStack stack = inventory.extractItem(i, 64, true);
if(!stack.isEmpty() && id.modifies(stack))
{
ItemStack toSend = theInventory.getStackInSlot(i).copy();
return new InvStack(theInventory, i, toSend);
return new InvStack(tileEntity, i, stack, side.getOpposite());
}
}
}
else {
else if(tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory)tileEntity;
if(slots != null && slots.length != 0)
{
for(i = i - 1; i >= 0; i--)
{
int slotID = slots[i];
if(!theInventory.getStackInSlot(slotID).isEmpty() && id.modifies(theInventory.getStackInSlot(slotID)))
if(!inventory.getStackInSlot(slotID).isEmpty() && id.modifies(inventory.getStackInSlot(slotID)))
{
ItemStack toSend = theInventory.getStackInSlot(slotID);
ItemStack toSend = inventory.getStackInSlot(slotID);
if(((ISidedInventory)theInventory).canExtractItem(slotID, toSend, side.getOpposite()))
if(((ISidedInventory)inventory).canExtractItem(slotID, toSend, side.getOpposite()))
{
return new InvStack(theInventory, slotID, toSend);
return new InvStack(tileEntity, slotID, toSend, side.getOpposite());
}
}
}
}
}
else if(tileEntity instanceof IInventory)
{
IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity);
for(i = i - 1; i >= 0; i--)
{
if(!inventory.getStackInSlot(i).isEmpty() && id.modifies(inventory.getStackInSlot(i)))
{
ItemStack toSend = inventory.getStackInSlot(i).copy();
return new InvStack(tileEntity, i, toSend, side.getOpposite());
}
}
}
return null;
}
public InvStack takeDefinedItem(ItemStack type, int min, int max)
{
InvStack ret = new InvStack(theInventory);
InvStack ret = new InvStack(tileEntity, side.getOpposite());
if(!(theInventory instanceof ISidedInventory))
if(InventoryUtils.isItemHandler(tileEntity, side.getOpposite()))
{
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
for(i = i - 1; i >= 0; i--)
{
if(!theInventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(i), type))
ItemStack stack = inventory.extractItem(i, max, true);
if(!stack.isEmpty() && StackUtils.equalsWildcard(stack, type))
{
ItemStack stack = theInventory.getStackInSlot(i);
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
if(current+stack.getCount() <= max)
@@ -99,8 +128,9 @@ public class StackSearcher
}
}
}
else {
ISidedInventory sidedInventory = (ISidedInventory)theInventory;
else if(tileEntity instanceof ISidedInventory)
{
ISidedInventory sidedInventory = (ISidedInventory)tileEntity;
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
if(slots != null && slots.length != 0)
@@ -109,7 +139,7 @@ public class StackSearcher
{
int slotID = slots[i];
if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(slotID), type))
if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(sidedInventory.getStackInSlot(slotID), type))
{
ItemStack stack = sidedInventory.getStackInSlot(slotID);
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
@@ -141,6 +171,34 @@ public class StackSearcher
}
}
}
else if(tileEntity instanceof IInventory)
{
IInventory inventory = InventoryUtils.checkChestInv((IInventory)tileEntity);
for(i = i - 1; i >= 0; i--)
{
if(!inventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type))
{
ItemStack stack = inventory.getStackInSlot(i);
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
if(current+stack.getCount() <= max)
{
ret.appendStack(i, stack.copy());
}
else {
ItemStack copy = stack.copy();
copy.setCount(max-current);
ret.appendStack(i, copy);
}
if(!ret.getStack().isEmpty() && ret.getStack().getCount() == max)
{
return ret;
}
}
}
}
if(ret != null && !ret.getStack().isEmpty() && ret.getStack().getCount() >= min)
{