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.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<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.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<Direction, Integer> getStackLocation(ItemStack stack) {
public int getStackSlot(ItemStack stack) {
if (this.isEmpty())
return null;
for (Map.Entry<Direction, Pair<Integer, ItemStack>> entry : this.items.entries()) {
if (entry.getValue().getRight().isItemEqual(stack))
return Pair.of(entry.getKey(), entry.getValue().getLeft());
return -1;
for (Map.Entry<Integer, ItemStack> 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();
}
}

View file

@ -180,7 +180,6 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
this.startProfile("get_network_items");
List<NetworkLocation> info = new ArrayList<>();
for (BlockPos dest : this.getOrderedDestinations(node)) {
NetworkLocation location = new NetworkLocation(dest);
PipeTileEntity pipe = this.getPipe(dest);
if (!pipe.canNetworkSee())
continue;
@ -188,16 +187,20 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, 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);
}
}
this.endProfile();
return info;
}

View file

@ -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);
}

View file

@ -57,16 +57,13 @@ public class RetrievalModuleItem extends ModuleItem {
if (locations == null)
locations = network.getOrderedNetworkItems(tile.getPos());
for (NetworkLocation location : locations) {
Pair<Direction, Integer> 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;
}
}