hash item stack nbt to avoid going over the packet size limit

Closes #120
This commit is contained in:
Ell 2022-03-30 14:16:52 +02:00
parent 9ae2aea70c
commit 0228932472
3 changed files with 16 additions and 5 deletions

View file

@ -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")

View file

@ -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);

View file

@ -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());