Intelligent inventory parsing to strategically find and grab OreDict-relative entries

This commit is contained in:
Aidan C. Brady 2013-12-25 19:18:07 -05:00
parent d0d4a858e1
commit 934f136d4f
8 changed files with 109 additions and 89 deletions

View file

@ -1,10 +1,8 @@
package mekanism.common.miner;
import java.util.ArrayList;
import java.util.List;
import mekanism.common.util.MekanismUtils;
import net.minecraft.block.Block;
import mekanism.common.transporter.Finder.OreDictFinder;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -23,48 +21,7 @@ public class MOreDictFilter extends MinerFilter
return false;
}
if(oreDictName.equals("*") && itemStack.itemID != Block.bedrock.blockID)
{
return true;
}
List<String> oreKeys = MekanismUtils.getOreDictName(itemStack);
if(oreKeys.isEmpty())
{
return false;
}
for(String oreKey : oreKeys)
{
if(oreDictName.equals(oreKey))
{
return true;
}
else if(oreDictName.endsWith("*") && !oreDictName.startsWith("*"))
{
if(oreKey.startsWith(oreDictName.substring(0, oreDictName.length()-1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && !oreDictName.endsWith("*"))
{
if(oreKey.endsWith(oreDictName.substring(1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && oreDictName.endsWith("*"))
{
if(oreKey.contains(oreDictName.substring(1, oreDictName.length()-1)))
{
return true;
}
}
}
return false;
return new OreDictFinder(oreDictName).modifies(itemStack);
}
@Override

View file

@ -15,6 +15,7 @@ import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketDataRequest;
import mekanism.common.network.PacketTileEntity;
import mekanism.common.tileentity.TileEntityLogisticalSorter;
import mekanism.common.transporter.Finder.FreeFinder;
import mekanism.common.transporter.InvStack;
import mekanism.common.transporter.TransporterManager;
import mekanism.common.transporter.TransporterStack;
@ -362,7 +363,7 @@ public class PartLogisticalTransporter extends PartSidedPipe implements ILogisti
if(tile instanceof IInventory)
{
IInventory inv = (IInventory)tile;
InvStack stack = InventoryUtils.takeTopStack(inv, side.ordinal());
InvStack stack = InventoryUtils.takeTopStack(inv, side.ordinal(), new FreeFinder());
if(stack != null && stack.getStack() != null)
{

View file

@ -3,8 +3,8 @@ package mekanism.common.tileentity;
import java.util.ArrayList;
import java.util.EnumSet;
import mekanism.api.EnumColor;
import mekanism.api.Coord4D;
import mekanism.api.EnumColor;
import mekanism.common.HashList;
import mekanism.common.IActiveState;
import mekanism.common.ILogisticalTransporter;
@ -13,6 +13,7 @@ import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission;
import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.network.PacketTileEntity;
import mekanism.common.transporter.Finder.FreeFinder;
import mekanism.common.transporter.InvStack;
import mekanism.common.transporter.TItemStackFilter;
import mekanism.common.transporter.TransporterFilter;
@ -118,7 +119,7 @@ public class TileEntityLogisticalSorter extends TileEntityElectricBlock implemen
if(!hasFilter && autoEject)
{
inInventory = InventoryUtils.takeTopStack(inventory, ForgeDirection.getOrientation(facing).getOpposite().ordinal());
inInventory = InventoryUtils.takeTopStack(inventory, ForgeDirection.getOrientation(facing).getOpposite().ordinal(), new FreeFinder());
}
if(inInventory != null && inInventory.getStack() != null)

View file

@ -0,0 +1,89 @@
package mekanism.common.transporter;
import java.util.List;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.StackUtils;
import net.minecraft.item.ItemStack;
public abstract class Finder
{
public abstract boolean modifies(ItemStack stack);
public static class FreeFinder extends Finder
{
@Override
public boolean modifies(ItemStack stack)
{
return true;
}
}
public static class OreDictFinder extends Finder
{
public String oreDictName;
public OreDictFinder(String name)
{
oreDictName = name;
}
@Override
public boolean modifies(ItemStack stack)
{
List<String> oreKeys = MekanismUtils.getOreDictName(stack);
if(oreKeys.isEmpty())
{
return false;
}
for(String oreKey : oreKeys)
{
if(oreDictName.equals(oreKey) || oreDictName.equals("*"))
{
return true;
}
else if(oreDictName.endsWith("*") && !oreDictName.startsWith("*"))
{
if(oreKey.startsWith(oreDictName.substring(0, oreDictName.length()-1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && !oreDictName.endsWith("*"))
{
if(oreKey.endsWith(oreDictName.substring(1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && oreDictName.endsWith("*"))
{
if(oreKey.contains(oreDictName.substring(1, oreDictName.length()-1)))
{
return true;
}
}
}
return false;
}
}
public static class ItemStackFinder extends Finder
{
public ItemStack itemType;
public ItemStackFinder(ItemStack type)
{
itemType = type;
}
@Override
public boolean modifies(ItemStack stack)
{
return StackUtils.equalsWildcard(itemType, stack);
}
}
}

View file

@ -2,6 +2,7 @@ package mekanism.common.transporter;
import java.util.ArrayList;
import mekanism.common.transporter.Finder.ItemStackFinder;
import mekanism.common.util.InventoryUtils;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
@ -43,7 +44,7 @@ public class TItemStackFilter extends TransporterFilter
return InventoryUtils.takeDefinedItem(inv, side.ordinal(), itemType, min, max);
}
else {
return InventoryUtils.takeTopStack(inv, side.ordinal());
return InventoryUtils.takeTopStack(inv, side.ordinal(), new ItemStackFinder(itemType));
}
}

View file

@ -3,6 +3,7 @@ package mekanism.common.transporter;
import java.util.ArrayList;
import java.util.List;
import mekanism.common.transporter.Finder.OreDictFinder;
import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils;
import net.minecraft.inventory.IInventory;
@ -19,49 +20,18 @@ public class TOreDictFilter extends TransporterFilter
@Override
public boolean canFilter(ItemStack itemStack)
{
List<String> oreKeys = MekanismUtils.getOreDictName(itemStack);
if(oreKeys.isEmpty())
if(itemStack == null)
{
return false;
}
for(String oreKey : oreKeys)
{
if(oreDictName.equals(oreKey) || oreDictName.equals("*"))
{
return true;
}
else if(oreDictName.endsWith("*") && !oreDictName.startsWith("*"))
{
if(oreKey.startsWith(oreDictName.substring(0, oreDictName.length()-1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && !oreDictName.endsWith("*"))
{
if(oreKey.endsWith(oreDictName.substring(1)))
{
return true;
}
}
else if(oreDictName.startsWith("*") && oreDictName.endsWith("*"))
{
if(oreKey.contains(oreDictName.substring(1, oreDictName.length()-1)))
{
return true;
}
}
}
return false;
return new OreDictFinder(oreDictName).modifies(itemStack);
}
@Override
public InvStack getStackFromInventory(IInventory inv, ForgeDirection side)
{
return InventoryUtils.takeTopStack(inv, side.ordinal());
return InventoryUtils.takeTopStack(inv, side.ordinal(), new OreDictFinder(oreDictName));
}
@Override

View file

@ -4,6 +4,7 @@ import mekanism.api.EnumColor;
import mekanism.common.IInvConfiguration;
import mekanism.common.tileentity.TileEntityBin;
import mekanism.common.tileentity.TileEntityLogisticalSorter;
import mekanism.common.transporter.Finder;
import mekanism.common.transporter.InvStack;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
@ -225,7 +226,7 @@ public final class InventoryUtils
{
for(int i = inventory.getSizeInventory() - 1; i >= 0; i--)
{
if(inventory.getStackInSlot(i) != null && inventory.getStackInSlot(i).isItemEqual(type))
if(inventory.getStackInSlot(i) != null && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type))
{
ItemStack stack = inventory.getStackInSlot(i);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
@ -257,7 +258,7 @@ public final class InventoryUtils
{
int slotID = slots[get];
if(sidedInventory.getStackInSlot(slotID) != null && inventory.getStackInSlot(slotID).isItemEqual(type))
if(sidedInventory.getStackInSlot(slotID) != null && StackUtils.equalsWildcard(inventory.getStackInSlot(slotID), type))
{
ItemStack stack = sidedInventory.getStackInSlot(slotID);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
@ -298,7 +299,7 @@ public final class InventoryUtils
return null;
}
public static InvStack takeTopStack(IInventory inventory, int side)
public static InvStack takeTopStack(IInventory inventory, int side, Finder id)
{
inventory = checkChestInv(inventory);
@ -306,7 +307,7 @@ public final class InventoryUtils
{
for(int i = inventory.getSizeInventory() - 1; i >= 0; i--)
{
if(inventory.getStackInSlot(i) != null)
if(inventory.getStackInSlot(i) != null && id.modifies(inventory.getStackInSlot(i)))
{
ItemStack toSend = inventory.getStackInSlot(i).copy();
return new InvStack(inventory, i, toSend);
@ -323,7 +324,7 @@ public final class InventoryUtils
{
int slotID = slots[get];
if(sidedInventory.getStackInSlot(slotID) != null)
if(sidedInventory.getStackInSlot(slotID) != null && id.modifies(sidedInventory.getStackInSlot(slotID)))
{
ItemStack toSend = sidedInventory.getStackInSlot(slotID);

View file

@ -478,7 +478,7 @@ public final class MekanismUtils
{
for(ItemStack stack : entry.getValue())
{
if(stack.isItemEqual(check))
if(StackUtils.equalsWildcard(stack, check))
{
idsFound.add(entry.getKey());
break;