made the retrieval module only retrieve when it's not waiting for crafting outputs

This commit is contained in:
Ell 2020-10-20 01:30:33 +02:00
parent 76f332c03e
commit afd055c8b1
3 changed files with 44 additions and 25 deletions

View file

@ -251,6 +251,37 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return tile; return tile;
} }
public List<Pair<BlockPos, ItemStack>> getCurrentlyCrafting(BlockPos node) {
this.startProfile("get_currently_crafting");
List<Pair<BlockPos, ItemStack>> items = new ArrayList<>();
for (Pair<BlockPos, ItemStack> craftable : this.getAllCraftables(node)) {
PipeTileEntity pipe = this.getPipe(craftable.getLeft());
if (pipe == null)
continue;
for (Pair<BlockPos, ItemStack> request : pipe.craftResultRequests) {
BlockPos dest = request.getLeft();
ItemStack stack = request.getRight();
// add up all the items that should go to the same location
Optional<Pair<BlockPos, ItemStack>> existing = items.stream()
.filter(s -> s.getLeft().equals(dest) && ItemEqualityType.compareItems(s.getRight(), stack, ItemEqualityType.NBT))
.findFirst();
if (existing.isPresent()) {
existing.get().getRight().grow(stack.getCount());
} else {
items.add(Pair.of(dest, stack.copy()));
}
}
}
this.endProfile();
return items;
}
public int getCurrentlyCraftingAmount(BlockPos destNode, ItemStack stack, ItemEqualityType... equalityTypes) {
return this.getCurrentlyCrafting(destNode).stream()
.filter(p -> p.getLeft().equals(destNode) && ItemEqualityType.compareItems(p.getRight(), stack, equalityTypes))
.mapToInt(p -> p.getRight().getCount()).sum();
}
public List<Pair<BlockPos, ItemStack>> getAllCraftables(BlockPos node) { public List<Pair<BlockPos, ItemStack>> getAllCraftables(BlockPos node) {
if (!this.isNode(node)) if (!this.isNode(node))
return Collections.emptyList(); return Collections.emptyList();

View file

@ -43,6 +43,7 @@ public class RetrievalModuleItem extends ModuleItem {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile);
filter.isWhitelist = true; filter.isWhitelist = true;
// loop through filter to see which items to pull // loop through filter to see which items to pull
for (int f = 0; f < filter.getSlots(); f++) { for (int f = 0; f < filter.getSlots(); f++) {
@ -54,7 +55,10 @@ public class RetrievalModuleItem extends ModuleItem {
Pair<BlockPos, ItemStack> dest = tile.getAvailableDestination(copy, true, this.preventOversending); Pair<BlockPos, ItemStack> dest = tile.getAvailableDestination(copy, true, this.preventOversending);
if (dest == null) if (dest == null)
continue; continue;
if (network.requestItem(tile.getPos(), dest.getLeft(), dest.getRight(), ItemFilter.getEqualityTypes(tile)).isEmpty()) // are we already waiting for crafting results? If so, don't request more
if (network.getCurrentlyCraftingAmount(tile.getPos(), copy, equalityTypes) >= this.maxExtraction)
break;
if (network.requestItem(tile.getPos(), dest.getLeft(), dest.getRight(), equalityTypes).isEmpty())
break; break;
} }
} }

View file

@ -133,10 +133,9 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return; return;
this.networkItems = this.collectItems(); this.networkItems = this.collectItems();
if (playersToSync.length > 0) { if (playersToSync.length > 0) {
List<Pair<BlockPos, ItemStack>> craftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos());
List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList()); List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList());
List<ItemStack> clientCraftables = craftables.stream().map(Pair::getRight).collect(Collectors.toList()); List<ItemStack> clientCraftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos()).stream().map(Pair::getRight).collect(Collectors.toList());
List<ItemStack> currentlyCrafting = this.getCurrentlyCrafting(craftables).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); List<ItemStack> currentlyCrafting = this.getCurrentlyCrafting().stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList());
for (PlayerEntity player : playersToSync) { for (PlayerEntity player : playersToSync) {
if (!(player.openContainer instanceof ItemTerminalContainer)) if (!(player.openContainer instanceof ItemTerminalContainer))
continue; continue;
@ -192,28 +191,13 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return items; return items;
} }
private List<ItemStack> getCurrentlyCrafting(List<Pair<BlockPos, ItemStack>> craftables) { private List<ItemStack> getCurrentlyCrafting() {
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
List<Pair<BlockPos, ItemStack>> items = new ArrayList<>(); PipeTileEntity pipe = this.getConnectedPipe();
for (Pair<BlockPos, ItemStack> craftable : craftables) {
PipeTileEntity pipe = network.getPipe(craftable.getLeft());
if (pipe == null) if (pipe == null)
continue; return Collections.emptyList();
for (Pair<BlockPos, ItemStack> request : pipe.craftResultRequests) { List<Pair<BlockPos, ItemStack>> crafting = network.getCurrentlyCrafting(pipe.getPos());
BlockPos dest = request.getLeft(); return crafting.stream().map(Pair::getRight).collect(Collectors.toList());
ItemStack stack = request.getRight();
// add up all the items that should go to the same location
Optional<Pair<BlockPos, ItemStack>> existing = items.stream()
.filter(s -> s.getLeft() == dest && ItemEqualityType.compareItems(s.getRight(), stack, ItemEqualityType.NBT))
.findFirst();
if (existing.isPresent()) {
existing.get().getRight().grow(stack.getCount());
} else {
items.add(Pair.of(dest, stack.copy()));
}
}
}
return items.stream().map(Pair::getRight).collect(Collectors.toList());
} }
public void cancelCrafting() { public void cancelCrafting() {