unify item requests into one method

This commit is contained in:
Ellpeck 2020-05-09 16:27:49 +02:00
parent d33a93e029
commit ede06585f7
5 changed files with 55 additions and 56 deletions

View file

@ -1,5 +1,6 @@
package de.ellpeck.prettypipes.network; package de.ellpeck.prettypipes.network;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT; import net.minecraft.nbt.ListNBT;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
@ -9,13 +10,11 @@ import java.util.Collection;
public class NetworkLock implements INBTSerializable<CompoundNBT> { public class NetworkLock implements INBTSerializable<CompoundNBT> {
public NetworkLocation location; public NetworkLocation location;
public int slot; public ItemStack stack;
public int amount;
public NetworkLock(NetworkLocation location, int slot, int amount) { public NetworkLock(NetworkLocation location, ItemStack stack) {
this.location = location; this.location = location;
this.slot = slot; this.stack = stack;
this.amount = amount;
} }
public NetworkLock(CompoundNBT nbt) { public NetworkLock(CompoundNBT nbt) {
@ -26,15 +25,13 @@ public class NetworkLock implements INBTSerializable<CompoundNBT> {
public CompoundNBT serializeNBT() { public CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT(); CompoundNBT nbt = new CompoundNBT();
nbt.put("location", this.location.serializeNBT()); nbt.put("location", this.location.serializeNBT());
nbt.putInt("slot", this.slot); nbt.put("stack", this.stack.write(new CompoundNBT()));
nbt.putInt("amount", this.amount);
return nbt; return nbt;
} }
@Override @Override
public void deserializeNBT(CompoundNBT nbt) { public void deserializeNBT(CompoundNBT nbt) {
this.location = new NetworkLocation(nbt.getCompound("location")); this.location = new NetworkLocation(nbt.getCompound("location"));
this.slot = nbt.getInt("slot"); this.stack = ItemStack.read(nbt.getCompound("stack"));
this.amount = nbt.getInt("amount");
} }
} }

View file

@ -177,6 +177,30 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return true; return true;
} }
public boolean requestItem(BlockPos destPipe, BlockPos destInventory, ItemStack stack, int amount, ItemEqualityType... equalityTypes) {
List<NetworkLocation> locations = this.getOrderedNetworkItems(destPipe);
for (NetworkLocation location : locations) {
if (this.requestItem(location, destPipe, destInventory, stack, amount, equalityTypes))
return true;
}
return false;
}
public boolean requestItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, ItemStack stack, int amount, ItemEqualityType... equalityTypes) {
if (location.pipePos.equals(destPipe))
return false;
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, amount, true);
if (this.routeItemToLocation(location.pipePos, location.getPos(), destPipe, destInventory, speed -> new PipeItem(extracted, speed))) {
handler.extractItem(slot, extracted.getCount(), false);
return true;
}
}
return false;
}
public PipeTileEntity getPipe(BlockPos pos) { public PipeTileEntity getPipe(BlockPos pos) {
PipeTileEntity tile = this.tileCache.get(pos); PipeTileEntity tile = this.tileCache.get(pos);
if (tile == null || tile.isRemoved()) { if (tile == null || tile.isRemoved()) {
@ -223,10 +247,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return this.networkLocks.get(pos); return this.networkLocks.get(pos);
} }
public int getLockedAmount(BlockPos pos, int slot) { public int getLockedAmount(BlockPos pos, ItemStack stack, ItemEqualityType... equalityTypes) {
return this.getNetworkLocks(pos).stream() return this.getNetworkLocks(pos).stream()
.filter(l -> l.slot == slot) .filter(l -> ItemEqualityType.compareItems(l.stack, stack, equalityTypes))
.mapToInt(l -> l.amount).sum(); .mapToInt(l -> l.stack.getCount()).sum();
} }
private void refreshNode(BlockPos pos, BlockState state) { private void refreshNode(BlockPos pos, BlockState state) {

View file

@ -4,6 +4,7 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.PipeItem; import de.ellpeck.prettypipes.network.PipeItem;
@ -39,7 +40,6 @@ public class RetrievalModuleItem extends ModuleItem {
if (!tile.canWork()) if (!tile.canWork())
return; return;
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
List<NetworkLocation> locations = null;
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
filter.isWhitelist = true; filter.isWhitelist = true;
@ -53,22 +53,8 @@ public class RetrievalModuleItem extends ModuleItem {
BlockPos dest = tile.getAvailableDestination(copy, true, this.preventOversending); BlockPos dest = tile.getAvailableDestination(copy, true, this.preventOversending);
if (dest == null) if (dest == null)
continue; continue;
// loop through locations to find a location that has the item if (network.requestItem(tile.getPos(), dest, filtered, this.maxExtraction, filter.getEqualityTypes()))
if (locations == null) break;
locations = network.getOrderedNetworkItems(tile.getPos());
for (NetworkLocation location : locations) {
if (location.pipePos.equals(tile.getPos()))
continue;
for (int slot : location.getStackSlots(tile.getWorld(), filtered, filter.getEqualityTypes())) {
// try to extract from that location's inventory and send the item
IItemHandler handler = location.getItemHandler(tile.getWorld());
ItemStack stack = handler.extractItem(slot, this.maxExtraction, true);
if (network.routeItemToLocation(location.pipePos, location.getPos(), tile.getPos(), dest, speed -> new PipeItem(stack, speed))) {
handler.extractItem(slot, stack.getCount(), false);
return;
}
}
}
} }
} }

View file

@ -118,13 +118,11 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
int available = 0; int available = 0;
if (item != null) { if (item != null) {
for (NetworkLocation location : item.getLocations()) { for (NetworkLocation location : item.getLocations()) {
for (int slot : location.getStackSlots(this.world, stack.stack, ItemEqualityType.NBT)) { int amount = location.getItemAmount(this.world, stack.stack, ItemEqualityType.NBT);
ItemStack inSlot = location.getItemHandler(this.world).extractItem(slot, Integer.MAX_VALUE, true); if (amount <= 0)
if (inSlot.isEmpty())
continue; continue;
inSlot.shrink(network.getLockedAmount(location.getPos(), slot)); amount -= network.getLockedAmount(location.getPos(), stack.stack, ItemEqualityType.NBT);
available += inSlot.getCount(); available += amount;
}
} }
// divide the total by the amount required to get the amount that // divide the total by the amount required to get the amount that
// we have available for each crafting slot that contains this item // we have available for each crafting slot that contains this item

View file

@ -84,14 +84,10 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
if (!this.pendingRequests.isEmpty()) { if (!this.pendingRequests.isEmpty()) {
NetworkLock request = this.pendingRequests.remove(); NetworkLock request = this.pendingRequests.remove();
network.resolveNetworkLock(request); network.resolveNetworkLock(request);
IItemHandler handler = request.location.getItemHandler(this.world); if (network.requestItem(request.location, pipe.getPos(), this.pos, request.stack, request.stack.getCount(), ItemEqualityType.NBT))
ItemStack extracted = handler.extractItem(request.slot, request.amount, true);
if (network.routeItemToLocation(request.location.pipePos, request.location.getPos(), pipe.getPos(), this.pos, speed -> new PipeItem(extracted, speed))) {
handler.extractItem(request.slot, extracted.getCount(), false);
update = true; update = true;
} }
} }
}
if (this.world.getGameTime() % 100 == 0 || update) { if (this.world.getGameTime() % 100 == 0 || update) {
PlayerEntity[] lookingPlayers = this.getLookingPlayers(); PlayerEntity[] lookingPlayers = this.getLookingPlayers();
@ -154,22 +150,20 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
NetworkItem item = this.networkItems.get(equatable); NetworkItem item = this.networkItems.get(equatable);
if (item != null) { if (item != null) {
int remain = stack.getCount(); int remain = stack.getCount();
locations:
for (NetworkLocation location : item.getLocations()) { for (NetworkLocation location : item.getLocations()) {
for (int slot : location.getStackSlots(this.world, stack, ItemEqualityType.NBT)) { int amount = location.getItemAmount(this.world, stack, ItemEqualityType.NBT);
ItemStack inSlot = location.getItemHandler(this.world).extractItem(slot, Integer.MAX_VALUE, true); if (amount <= 0)
if (inSlot.isEmpty())
continue; continue;
inSlot.shrink(network.getLockedAmount(location.getPos(), slot)); amount -= network.getLockedAmount(location.getPos(), stack, ItemEqualityType.NBT);
if (inSlot.getCount() > 0) { if (amount > 0) {
int extract = Math.min(inSlot.getCount(), remain); if (remain < amount)
NetworkLock lock = new NetworkLock(location, slot, extract); amount = remain;
NetworkLock lock = new NetworkLock(location, stack);
this.pendingRequests.add(lock); this.pendingRequests.add(lock);
network.createNetworkLock(lock); network.createNetworkLock(lock);
remain -= extract; remain -= amount;
if (remain <= 0) if (remain <= 0)
break locations; break;
}
} }
} }
return stack.getCount() - remain; return stack.getCount() - remain;