Converted the Master Routing Node so that it would use a TreeMap instead of a HashMap for sorting. Modified TestItemFilter so it actually worked without exceptions.

Added default item filter which allows all items through.
This commit is contained in:
WayofTime 2016-01-21 10:45:45 -05:00
parent 8f17953b97
commit 8b6787b27c
8 changed files with 195 additions and 42 deletions

View file

@ -71,10 +71,6 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider
}
ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack);
if (ghostStack.stackSize == 0)
{
ghostStack.stackSize = Integer.MAX_VALUE;
}
filteredList.add(ghostStack);
}

View file

@ -168,7 +168,7 @@ public class ModRecipes
TartaricForgeRecipeRegistry.registerRecipe(ItemComponent.getStack(ItemComponent.REAGENT_SUPPRESSION), 500, 50, ModBlocks.teleposer, Items.water_bucket, Items.lava_bucket, Items.blaze_rod);
TartaricForgeRecipeRegistry.registerRecipe(ItemComponent.getStack(ItemComponent.REAGENT_BINDING), 400, 10, "dustGlowstone", "dustRedstone", "nuggetGold", Items.gunpowder);
TartaricForgeRecipeRegistry.registerRecipe(new ItemStack(ModItems.baseItemFilter), 400, 10, new ItemStack(Blocks.glass), new ItemStack(Blocks.cobblestone), new ItemStack(ModItems.slate));
TartaricForgeRecipeRegistry.registerRecipe(new ItemStack(ModItems.baseItemFilter), 400, 10, new ItemStack(Blocks.glass), new ItemStack(Blocks.cobblestone), new ItemStack(ModItems.slate), Items.stick);
TartaricForgeRecipeRegistry.registerRecipe(new ItemStack(ModItems.nodeRouter), 400, 5, Items.stick, new ItemStack(ModItems.slate, 1, 1), "gemLapis", "gemLapis");
TartaricForgeRecipeRegistry.registerRecipe(new ItemStack(ModBlocks.itemRoutingNode), 400, 5, "dustGlowstone", "dustRedstone", "blockGlass", "stone");
TartaricForgeRecipeRegistry.registerRecipe(new ItemStack(ModBlocks.outputRoutingNode), 400, 25, "dustGlowstone", "dustRedstone", "ingotIron", new ItemStack(ModBlocks.itemRoutingNode));

View file

@ -0,0 +1,141 @@
package WayofTime.bloodmagic.routing;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import WayofTime.bloodmagic.util.Utils;
/**
* This particular implementation of IItemFilter allows any item to be drained
* from or inputed to the connected inventory. Every stack is accepted here!
* We're basically Olive Gardens.
*
* @author WayofTime
*
*/
public class DefaultItemFilter implements IItemFilter
{
private IInventory accessedInventory;
private EnumFacing accessedSide;
/**
* Initializes the filter so that it knows what it wants to fulfill.
*
* @param filteredList
* - The list of ItemStacks that the filter is set to.
* @param inventory
* - The inventory that is being accessed. This inventory is either
* being pulled from or pushed to.
* @param side
* - The side that the inventory is being accessed from. Used for
* pulling/pushing from/to the inventory.
* @param isFilterOutput
* - Tells the filter what actions to expect. If true, it should be
* initialized as an output filter. If false, it should be
* initialized as an input filter.
*/
public void initializeFilter(List<ItemStack> filteredList, IInventory inventory, EnumFacing side, boolean isFilterOutput)
{
accessedInventory = inventory;
accessedSide = side;
}
/**
* This method is only called when the output inventory this filter is
* managing receives an ItemStack. Should only really be called by the Input
* filter via it's transfer method.
*
* @param stack
* -
* @return - The remainder of the stack after it has been absorbed into the
* inventory.
*/
public ItemStack transferStackThroughOutputFilter(ItemStack inputStack)
{
int allowedAmount = inputStack.stackSize; //This is done to make the migration to a maximum amount transfered a lot easier
if (allowedAmount <= 0)
{
return inputStack;
}
ItemStack testStack = inputStack.copy();
testStack.stackSize = allowedAmount;
ItemStack remainderStack = Utils.insertStackIntoInventory(testStack, accessedInventory, accessedSide);
int changeAmount = allowedAmount - (remainderStack == null ? 0 : remainderStack.stackSize);
testStack = inputStack.copy();
testStack.stackSize -= changeAmount;
return testStack;
}
/**
* This method is only called on an input filter to transfer ItemStacks from
* the input inventory to the output inventory.
*/
public void transferThroughInputFilter(IItemFilter outputFilter)
{
boolean[] canAccessSlot = new boolean[accessedInventory.getSizeInventory()];
if (accessedInventory instanceof ISidedInventory)
{
int[] slots = ((ISidedInventory) accessedInventory).getSlotsForFace(accessedSide);
for (int slot : slots)
{
canAccessSlot[slot] = true;
}
} else
{
for (int slot = 0; slot < accessedInventory.getSizeInventory(); slot++)
{
canAccessSlot[slot] = true;
}
}
for (int slot = 0; slot < accessedInventory.getSizeInventory(); slot++)
{
if (!canAccessSlot[slot])
{
continue;
}
ItemStack inputStack = accessedInventory.getStackInSlot(slot);
if (inputStack == null || (accessedInventory instanceof ISidedInventory && !((ISidedInventory) accessedInventory).canExtractItem(slot, inputStack, accessedSide)))
{
continue;
}
int allowedAmount = inputStack.stackSize;
ItemStack testStack = inputStack.copy();
testStack.stackSize = allowedAmount;
ItemStack remainderStack = outputFilter.transferStackThroughOutputFilter(testStack);
int changeAmount = allowedAmount - (remainderStack == null ? 0 : remainderStack.stackSize);
if (remainderStack != null && remainderStack.stackSize == allowedAmount)
{
//Nothing has changed. Moving on!
continue;
}
inputStack.stackSize -= changeAmount;
accessedInventory.setInventorySlotContents(slot, inputStack.stackSize <= 0 ? null : inputStack); //Sets the slot in the inventory
}
}
@Override
public boolean doesStackMatchFilter(ItemStack testStack)
{
return true;
}
@Override
public boolean doStacksMatch(ItemStack filterStack, ItemStack testStack)
{
return true;
}
}

View file

@ -192,14 +192,16 @@ public class TestItemFilter implements IItemFilter
testStack = inputStack.copy();
testStack.stackSize -= changeAmount;
for (ItemStack filterStack : requestList)
Iterator<ItemStack> itr = requestList.iterator();
while (itr.hasNext())
{
ItemStack filterStack = itr.next();
if (doStacksMatch(filterStack, inputStack))
{
filterStack.stackSize -= changeAmount;
if (filterStack.stackSize <= 0)
{
requestList.remove(filterStack);
itr.remove();
}
}
}
@ -273,14 +275,16 @@ public class TestItemFilter implements IItemFilter
accessedInventory.setInventorySlotContents(slot, inputStack.stackSize <= 0 ? null : inputStack); //Sets the slot in the inventory
for (ItemStack filterStack : requestList)
Iterator<ItemStack> itr = requestList.iterator();
while (itr.hasNext())
{
ItemStack filterStack = itr.next();
if (doStacksMatch(filterStack, inputStack))
{
filterStack.stackSize -= changeAmount;
if (filterStack.stackSize <= 0)
{
requestList.remove(filterStack);
itr.remove();
}
}
}

View file

@ -1,6 +1,11 @@
package WayofTime.bloodmagic.tile.routing;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import WayofTime.bloodmagic.item.routing.IItemFilterProvider;
import WayofTime.bloodmagic.routing.DefaultItemFilter;
import WayofTime.bloodmagic.routing.IInputItemRoutingNode;
import WayofTime.bloodmagic.routing.IItemFilter;
@ -20,20 +25,21 @@ public class TileInputRoutingNode extends TileFilteredRoutingNode implements IIn
@Override
public IItemFilter getInputFilterForSide(EnumFacing side)
{
// ItemStack filterStack = this.getFilterStack(side);
//
// if (filterStack == null || !(filterStack.getItem() instanceof IItemFilterProvider))
// {
// return null;
// }
//
// IItemFilterProvider filter = (IItemFilterProvider) filterStack.getItem();
//
// TileEntity tile = worldObj.getTileEntity(pos.offset(side));
// if (tile instanceof IInventory)
// {
// return filter.getInputItemFilter(filterStack, (IInventory) tile, side.getOpposite());
// }
TileEntity tile = worldObj.getTileEntity(pos.offset(side));
if (tile instanceof IInventory)
{
ItemStack filterStack = this.getFilterStack(side);
if (filterStack == null || !(filterStack.getItem() instanceof IItemFilterProvider))
{
IItemFilter filter = new DefaultItemFilter();
filter.initializeFilter(null, (IInventory) tile, side.getOpposite(), false);
return filter;
}
IItemFilterProvider filter = (IItemFilterProvider) filterStack.getItem();
return filter.getInputItemFilter(filterStack, (IInventory) tile, side.getOpposite());
}
return null;
}

View file

@ -1,6 +1,6 @@
package WayofTime.bloodmagic.tile.routing;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -31,7 +31,7 @@ public class TileMasterRoutingNode extends TileInventory implements IMasterRouti
}
// A list of connections
private HashMap<BlockPos, List<BlockPos>> connectionMap = new HashMap<BlockPos, List<BlockPos>>();
private TreeMap<BlockPos, List<BlockPos>> connectionMap = new TreeMap<BlockPos, List<BlockPos>>();
private List<BlockPos> generalNodeList = new LinkedList<BlockPos>();
private List<BlockPos> outputNodeList = new LinkedList<BlockPos>();
private List<BlockPos> inputNodeList = new LinkedList<BlockPos>();
@ -46,7 +46,7 @@ public class TileMasterRoutingNode extends TileInventory implements IMasterRouti
return;
}
Map<Integer, List<IItemFilter>> outputMap = new HashMap<Integer, List<IItemFilter>>();
Map<Integer, List<IItemFilter>> outputMap = new TreeMap<Integer, List<IItemFilter>>();
for (BlockPos outputPos : outputNodeList)
{
@ -80,7 +80,7 @@ public class TileMasterRoutingNode extends TileInventory implements IMasterRouti
}
}
Map<Integer, List<IItemFilter>> inputMap = new HashMap<Integer, List<IItemFilter>>();
Map<Integer, List<IItemFilter>> inputMap = new TreeMap<Integer, List<IItemFilter>>();
for (BlockPos inputPos : inputNodeList)
{

View file

@ -1,6 +1,11 @@
package WayofTime.bloodmagic.tile.routing;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import WayofTime.bloodmagic.item.routing.IItemFilterProvider;
import WayofTime.bloodmagic.routing.DefaultItemFilter;
import WayofTime.bloodmagic.routing.IItemFilter;
import WayofTime.bloodmagic.routing.IOutputItemRoutingNode;
@ -20,20 +25,22 @@ public class TileOutputRoutingNode extends TileFilteredRoutingNode implements IO
@Override
public IItemFilter getOutputFilterForSide(EnumFacing side)
{
// ItemStack filterStack = this.getFilterStack(side);
//
// if (filterStack == null || !(filterStack.getItem() instanceof IItemFilterProvider))
// {
// return null;
// }
//
// IItemFilterProvider filter = (IItemFilterProvider) filterStack.getItem();
//
// TileEntity tile = worldObj.getTileEntity(pos.offset(side));
// if (tile instanceof IInventory)
// {
// return filter.getOutputItemFilter(filterStack, (IInventory) tile, side.getOpposite());
// }
TileEntity tile = worldObj.getTileEntity(pos.offset(side));
if (tile instanceof IInventory)
{
ItemStack filterStack = this.getFilterStack(side);
if (filterStack == null || !(filterStack.getItem() instanceof IItemFilterProvider))
{
IItemFilter filter = new DefaultItemFilter();
filter.initializeFilter(null, (IInventory) tile, side.getOpposite(), true);
return filter;
}
IItemFilterProvider filter = (IItemFilterProvider) filterStack.getItem();
return filter.getOutputItemFilter(filterStack, (IInventory) tile, side.getOpposite());
}
return null;
}

View file

@ -167,7 +167,6 @@ public class TileRoutingNode extends TileInventory implements IRoutingNode, IIte
if (connectionList.contains(pos1))
{
connectionList.remove(pos1);
System.out.println("Position: " + pos + ", remaining: " + connectionList.size());
worldObj.markBlockForUpdate(pos);
}
}