Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void addInformation(ItemStack stack, World world, List<String> tooltip, I

@Override
public IItemFilter getInputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) {
IItemFilter testFilter = new TestItemFilter();
IItemFilter testFilter = new PreciseItemFilter();

switch (filterStack.getMetadata()) {
case 0:
testFilter = new TestItemFilter();
testFilter = new PreciseItemFilter();
break;
case 1:
testFilter = new IgnoreNBTItemFilter();
Expand Down Expand Up @@ -101,7 +101,7 @@ public IItemFilter getOutputItemFilter(ItemStack filterStack, TileEntity tile, I

switch (filterStack.getMetadata()) {
case 0:
testFilter = new TestItemFilter();
testFilter = new PreciseItemFilter();
break;
case 1:
testFilter = new IgnoreNBTItemFilter();
Expand Down
78 changes: 23 additions & 55 deletions src/main/java/WayofTime/bloodmagic/routing/DefaultItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,82 +36,50 @@ public void initializeFilter(List<ItemStack> filteredList, TileEntity tile, IIte
this.itemHandler = itemHandler;
}

/**
* 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 inputStack - The stack to transfer
* @return - The remainder of the stack after it has been absorbed into the
* inventory.
*/
@Override
public ItemStack transferStackThroughOutputFilter(ItemStack inputStack) {
int allowedAmount = inputStack.getCount(); //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.setCount(allowedAmount);
ItemStack remainderStack = Utils.insertStackIntoTile(testStack, itemHandler);

int changeAmount = allowedAmount - (remainderStack.isEmpty() ? 0 : remainderStack.getCount());
testStack = inputStack.copy();
testStack.shrink(changeAmount);

World world = accessedTile.getWorld();
BlockPos pos = accessedTile.getPos();
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);

return testStack;
}

/**
* This method is only called on an input filter to transfer ItemStacks from
* the input inventory to the output inventory.
*/
@Override
public int transferThroughInputFilter(IItemFilter outputFilter, int maxTransfer) {
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
ItemStack inputStack = itemHandler.getStackInSlot(slot);
if (inputStack.isEmpty() || itemHandler.extractItem(slot, inputStack.getCount(), true).isEmpty())//(accessedInventory instanceof ISidedInventory && !((ISidedInventory) accessedInventory).canExtractItem(slot, inputStack, accessedSide)))
{
int taken = outputFilter.offerStack(itemHandler, slot, maxTransfer);
if (taken == 0) {
continue;
}

int allowedAmount = Math.min(itemHandler.extractItem(slot, inputStack.getCount(), true).getCount(), maxTransfer);

ItemStack testStack = inputStack.copy();
testStack.setCount(allowedAmount);
ItemStack remainderStack = outputFilter.transferStackThroughOutputFilter(testStack);
int changeAmount = allowedAmount - (remainderStack.isEmpty() ? 0 : remainderStack.getCount());

if (!remainderStack.isEmpty() && remainderStack.getCount() == allowedAmount) {
//Nothing has changed. Moving on!
continue;
}

itemHandler.extractItem(slot, changeAmount, false);

World world = accessedTile.getWorld();
BlockPos pos = accessedTile.getPos();
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);

return changeAmount;
return taken;
}

return 0;
}

@Override
public boolean doesStackMatchFilter(ItemStack testStack) {
return true;
public int offerStack(IItemHandler inv, int slot, int maxTransfer) {
if (inv.getStackInSlot(slot).isEmpty()) {
return 0;
}

ItemStack extracted = inv.extractItem(slot, maxTransfer, true);
if (extracted.isEmpty()) {
return 0;
}
ItemStack remainderStack = Utils.insertStackIntoTile(extracted, itemHandler);
int taken = extracted.getCount() - remainderStack.getCount();
inv.extractItem(slot, taken, false);

World world = accessedTile.getWorld();
BlockPos pos = accessedTile.getPos();
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);

return taken;
}

@Override
public boolean doStacksMatch(ItemStack filterStack, ItemStack testStack) {
return true;
public boolean canSkip() {
return false;
}
}
35 changes: 21 additions & 14 deletions src/main/java/WayofTime/bloodmagic/routing/IItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ public interface IItemFilter extends IRoutingFilter {
void initializeFilter(List<ItemStack> filteredList, TileEntity tile, IItemHandler itemHandler, boolean isFilterOutput);

/**
* 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 inputStack - The stack to filter
* @return - The remainder of the stack after it has been absorbed into the
* inventory.
* Tries to transfer items from this filter to outputFilter.
* Potentially modifies outputFilter and this filter.
* @param outputFilter the filter to transfer items to
* @param maxTransfer the max amount of items to transfer
* @return the amount of items actually transferred.
*/
ItemStack transferStackThroughOutputFilter(ItemStack inputStack);
int transferThroughInputFilter(IItemFilter outputFilter, int maxTransfer);

/**
* This method is only called on an input filter to transfer ItemStacks from
* the input inventory to the output inventory.
* Offers an inventory slot to this output filter. The output filter will
* try to extract the stack and return how many items it actually extracted.
* @param inv The inventory that has the offered ItemStack.
* inv may be modified by calling inv.extractItem(slot, ..., false)
* @param slot The slot of inv to check.
* @param maxTransfer The max amount of items that can be taken.
* @return The number of items that were actually extracted and taken.
*/
int transferThroughInputFilter(IItemFilter outputFilter, int maxTransfer);

boolean doesStackMatchFilter(ItemStack testStack);
int offerStack(IItemHandler inv, int slot, int maxTransfer);

boolean doStacksMatch(ItemStack filterStack, ItemStack testStack);
/**
* Returns true if this is an input filter that can accept no more items
* or if this is an output filter that can output no items.
* Filters may return false even if they have no work.
* @return true if this filter can be skipped
*/
boolean canSkip();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.item.ItemStack;

public class IgnoreNBTItemFilter extends TestItemFilter {
public class IgnoreNBTItemFilter extends PreciseItemFilter {
@Override
public boolean doesStackMatchFilter(ItemStack testStack) {
for (ItemStack filterStack : requestList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.item.ItemStack;

public class ModIdItemFilter extends TestItemFilter {
public class ModIdItemFilter extends PreciseItemFilter {

@Override
public boolean doStacksMatch(ItemStack filterStack, ItemStack testStack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

public class OreDictItemFilter extends TestItemFilter {
public class OreDictItemFilter extends PreciseItemFilter {
@Override
public boolean doesStackMatchFilter(ItemStack testStack) {
for (ItemStack filterStack : requestList) {
Expand Down
Loading