From afd055c8b196d91c7a79d6c2895b3b45c216a03c Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 20 Oct 2020 01:30:33 +0200 Subject: [PATCH] made the retrieval module only retrieve when it's not waiting for crafting outputs --- .../prettypipes/network/PipeNetwork.java | 31 ++++++++++++++++++ .../retrieval/RetrievalModuleItem.java | 6 +++- .../terminal/ItemTerminalTileEntity.java | 32 +++++-------------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index b800811..6fd7bd2 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -251,6 +251,37 @@ public class PipeNetwork implements ICapabilitySerializable, GraphL return tile; } + public List> getCurrentlyCrafting(BlockPos node) { + this.startProfile("get_currently_crafting"); + List> items = new ArrayList<>(); + for (Pair craftable : this.getAllCraftables(node)) { + PipeTileEntity pipe = this.getPipe(craftable.getLeft()); + if (pipe == null) + continue; + for (Pair request : pipe.craftResultRequests) { + BlockPos dest = request.getLeft(); + ItemStack stack = request.getRight(); + // add up all the items that should go to the same location + Optional> 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> getAllCraftables(BlockPos node) { if (!this.isNode(node)) return Collections.emptyList(); 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 2501dd0..1a6ffb8 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 @@ -43,6 +43,7 @@ public class RetrievalModuleItem extends ModuleItem { PipeNetwork network = PipeNetwork.get(tile.getWorld()); ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); + ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile); filter.isWhitelist = true; // loop through filter to see which items to pull for (int f = 0; f < filter.getSlots(); f++) { @@ -54,7 +55,10 @@ public class RetrievalModuleItem extends ModuleItem { Pair dest = tile.getAvailableDestination(copy, true, this.preventOversending); if (dest == null) 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; } } diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java index 5fdad28..121b10d 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java @@ -133,10 +133,9 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine return; this.networkItems = this.collectItems(); if (playersToSync.length > 0) { - List> craftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos()); List clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList()); - List clientCraftables = craftables.stream().map(Pair::getRight).collect(Collectors.toList()); - List currentlyCrafting = this.getCurrentlyCrafting(craftables).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); + List clientCraftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos()).stream().map(Pair::getRight).collect(Collectors.toList()); + List currentlyCrafting = this.getCurrentlyCrafting().stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); for (PlayerEntity player : playersToSync) { if (!(player.openContainer instanceof ItemTerminalContainer)) continue; @@ -192,28 +191,13 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine return items; } - private List getCurrentlyCrafting(List> craftables) { + private List getCurrentlyCrafting() { PipeNetwork network = PipeNetwork.get(this.world); - List> items = new ArrayList<>(); - for (Pair craftable : craftables) { - PipeTileEntity pipe = network.getPipe(craftable.getLeft()); - if (pipe == null) - continue; - for (Pair request : pipe.craftResultRequests) { - BlockPos dest = request.getLeft(); - ItemStack stack = request.getRight(); - // add up all the items that should go to the same location - Optional> 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()); + PipeTileEntity pipe = this.getConnectedPipe(); + if (pipe == null) + return Collections.emptyList(); + List> crafting = network.getCurrentlyCrafting(pipe.getPos()); + return crafting.stream().map(Pair::getRight).collect(Collectors.toList()); } public void cancelCrafting() {