diff --git a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java index 7dc3ba9..2d87376 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java +++ b/src/main/java/de/ellpeck/prettypipes/network/NetworkLocation.java @@ -5,38 +5,51 @@ import com.google.common.collect.ListMultimap; import net.minecraft.item.ItemStack; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; +import net.minecraftforge.items.IItemHandler; import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; public class NetworkLocation { public final BlockPos pipePos; - private ListMultimap> items; + public final Direction direction; + public final BlockPos pos; + public final IItemHandler handler; + private Map items; - public NetworkLocation(BlockPos pipePos) { + public NetworkLocation(BlockPos pipePos, Direction direction, IItemHandler handler) { this.pipePos = pipePos; + this.direction = direction; + this.pos = pipePos.offset(direction); + this.handler = handler; } - public void addItem(Direction direction, int slot, ItemStack stack) { + public void addItem(int slot, ItemStack stack) { if (this.items == null) - this.items = ArrayListMultimap.create(); - this.items.put(direction, Pair.of(slot, stack)); + this.items = new HashMap<>(); + this.items.put(slot, stack); } - public Pair getStackLocation(ItemStack stack) { + public int getStackSlot(ItemStack stack) { if (this.isEmpty()) - return null; - for (Map.Entry> entry : this.items.entries()) { - if (entry.getValue().getRight().isItemEqual(stack)) - return Pair.of(entry.getKey(), entry.getValue().getLeft()); + return -1; + for (Map.Entry entry : this.items.entrySet()) { + if (entry.getValue().isItemEqual(stack)) + return entry.getKey(); } - return null; + return -1; } public boolean isEmpty() { return this.items == null || this.items.isEmpty(); } + + @Override + public String toString() { + return this.items.values().toString(); + } } diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index af76115..feeaa50 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -180,7 +180,6 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL this.startProfile("get_network_items"); List info = new ArrayList<>(); for (BlockPos dest : this.getOrderedDestinations(node)) { - NetworkLocation location = new NetworkLocation(dest); PipeTileEntity pipe = this.getPipe(dest); if (!pipe.canNetworkSee()) continue; @@ -188,15 +187,19 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL IItemHandler handler = pipe.getItemHandler(dir); if (handler == null) continue; + // check if this handler already exists (double-connected pipes, double chests etc.) + if (info.stream().anyMatch(l -> l.handler == handler)) + continue; + NetworkLocation location = new NetworkLocation(dest, dir, handler); for (int i = 0; i < handler.getSlots(); i++) { ItemStack found = handler.extractItem(i, Integer.MAX_VALUE, true); if (found.isEmpty()) continue; - location.addItem(dir, i, found); + location.addItem(i, found); } + if (!location.isEmpty()) + info.add(location); } - if (!location.isEmpty()) - info.add(location); } this.endProfile(); return info; diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index 9d4591a..c1aae7b 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -204,7 +204,6 @@ public class PipeBlock extends ContainerBlock implements IPipeConnectable { } if (force || connections > 2) { network.addNode(pos, newState); - System.out.println("Node at " + pos); } else { network.removeNode(pos); } 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 94af0bd..f9767da 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 @@ -57,16 +57,13 @@ public class RetrievalModuleItem extends ModuleItem { if (locations == null) locations = network.getOrderedNetworkItems(tile.getPos()); for (NetworkLocation location : locations) { - Pair item = location.getStackLocation(filtered); - if (item == null) + int slot = location.getStackSlot(filtered); + if (slot < 0) continue; - // get that location's pipe and inventory - PipeTileEntity pipe = network.getPipe(location.pipePos); - IItemHandler handler = pipe.getItemHandler(item.getKey()); // try to extract from that location's inventory and send the item - ItemStack stack = handler.extractItem(item.getValue(), this.maxExtraction, true); - if (network.routeItemToLocation(location.pipePos, location.pipePos.offset(item.getKey()), tile.getPos(), dest, speed -> new PipeItem(stack, speed))) { - handler.extractItem(item.getValue(), stack.getCount(), false); + ItemStack stack = location.handler.extractItem(slot, this.maxExtraction, true); + if (network.routeItemToLocation(location.pipePos, location.pos, tile.getPos(), dest, speed -> new PipeItem(stack, speed))) { + location.handler.extractItem(slot, stack.getCount(), false); return; } }