diff --git a/src/main/java/de/ellpeck/prettypipes/Registry.java b/src/main/java/de/ellpeck/prettypipes/Registry.java index acfbd62..0ea271d 100644 --- a/src/main/java/de/ellpeck/prettypipes/Registry.java +++ b/src/main/java/de/ellpeck/prettypipes/Registry.java @@ -3,6 +3,7 @@ package de.ellpeck.prettypipes; import de.ellpeck.prettypipes.entities.PipeFrameEntity; import de.ellpeck.prettypipes.entities.PipeFrameRenderer; import de.ellpeck.prettypipes.items.*; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.pipe.modules.FilterModifierModule; import de.ellpeck.prettypipes.pipe.modules.LowPriorityModuleItem; import de.ellpeck.prettypipes.pipe.modules.RedstoneModuleItem; @@ -116,7 +117,7 @@ public final class Registry { registry.registerAll(createTieredModule("low_priority_module", LowPriorityModuleItem::new)); registry.registerAll(createTieredModule("retrieval_module", RetrievalModuleItem::new)); registry.register(new StackSizeModuleItem("stack_size_module")); - registry.registerAll(Arrays.stream(FilterModifierModule.Type.values()).map(t -> new FilterModifierModule(t.name().toLowerCase(Locale.ROOT) + "_filter_modifier", t)).toArray(Item[]::new)); + registry.registerAll(Arrays.stream(ItemEqualityType.values()).map(t -> new FilterModifierModule(t.name().toLowerCase(Locale.ROOT) + "_filter_modifier", t)).toArray(Item[]::new)); registry.register(new RedstoneModuleItem("redstone_module")); ForgeRegistries.BLOCKS.getValues().stream() diff --git a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java index 47dd5ee..cce301b 100644 --- a/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java @@ -68,7 +68,7 @@ public class PipeFrameEntity extends ItemFrameEntity implements IEntityAdditiona ItemStack stack = this.getDisplayedItem(); if (!stack.isEmpty()) { List items = network.getOrderedNetworkItems(node); - int amount = items.stream().mapToInt(i -> i.getItemAmount(stack, false)).sum(); + int amount = items.stream().mapToInt(i -> i.getItemAmount(stack)).sum(); this.dataManager.set(AMOUNT, amount); return; } diff --git a/src/main/java/de/ellpeck/prettypipes/misc/EquatableItemStack.java b/src/main/java/de/ellpeck/prettypipes/misc/EquatableItemStack.java index 8584a6e..14dd9c8 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/EquatableItemStack.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/EquatableItemStack.java @@ -15,7 +15,7 @@ public class EquatableItemStack { public boolean equals(Object o) { if (o instanceof EquatableItemStack) { ItemStack other = ((EquatableItemStack) o).stack; - return ItemStack.areItemsEqual(this.stack, other) && ItemStack.areItemStackTagsEqual(this.stack, other); + return ItemEqualityType.compareItems(this.stack, other, ItemEqualityType.NBT); } return false; } diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemEqualityType.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemEqualityType.java new file mode 100644 index 0000000..56bd97e --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemEqualityType.java @@ -0,0 +1,42 @@ +package de.ellpeck.prettypipes.misc; + +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import java.util.Arrays; +import java.util.Set; +import java.util.function.BiFunction; + +public enum ItemEqualityType { + + DAMAGE((stack, filter) -> stack.getDamage() == filter.getDamage(), false), + NBT(ItemStack::areItemStackTagsEqual, false), + TAG((stack, filter) -> { + Set stackTags = stack.getItem().getTags(); + Set filterTags = filter.getItem().getTags(); + if (filterTags.isEmpty()) + return false; + return stackTags.containsAll(filterTags); + }, true); + + public final BiFunction filter; + public final boolean ignoreItemEquality; + + ItemEqualityType(BiFunction filter, boolean ignoreItemEquality) { + this.filter = filter; + this.ignoreItemEquality = ignoreItemEquality; + } + + public static boolean compareItems(ItemStack stack, ItemStack filter, ItemEqualityType... types) { + boolean equal = ItemStack.areItemsEqual(stack, filter); + if (types.length <= 0) + return equal; + for (ItemEqualityType type : types) { + if (!type.ignoreItemEquality && !equal) + return false; + if (!type.filter.apply(stack, filter)) + return false; + } + return true; + } +} diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java index f6c947c..6eda022 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java @@ -97,27 +97,25 @@ public class ItemFilter extends ItemStackHandler { } private boolean isFiltered(ItemStack stack) { - List modifiers = this.pipe.streamModules() - .map(Pair::getRight) - .filter(m -> m instanceof FilterModifierModule) - .map(m -> ((FilterModifierModule) m).type) - .collect(Collectors.toList()); + ItemEqualityType[] types = this.getEqualityTypes(); for (int i = 0; i < this.getSlots(); i++) { ItemStack filter = this.getStackInSlot(i); if (filter.isEmpty()) continue; - boolean equal = ItemStack.areItemsEqual(stack, filter); - if (modifiers.isEmpty()) { - if (equal) - return true; - } else { - if (modifiers.stream().allMatch(m -> (m.ignoreItemEquality || equal) && m.filter.apply(stack, filter))) - return true; - } + if (ItemEqualityType.compareItems(stack, filter, types)) + return true; } return false; } + public ItemEqualityType[] getEqualityTypes() { + return this.pipe.streamModules() + .map(Pair::getRight) + .filter(m -> m instanceof FilterModifierModule) + .map(m -> ((FilterModifierModule) m).type) + .toArray(ItemEqualityType[]::new); + } + public void save() { this.stack.getOrCreateTag().put("filter", this.serializeNBT()); } diff --git a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java index 77e80a8..104cdfd 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java +++ b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java @@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.network; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import net.minecraft.item.ItemStack; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; @@ -32,18 +33,18 @@ public class NetworkLocation { this.items.put(slot, stack); } - public List getStackSlots(ItemStack stack) { + public List getStackSlots(ItemStack stack, ItemEqualityType... equalityTypes) { if (this.isEmpty()) return Collections.emptyList(); return this.items.entrySet().stream() - .filter(e -> e.getValue().isItemEqual(stack)) + .filter(e -> ItemEqualityType.compareItems(e.getValue(), stack, equalityTypes)) .map(Map.Entry::getKey) .collect(Collectors.toList()); } - public int getItemAmount(ItemStack stack, boolean compareTag) { + public int getItemAmount(ItemStack stack, ItemEqualityType... equalityTypes) { return this.items.values().stream() - .filter(i -> ItemStack.areItemsEqual(i, stack) && (!compareTag || ItemStack.areItemStackTagsEqual(i, stack))) + .filter(i -> ItemEqualityType.compareItems(stack, i, equalityTypes)) .mapToInt(ItemStack::getCount).sum(); } diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index d46f482..495fceb 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -6,6 +6,7 @@ import com.google.common.collect.Streams; import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Utility; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.packets.PacketHandler; @@ -329,9 +330,9 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL return ret; } - public int getItemsOnTheWay(BlockPos goalPipe, ItemStack type) { + public int getItemsOnTheWay(BlockPos goalPipe, ItemStack type, ItemEqualityType... equalityTypes) { return this.getPipeItemsOnTheWay(goalPipe) - .filter(i -> i.stack.isItemEqual(type)) + .filter(i -> ItemEqualityType.compareItems(i.stack, type, equalityTypes)) .mapToInt(i -> i.stack.getCount()).sum(); } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/FilterModifierModule.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/FilterModifierModule.java index 8c5ba73..475c3be 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/FilterModifierModule.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/FilterModifierModule.java @@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.pipe.modules; import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.ModuleItem; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.item.ItemStack; @@ -16,9 +17,9 @@ import java.util.function.BiFunction; public class FilterModifierModule extends ModuleItem { - public final Type type; + public final ItemEqualityType type; - public FilterModifierModule(String name, Type type) { + public FilterModifierModule(String name, ItemEqualityType type) { super(name); this.type = type; this.setRegistryName(name); @@ -38,24 +39,4 @@ public class FilterModifierModule extends ModuleItem { public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { super.addInformation(stack, worldIn, tooltip, flagIn); } - - public enum Type { - DAMAGE(false, (stack, filter) -> stack.getDamage() == filter.getDamage()), - NBT(false, ItemStack::areItemStackTagsEqual), - TAG(true, (stack, filter) -> { - Set stackTags = stack.getItem().getTags(); - Set filterTags = filter.getItem().getTags(); - if (filterTags.isEmpty()) - return false; - return stackTags.containsAll(filterTags); - }); - - public final boolean ignoreItemEquality; - public final BiFunction filter; - - Type(boolean ignoreItemEquality, BiFunction filter) { - this.ignoreItemEquality = ignoreItemEquality; - this.filter = filter; - } - } } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java index 1b3ef2a..f0c72cd 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/retrieval/RetrievalModuleItem.java @@ -58,7 +58,7 @@ public class RetrievalModuleItem extends ModuleItem { for (NetworkLocation location : locations) { if (location.pipePos.equals(tile.getPos())) continue; - for (int slot : location.getStackSlots(filtered)) { + for (int slot : location.getStackSlots(filtered, filter.getEqualityTypes())) { // try to extract from that location's inventory and send the item ItemStack stack = location.handler.extractItem(slot, this.maxExtraction, true); if (network.routeItemToLocation(location.pipePos, location.pos, tile.getPos(), dest, speed -> new PipeItem(stack, speed))) { diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/stacksize/StackSizeModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/stacksize/StackSizeModuleItem.java index 11c94e8..beb7b42 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/stacksize/StackSizeModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/stacksize/StackSizeModuleItem.java @@ -3,6 +3,7 @@ package de.ellpeck.prettypipes.pipe.modules.stacksize; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.ModuleItem; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import net.minecraft.entity.player.PlayerEntity; @@ -50,7 +51,7 @@ public class StackSizeModuleItem extends ModuleItem { ItemStack stored = destination.getStackInSlot(i); if (stored.isEmpty()) continue; - if (!stored.isItemEqual(stack)) + if (!ItemEqualityType.compareItems(stored, stack)) continue; amount += stored.getCount(); if (amount >= max) diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java index 48f0a50..41297f6 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java @@ -4,6 +4,7 @@ import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.misc.EquatableItemStack; +import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.misc.ItemOrder; import de.ellpeck.prettypipes.network.NetworkItem; import de.ellpeck.prettypipes.network.NetworkLocation; @@ -136,7 +137,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine int remain = stack.getCount(); locations: for (NetworkLocation location : item.getLocations()) { - for (int slot : location.getStackSlots(stack)) { + for (int slot : location.getStackSlots(stack, ItemEqualityType.NBT)) { ItemStack extracted = location.handler.extractItem(slot, remain, true); if (!extracted.isEmpty()) { this.pendingRequests.add(Triple.of(location, slot, extracted.getCount()));