mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
153 lines
3.7 KiB
Java
153 lines
3.7 KiB
Java
package mekanism.common.content.transporter;
|
|
|
|
import mekanism.common.util.InventoryUtils;
|
|
import mekanism.common.util.StackUtils;
|
|
import net.minecraft.inventory.IInventory;
|
|
import net.minecraft.inventory.ISidedInventory;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
public class StackSearcher
|
|
{
|
|
public int i;
|
|
public int[] slots;
|
|
public IInventory theInventory;
|
|
public EnumFacing side;
|
|
|
|
public StackSearcher(IInventory inventory, EnumFacing direction)
|
|
{
|
|
theInventory = InventoryUtils.checkChestInv(inventory);
|
|
side = direction;
|
|
|
|
if(!(theInventory instanceof ISidedInventory))
|
|
{
|
|
i = inventory.getSizeInventory();
|
|
}
|
|
else {
|
|
slots = ((ISidedInventory)theInventory).getSlotsForFace(side.getOpposite());
|
|
|
|
if(slots != null)
|
|
{
|
|
i = slots.length;
|
|
}
|
|
}
|
|
}
|
|
|
|
public InvStack takeTopStack(Finder id)
|
|
{
|
|
if(!(theInventory instanceof ISidedInventory))
|
|
{
|
|
for(i = i - 1; i >= 0; i--)
|
|
{
|
|
if(!theInventory.getStackInSlot(i).isEmpty() && id.modifies(theInventory.getStackInSlot(i)))
|
|
{
|
|
ItemStack toSend = theInventory.getStackInSlot(i).copy();
|
|
return new InvStack(theInventory, i, toSend);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
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)))
|
|
{
|
|
ItemStack toSend = theInventory.getStackInSlot(slotID);
|
|
|
|
if(((ISidedInventory)theInventory).canExtractItem(slotID, toSend, side.getOpposite()))
|
|
{
|
|
return new InvStack(theInventory, slotID, toSend);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public InvStack takeDefinedItem(ItemStack type, int min, int max)
|
|
{
|
|
InvStack ret = new InvStack(theInventory);
|
|
|
|
if(!(theInventory instanceof ISidedInventory))
|
|
{
|
|
for(i = i - 1; i >= 0; i--)
|
|
{
|
|
if(!theInventory.getStackInSlot(i).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(i), type))
|
|
{
|
|
ItemStack stack = theInventory.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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
ISidedInventory sidedInventory = (ISidedInventory)theInventory;
|
|
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
|
|
|
if(slots != null && slots.length != 0)
|
|
{
|
|
for(i = i - 1; i >= 0; i--)
|
|
{
|
|
int slotID = slots[i];
|
|
|
|
if(!sidedInventory.getStackInSlot(slotID).isEmpty() && StackUtils.equalsWildcard(theInventory.getStackInSlot(slotID), type))
|
|
{
|
|
ItemStack stack = sidedInventory.getStackInSlot(slotID);
|
|
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
|
|
|
|
if(current+stack.getCount() <= max)
|
|
{
|
|
ItemStack copy = stack.copy();
|
|
|
|
if(sidedInventory.canExtractItem(slotID, copy, side.getOpposite()))
|
|
{
|
|
ret.appendStack(slotID, copy);
|
|
}
|
|
}
|
|
else {
|
|
ItemStack copy = stack.copy();
|
|
|
|
if(sidedInventory.canExtractItem(slotID, copy, side.getOpposite()))
|
|
{
|
|
copy.setCount(max-current);
|
|
ret.appendStack(slotID, copy);
|
|
}
|
|
}
|
|
|
|
if(!ret.getStack().isEmpty() && ret.getStack().getCount() == max)
|
|
{
|
|
return ret;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(ret != null && !ret.getStack().isEmpty() && ret.getStack().getCount() >= min)
|
|
{
|
|
return ret;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|