fixed items being seen as in the network multiple times when connected at multiple pipes

This commit is contained in:
Ellpeck 2020-04-19 18:23:53 +02:00
parent dd894f996e
commit 34f037a8fd
4 changed files with 36 additions and 24 deletions

View file

@ -5,38 +5,51 @@ import com.google.common.collect.ListMultimap;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class NetworkLocation { public class NetworkLocation {
public final BlockPos pipePos; public final BlockPos pipePos;
private ListMultimap<Direction, Pair<Integer, ItemStack>> items; public final Direction direction;
public final BlockPos pos;
public final IItemHandler handler;
private Map<Integer, ItemStack> items;
public NetworkLocation(BlockPos pipePos) { public NetworkLocation(BlockPos pipePos, Direction direction, IItemHandler handler) {
this.pipePos = pipePos; 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) if (this.items == null)
this.items = ArrayListMultimap.create(); this.items = new HashMap<>();
this.items.put(direction, Pair.of(slot, stack)); this.items.put(slot, stack);
} }
public Pair<Direction, Integer> getStackLocation(ItemStack stack) { public int getStackSlot(ItemStack stack) {
if (this.isEmpty()) if (this.isEmpty())
return null; return -1;
for (Map.Entry<Direction, Pair<Integer, ItemStack>> entry : this.items.entries()) { for (Map.Entry<Integer, ItemStack> entry : this.items.entrySet()) {
if (entry.getValue().getRight().isItemEqual(stack)) if (entry.getValue().isItemEqual(stack))
return Pair.of(entry.getKey(), entry.getValue().getLeft()); return entry.getKey();
} }
return null; return -1;
} }
public boolean isEmpty() { public boolean isEmpty() {
return this.items == null || this.items.isEmpty(); return this.items == null || this.items.isEmpty();
} }
@Override
public String toString() {
return this.items.values().toString();
}
} }

View file

@ -180,7 +180,6 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
this.startProfile("get_network_items"); this.startProfile("get_network_items");
List<NetworkLocation> info = new ArrayList<>(); List<NetworkLocation> info = new ArrayList<>();
for (BlockPos dest : this.getOrderedDestinations(node)) { for (BlockPos dest : this.getOrderedDestinations(node)) {
NetworkLocation location = new NetworkLocation(dest);
PipeTileEntity pipe = this.getPipe(dest); PipeTileEntity pipe = this.getPipe(dest);
if (!pipe.canNetworkSee()) if (!pipe.canNetworkSee())
continue; continue;
@ -188,16 +187,20 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
IItemHandler handler = pipe.getItemHandler(dir); IItemHandler handler = pipe.getItemHandler(dir);
if (handler == null) if (handler == null)
continue; 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++) { for (int i = 0; i < handler.getSlots(); i++) {
ItemStack found = handler.extractItem(i, Integer.MAX_VALUE, true); ItemStack found = handler.extractItem(i, Integer.MAX_VALUE, true);
if (found.isEmpty()) if (found.isEmpty())
continue; continue;
location.addItem(dir, i, found); location.addItem(i, found);
}
} }
if (!location.isEmpty()) if (!location.isEmpty())
info.add(location); info.add(location);
} }
}
this.endProfile(); this.endProfile();
return info; return info;
} }

View file

@ -204,7 +204,6 @@ public class PipeBlock extends ContainerBlock implements IPipeConnectable {
} }
if (force || connections > 2) { if (force || connections > 2) {
network.addNode(pos, newState); network.addNode(pos, newState);
System.out.println("Node at " + pos);
} else { } else {
network.removeNode(pos); network.removeNode(pos);
} }

View file

@ -57,16 +57,13 @@ public class RetrievalModuleItem extends ModuleItem {
if (locations == null) if (locations == null)
locations = network.getOrderedNetworkItems(tile.getPos()); locations = network.getOrderedNetworkItems(tile.getPos());
for (NetworkLocation location : locations) { for (NetworkLocation location : locations) {
Pair<Direction, Integer> item = location.getStackLocation(filtered); int slot = location.getStackSlot(filtered);
if (item == null) if (slot < 0)
continue; 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 // try to extract from that location's inventory and send the item
ItemStack stack = handler.extractItem(item.getValue(), this.maxExtraction, true); ItemStack stack = location.handler.extractItem(slot, this.maxExtraction, true);
if (network.routeItemToLocation(location.pipePos, location.pipePos.offset(item.getKey()), tile.getPos(), dest, speed -> new PipeItem(stack, speed))) { if (network.routeItemToLocation(location.pipePos, location.pos, tile.getPos(), dest, speed -> new PipeItem(stack, speed))) {
handler.extractItem(item.getValue(), stack.getCount(), false); location.handler.extractItem(slot, stack.getCount(), false);
return; return;
} }
} }