some more fixes and cleanup

This commit is contained in:
Ell 2020-10-15 01:56:42 +02:00
parent 87d983611e
commit 27954a9765
4 changed files with 25 additions and 23 deletions

View file

@ -213,15 +213,27 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
public ItemStack requestExistingItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, ItemStack stack, ItemEqualityType... equalityTypes) {
if (location.getPos().equals(destInventory))
return stack;
// make sure we don't pull any locked items
int amount = location.getItemAmount(this.world, stack, equalityTypes);
if (amount <= 0)
return stack;
// we ignore locks on the destination inventory, since we're probably the ones trying to solve those locks
amount -= this.getLockedAmount(location.getPos(), stack, destInventory, equalityTypes);
if (amount <= 0)
return stack;
ItemStack remain = stack.copy();
// make sure we only extract less than or equal to the requested amount
if (remain.getCount() < amount)
amount = remain.getCount();
remain.shrink(amount);
for (int slot : location.getStackSlots(this.world, stack, equalityTypes)) {
// try to extract from that location's inventory and send the item
IItemHandler handler = location.getItemHandler(this.world);
ItemStack extracted = handler.extractItem(slot, remain.getCount(), true);
ItemStack extracted = handler.extractItem(slot, amount, true);
if (this.routeItemToLocation(location.pipePos, location.getPos(), destPipe, destInventory, extracted, speed -> new PipeItem(extracted, speed))) {
handler.extractItem(slot, extracted.getCount(), false);
remain.shrink(extracted.getCount());
if (remain.isEmpty())
amount -= extracted.getCount();
if (amount <= 0)
break;
}
}
@ -295,9 +307,9 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return this.networkLocks.get(pos);
}
public int getLockedAmount(BlockPos pos, ItemStack stack, ItemEqualityType... equalityTypes) {
public int getLockedAmount(BlockPos pos, ItemStack stack, BlockPos ignoredLock, ItemEqualityType... equalityTypes) {
return this.getNetworkLocks(pos).stream()
.filter(l -> ItemEqualityType.compareItems(l.stack, stack, equalityTypes))
.filter(l -> ItemEqualityType.compareItems(l.stack, stack, equalityTypes) && (ignoredLock == null || !ignoredLock.equals(l.location.getPos())))
.mapToInt(l -> l.stack.getCount()).sum();
}

View file

@ -100,23 +100,13 @@ public class CraftingModuleItem extends ModuleItem {
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos());
ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile);
for (Triple<BlockPos, BlockPos, ItemStack> request : tile.craftResultRequests) {
ItemStack reqItem = request.getRight();
ItemStack remain = request.getRight().copy();
for (NetworkLocation item : items) {
int amount = item.getItemAmount(tile.getWorld(), reqItem, equalityTypes);
amount -= network.getLockedAmount(item.getPos(), reqItem, equalityTypes);
if (amount <= 0)
continue;
ItemStack remain = reqItem.copy();
if (remain.getCount() < amount)
amount = remain.getCount();
remain.shrink(amount);
while (amount > 0) {
ItemStack copy = reqItem.copy();
copy.setCount(Math.min(reqItem.getMaxStackSize(), amount));
// we don't need to do any checks here because we just calculated the max amount we can definitely extract
network.requestExistingItem(item, request.getLeft(), request.getMiddle(), copy, equalityTypes);
amount -= copy.getCount();
remain = network.requestExistingItem(item, request.getLeft(), request.getMiddle(), remain, equalityTypes);
if (remain.isEmpty())
break;
}
if (remain.getCount() != request.getRight().getCount()) {
tile.craftResultRequests.remove(request);
// if we couldn't pull everything, log a new request
if (!remain.isEmpty())

View file

@ -173,7 +173,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
int amount = location.getItemAmount(world, stack.stack, equalityTypes);
if (amount <= 0)
continue;
amount -= network.getLockedAmount(location.getPos(), stack.stack, equalityTypes);
amount -= network.getLockedAmount(location.getPos(), stack.stack, null, equalityTypes);
available += amount;
}
// divide the total by the amount required to get the amount that

View file

@ -221,7 +221,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
int amount = location.getItemAmount(world, stack, ItemEqualityType.NBT);
if (amount <= 0)
continue;
amount -= network.getLockedAmount(location.getPos(), stack, ItemEqualityType.NBT);
amount -= network.getLockedAmount(location.getPos(), stack, destInventory, ItemEqualityType.NBT);
if (amount > 0) {
if (remain.getCount() < amount)
amount = remain.getCount();