From 58f665452dff12d3967ceec16b94ffecd319087f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 30 Mar 2022 14:48:51 +0200 Subject: [PATCH] backport 0228932472b7484d11b972aa319544bf9369a3f8 --- .../de/ellpeck/prettypipes/packets/PacketRequest.java | 6 +++++- .../prettypipes/terminal/ItemTerminalTileEntity.java | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java index 36935be..0345fdb 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java @@ -14,11 +14,13 @@ public class PacketRequest { private BlockPos pos; private ItemStack stack; + private int nbtHash; private int amount; public PacketRequest(BlockPos pos, ItemStack stack, int amount) { this.pos = pos; this.stack = stack; + this.nbtHash = stack.getTag().hashCode(); this.amount = amount; } @@ -30,6 +32,7 @@ public class PacketRequest { PacketRequest packet = new PacketRequest(); packet.pos = buf.readBlockPos(); packet.stack = buf.readItemStack(); + packet.nbtHash = buf.readVarInt(); packet.amount = buf.readVarInt(); return packet; } @@ -37,6 +40,7 @@ public class PacketRequest { public static void toBytes(PacketRequest packet, PacketBuffer buf) { buf.writeBlockPos(packet.pos); buf.writeItemStack(packet.stack); + buf.writeVarInt(packet.nbtHash); buf.writeVarInt(packet.amount); } @@ -48,7 +52,7 @@ public class PacketRequest { PlayerEntity player = ctx.get().getSender(); ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, player.world, message.pos); message.stack.setCount(message.amount); - tile.requestItem(player, message.stack); + tile.requestItem(player, message.stack, message.nbtHash); } }); ctx.get().setPacketHandled(true); diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java index e58cad9..8d76cea 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalTileEntity.java @@ -154,10 +154,18 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine } } - public void requestItem(PlayerEntity player, ItemStack stack) { + public void requestItem(PlayerEntity player, ItemStack stack, int nbtHash) { PipeNetwork network = PipeNetwork.get(this.world); network.startProfile("terminal_request_item"); this.updateItems(); + if (nbtHash != 0) { + ItemStack filter = stack; + stack = this.networkItems.values().stream() + .map(NetworkItem::asStack) + // don't compare with nbt equality here or the whole hashing thing is pointless + .filter(s -> ItemEquality.compareItems(s, filter) && s.getTag().hashCode() == nbtHash) + .findFirst().orElse(filter); + } int requested = this.requestItemImpl(stack, onItemUnavailable(player)); if (requested > 0) { player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID());