From 0228932472b7484d11b972aa319544bf9369a3f8 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 30 Mar 2022 14:16:52 +0200 Subject: [PATCH] hash item stack nbt to avoid going over the packet size limit Closes #120 --- build.gradle | 2 +- .../de/ellpeck/prettypipes/packets/PacketRequest.java | 9 ++++++--- .../prettypipes/terminal/ItemTerminalBlockEntity.java | 10 +++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 6b5012d..dba2ff8 100644 --- a/build.gradle +++ b/build.gradle @@ -105,7 +105,7 @@ configurations { } dependencies { - minecraft 'net.minecraftforge:forge:1.18.2-40.0.3' + minecraft 'net.minecraftforge:forge:1.18.2-40.0.32' embed 'org.jgrapht:jgrapht-core:1.5.1' compileOnly fg.deobf("mezz.jei:jei-1.18.2:9.5.5.174:api") diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketRequest.java index 0ec142f..c2088f1 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,14 +32,15 @@ public class PacketRequest { var packet = new PacketRequest(); packet.pos = buf.readBlockPos(); packet.stack = buf.readItem(); + packet.nbtHash = buf.readVarInt(); packet.amount = buf.readVarInt(); return packet; } public static void toBytes(PacketRequest packet, FriendlyByteBuf buf) { buf.writeBlockPos(packet.pos); - // if we limit the tag here, non-shared data will be omitted, making the requested item not match the stored one - buf.writeItemStack(packet.stack, false); + buf.writeItem(packet.stack); + buf.writeVarInt(packet.nbtHash); buf.writeVarInt(packet.amount); } @@ -49,7 +52,7 @@ public class PacketRequest { Player player = ctx.get().getSender(); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level, 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/ItemTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java index d6132a0..b7d1a1b 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java @@ -151,10 +151,18 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect } } - public void requestItem(Player player, ItemStack stack) { + public void requestItem(Player player, ItemStack stack, int nbtHash) { var network = PipeNetwork.get(this.level); network.startProfile("terminal_request_item"); this.updateItems(); + if (nbtHash != 0) { + var 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); + } var requested = this.requestItemImpl(stack, onItemUnavailable(player)); if (requested > 0) { player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getHoverName()).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GREEN)), UUID.randomUUID());