Make the logistical sorter skip unplaceable stacks

Java could really do with generators/yield semantics.
This commit is contained in:
Ben Spiers 2015-07-19 01:16:46 +01:00
parent d994f18117
commit d5f5565694
7 changed files with 192 additions and 17 deletions

View file

@ -0,0 +1,155 @@
package mekanism.common.content.transporter;
import mekanism.api.util.StackUtils;
import mekanism.common.util.InventoryUtils;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
public class StackSearcher
{
public int i;
public int[] slots;
public IInventory theInventory;
public ForgeDirection side;
public StackSearcher(IInventory inventory, ForgeDirection direction)
{
theInventory = InventoryUtils.checkChestInv(inventory);
if(!(theInventory instanceof ISidedInventory))
{
i = inventory.getSizeInventory();
}
else
{
slots = ((ISidedInventory)theInventory).getAccessibleSlotsFromSide(side.getOpposite().ordinal());
if(slots != null)
{
i = slots.length;
}
}
side = direction;
}
public InvStack takeTopStack(Finder id)
{
if(!(theInventory instanceof ISidedInventory))
{
for(i = i - 1; i >= 0; i--)
{
if(theInventory.getStackInSlot(i) != null && 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) != null && id.modifies(theInventory.getStackInSlot(slotID)))
{
ItemStack toSend = theInventory.getStackInSlot(slotID);
if(((ISidedInventory)theInventory).canExtractItem(slotID, toSend, side.getOpposite().ordinal()))
{
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) != null && StackUtils.equalsWildcard(theInventory.getStackInSlot(i), type))
{
ItemStack stack = theInventory.getStackInSlot(i);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
if(current+stack.stackSize <= max)
{
ret.appendStack(i, stack.copy());
}
else {
ItemStack copy = stack.copy();
copy.stackSize = max-current;
ret.appendStack(i, copy);
}
if(ret.getStack() != null && ret.getStack().stackSize == max)
{
return ret;
}
}
}
}
else {
ISidedInventory sidedInventory = (ISidedInventory)theInventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side.getOpposite().ordinal());
if(slots != null && slots.length != 0)
{
for(i = i - 1; i >= 0; i--)
{
int slotID = slots[i];
if(sidedInventory.getStackInSlot(slotID) != null && StackUtils.equalsWildcard(theInventory.getStackInSlot(slotID), type))
{
ItemStack stack = sidedInventory.getStackInSlot(slotID);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
if(current+stack.stackSize <= max)
{
ItemStack copy = stack.copy();
if(sidedInventory.canExtractItem(slotID, copy, side.getOpposite().ordinal()))
{
ret.appendStack(slotID, copy);
}
}
else {
ItemStack copy = stack.copy();
if(sidedInventory.canExtractItem(slotID, copy, side.getOpposite().ordinal()))
{
copy.stackSize = max-current;
ret.appendStack(slotID, copy);
}
}
if(ret.getStack() != null && ret.getStack().stackSize == max)
{
return ret;
}
}
}
}
}
if(ret != null && ret.getStack() != null && ret.getStack().stackSize >= min)
{
return ret;
}
return null;
}
}

View file

@ -40,17 +40,22 @@ public class TItemStackFilter extends TransporterFilter
}
@Override
public InvStack getStackFromInventory(IInventory inv, ForgeDirection side)
public InvStack getStackFromInventory(StackSearcher searcher)
{
if(sizeMode)
{
return InventoryUtils.takeDefinedItem(inv, side.ordinal(), itemType, min, max);
return searcher.takeDefinedItem(itemType, min, max);
}
else {
return InventoryUtils.takeTopStack(inv, side.ordinal(), new ItemStackFinder(itemType));
return super.getStackFromInventory(searcher);
}
}
public Finder getFinder()
{
return new ItemStackFinder(itemType);
}
@Override
public void write(NBTTagCompound nbtTags)
{

View file

@ -38,9 +38,9 @@ public class TMaterialFilter extends TransporterFilter
}
@Override
public InvStack getStackFromInventory(IInventory inv, ForgeDirection side)
public Finder getFinder()
{
return InventoryUtils.takeTopStack(inv, side.ordinal(), new MaterialFinder(getMaterial()));
return new MaterialFinder(getMaterial());
}
@Override

View file

@ -29,9 +29,9 @@ public class TModIDFilter extends TransporterFilter
}
@Override
public InvStack getStackFromInventory(IInventory inv, ForgeDirection side)
public Finder getFinder()
{
return InventoryUtils.takeTopStack(inv, side.ordinal(), new ModIDFinder(modID));
return new ModIDFinder(modID);
}
@Override

View file

@ -29,9 +29,9 @@ public class TOreDictFilter extends TransporterFilter
}
@Override
public InvStack getStackFromInventory(IInventory inv, ForgeDirection side)
public Finder getFinder()
{
return InventoryUtils.takeTopStack(inv, side.ordinal(), new OreDictFinder(oreDictName));
return new OreDictFinder(oreDictName);
}
@Override

View file

@ -7,6 +7,7 @@ import java.util.Arrays;
import java.util.List;
import mekanism.api.EnumColor;
import mekanism.common.util.InventoryUtils;
import mekanism.common.util.TransporterUtils;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
@ -23,7 +24,12 @@ public abstract class TransporterFilter
public abstract boolean canFilter(ItemStack itemStack);
public abstract InvStack getStackFromInventory(IInventory inv, ForgeDirection side);
public abstract Finder getFinder();
public InvStack getStackFromInventory(StackSearcher searcher)
{
return searcher.takeTopStack(getFinder());
}
public void write(NBTTagCompound nbtTags)
{

View file

@ -19,6 +19,7 @@ import mekanism.common.base.ITransporterTile;
import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.content.transporter.Finder.FirstFinder;
import mekanism.common.content.transporter.InvStack;
import mekanism.common.content.transporter.StackSearcher;
import mekanism.common.content.transporter.TItemStackFilter;
import mekanism.common.content.transporter.TransporterFilter;
import mekanism.common.content.transporter.TransporterManager;
@ -93,12 +94,19 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen
boolean sentItems = false;
int min = 0;
outer:
for(TransporterFilter filter : filters)
{
InvStack invStack = filter.getStackFromInventory(inventory, ForgeDirection.getOrientation(facing).getOpposite());
if(invStack != null && invStack.getStack() != null)
inner:
for(StackSearcher search = new StackSearcher(inventory, ForgeDirection.getOrientation(facing)); search.i >= 0;)
{
InvStack invStack = filter.getStackFromInventory(search);
if(invStack == null || invStack.getStack() == null)
{
break inner;
}
if(filter.canFilter(invStack.getStack()))
{
if(filter instanceof TItemStackFilter)
@ -120,9 +128,10 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen
setActive(true);
sentItems = true;
break;
break outer;
}
}
}
}