Skip to content

Commit 04baa95

Browse files
committed
clean up crafting module item requesting code
1 parent e31889a commit 04baa95

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,25 @@ public void tick(ItemStack module, PipeBlockEntity tile) {
8484
if (craft.moduleSlot == slot) {
8585
network.startProfile("crafting_ingredients");
8686
var ingredient = craft.ingredientsToRequest.getFirst();
87-
var toRequest = ingredient.map(l -> l.stack, s -> s).copy();
87+
var toRequest = ingredient.map(l -> l.stack, s -> s);
8888
var dest = tile.getAvailableDestination(Direction.values(), toRequest, true, true);
8989
if (dest != null) {
9090
// if we're ensuring the correct item order and the item is already on the way, don't do anything yet
9191
if (module.get(Contents.TYPE).insertionType != InsertionType.PER_ITEM || craft.travelingIngredients.isEmpty()) {
9292
var equalityTypes = ItemFilter.getEqualityTypes(tile);
93-
var requested = ingredient.map(l -> {
94-
// we can ignore the return value here since we're using a lock, so we know that the item is already waiting for us there
95-
network.requestExistingItem(l.location, tile.getBlockPos(), dest.getLeft(), l, dest.getRight(), equalityTypes);
96-
network.resolveNetworkLock(l);
97-
return toRequest;
98-
}, s -> {
99-
var remain = network.requestExistingItem(tile.getBlockPos(), dest.getLeft(), null, dest.getRight(), equalityTypes);
100-
var ret = s.copyWithCount(s.getCount() - remain.getCount());
101-
s.setCount(remain.getCount());
93+
var remain = ingredient.map(l -> {
94+
var ret = network.requestExistingItem(l.location, tile.getBlockPos(), dest.getLeft(), l, dest.getRight(), equalityTypes);
95+
if (ret.getCount() != l.stack.getCount())
96+
network.resolveNetworkLock(l);
10297
return ret;
103-
});
104-
if (!requested.isEmpty()) {
105-
if (toRequest.getCount() - requested.getCount() <= 0)
106-
craft.ingredientsToRequest.remove(ingredient);
107-
craft.travelingIngredients.add(requested);
98+
}, s -> network.requestExistingItem(tile.getBlockPos(), dest.getLeft(), null, dest.getRight(), equalityTypes));
99+
// dest may be able to accept less than toRequest, so the amount that remains there also needs to be taken into account
100+
remain.grow(toRequest.getCount() - dest.getRight().getCount());
101+
if (remain.getCount() != dest.getRight().getCount()) {
102+
if (!remain.isEmpty())
103+
craft.ingredientsToRequest.add(craft.ingredientsToRequest.indexOf(ingredient), Either.right(remain));
104+
craft.ingredientsToRequest.remove(ingredient);
105+
craft.travelingIngredients.add(toRequest.copyWithCount(toRequest.getCount() - remain.getCount()));
108106
craft.inProgress = true;
109107
}
110108
}

0 commit comments

Comments
 (0)