Compare commits

...

2 commits

Author SHA1 Message Date
Ell
24b1bbd84b TODO for cleanup 2024-11-27 21:46:51 +01:00
Ell
f6ca9297dd store craft ingredient requests bundled together 2024-11-27 21:25:45 +01:00
4 changed files with 23 additions and 13 deletions

View file

@ -270,8 +270,10 @@ public class PipeBlock extends BaseEntityBlock implements SimpleWaterloggedBlock
network.onPipeChanged(pos, state);
if (worldIn.getBlockEntity(pos) instanceof PipeBlockEntity pipe) {
pipe.getItems().clear();
for (var lock : pipe.craftIngredientRequests)
network.resolveNetworkLock(lock.getRight());
for (var locks : pipe.craftIngredientRequests) {
for (var lock : locks.getRight())
network.resolveNetworkLock(lock);
}
}
super.onRemove(state, worldIn, pos, newState, isMoving);
}

View file

@ -71,8 +71,9 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
PipeBlockEntity.this.setChanged();
}
};
// crafting module slot, ingredient request network lock
public final List<Pair<Integer, NetworkLock>> craftIngredientRequests = new ArrayList<>();
// TODO instead of having these loose lists, it would be nice to have a "ModuleData" system that allows modules to store an object of custom data on the pipe
// crafting module slot, ingredient request network locks (one list for each recipe)
public final List<Pair<Integer, List<NetworkLock>>> craftIngredientRequests = new ArrayList<>();
// crafting module slot, destination pipe for the result, result item
public final List<Triple<Integer, BlockPos, ItemStack>> craftResultRequests = new ArrayList<>();
public PressurizerBlockEntity pressurizer;
@ -105,7 +106,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
for (var tuple : this.craftIngredientRequests) {
var nbt = new CompoundTag();
nbt.putInt("module_slot", tuple.getLeft());
nbt.put("lock", tuple.getRight().serializeNBT(provider));
nbt.put("locks", Utility.serializeAll(provider, tuple.getRight()));
requests.add(nbt);
}
compound.put("craft_requests", requests);
@ -133,7 +134,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
var nbt = requests.getCompound(i);
this.craftIngredientRequests.add(Pair.of(
nbt.getInt("module_slot"),
new NetworkLock(provider, nbt.getCompound("lock"))));
Utility.deserializeAll(nbt.getList("locks", Tag.TAG_COMPOUND), c -> new NetworkLock(provider, c))));
}
this.craftResultRequests.clear();
var results = compound.getList("craft_results", Tag.TAG_COMPOUND);

View file

@ -76,7 +76,8 @@ public class CraftingModuleItem extends ModuleItem {
network.startProfile("crafting_ingredients");
var request = tile.craftIngredientRequests.getFirst();
if (request.getLeft() == slot) {
var lock = request.getRight();
var locks = request.getRight();
var lock = locks.getFirst();
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var dest = tile.getAvailableDestination(Direction.values(), lock.stack, true, true);
if (dest != null) {
@ -85,7 +86,7 @@ public class CraftingModuleItem extends ModuleItem {
if (!ensureItemOrder || network.getPipeItemsOnTheWay(dest.getLeft()).findAny().isEmpty()) {
var requestRemain = network.requestExistingItem(lock.location, tile.getBlockPos(), dest.getLeft(), lock, dest.getRight(), equalityTypes);
network.resolveNetworkLock(lock);
tile.craftIngredientRequests.remove(request);
locks.remove(lock);
// if we couldn't fit all items into the destination, create another request for the rest
var remain = lock.stack.copy();
@ -94,9 +95,12 @@ public class CraftingModuleItem extends ModuleItem {
var remainRequest = new NetworkLock(lock.location, remain);
// if we're ensuring item order, we need to insert the remaining request at the start so that it gets processed first
var index = ensureItemOrder ? 0 : tile.craftResultRequests.size();
tile.craftIngredientRequests.add(index, Pair.of(slot, remainRequest));
locks.add(index, remainRequest);
network.createNetworkLock(remainRequest);
}
if (locks.isEmpty())
tile.craftIngredientRequests.remove(request);
}
}
}
@ -186,6 +190,7 @@ public class CraftingModuleItem extends ModuleItem {
var craftableCrafts = Mth.ceil(craftableAmount / (float) resultAmount);
var toCraft = Math.min(craftableCrafts, requiredCrafts);
var locks = new ArrayList<NetworkLock>();
var contents = module.get(Contents.TYPE);
// if we're ensuring item order, all items for a single recipe should be sent in order first before starting on the next one!
for (var c = contents.ensureItemOrder ? toCraft : 1; c > 0; c--) {
@ -197,10 +202,10 @@ public class CraftingModuleItem extends ModuleItem {
if (!contents.ensureItemOrder)
copy.setCount(in.getCount() * toCraft);
var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
for (var lock : ret.getLeft())
tile.craftIngredientRequests.add(Pair.of(slot, lock));
locks.addAll(ret.getLeft());
}
}
tile.craftIngredientRequests.add(Pair.of(slot, locks));
var remain = stack.copy();
remain.shrink(resultAmount * toCraft);

View file

@ -217,8 +217,10 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
for (var craftable : network.getAllCraftables(pipe.getBlockPos())) {
var otherPipe = network.getPipe(craftable.getLeft());
if (otherPipe != null) {
for (var lock : otherPipe.craftIngredientRequests)
network.resolveNetworkLock(lock.getRight());
for (var locks : otherPipe.craftIngredientRequests) {
for (var lock : locks.getRight())
network.resolveNetworkLock(lock);
}
otherPipe.craftIngredientRequests.clear();
otherPipe.craftResultRequests.clear();
}