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:
parent
8f17953b97
commit
8b6787b27c
8 changed files with 195 additions and 42 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue