From d1f8e60a7e3c0dc1304570ebd5bf4fe6d47c2579 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 10 Apr 2021 00:55:18 +0200 Subject: [PATCH] Cache meta binary --- .../java/net/minestom/server/item/ItemMeta.java | 17 ++++++++++++++++- .../net/minestom/server/utils/NBTUtils.java | 13 ------------- .../server/utils/binary/BinaryWriter.java | 10 ++++++++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index 1560decc7..7c02be9de 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -2,6 +2,8 @@ package net.minestom.server.item; import net.kyori.adventure.text.Component; import net.minestom.server.item.attribute.ItemAttribute; +import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -12,7 +14,7 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; -public class ItemMeta { +public class ItemMeta implements Writeable { private final int damage; private final boolean unbreakable; @@ -28,6 +30,8 @@ public class ItemMeta { private final NBTCompound nbt; private final ItemMetaBuilder emptyBuilder; + private BinaryWriter cachedBuffer; + protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) { this.damage = metaBuilder.damage; this.unbreakable = metaBuilder.unbreakable; @@ -124,4 +128,15 @@ public class ItemMeta { protected @NotNull ItemMetaBuilder builder() { return ItemMetaBuilder.fromNBT(emptyBuilder, nbt); } + + @Override + public synchronized void write(@NotNull BinaryWriter writer) { + if (cachedBuffer == null) { + BinaryWriter w = new BinaryWriter(); + w.writeNBT("", nbt); + this.cachedBuffer = w; + } + writer.write(cachedBuffer); + cachedBuffer.getBuffer().resetReaderIndex(); + } } diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 2d640de38..87b25e519 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -16,7 +16,6 @@ import net.minestom.server.item.attribute.AttributeSlot; import net.minestom.server.item.attribute.ItemAttribute; import net.minestom.server.registry.Registries; import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -280,18 +279,6 @@ public final class NBTUtils { } } - public static void writeItemStack(@NotNull BinaryWriter packet, @NotNull ItemStack itemStack) { - if (itemStack.isAir()) { - packet.writeBoolean(false); - } else { - packet.writeBoolean(true); - packet.writeVarInt(itemStack.getMaterial().getId()); - packet.writeByte((byte) itemStack.getAmount()); - - packet.writeNBT("", itemStack.getMeta().toNBT()); - } - } - @FunctionalInterface public interface EnchantmentSetter { void applyEnchantment(Enchantment name, short level); diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java index 152f65268..2f6edee3d 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java @@ -9,7 +9,6 @@ import net.minestom.server.adventure.AdventureSerializer; import net.minestom.server.chat.JsonMessage; import net.minestom.server.item.ItemStack; import net.minestom.server.utils.BlockPosition; -import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.SerializerUtils; import net.minestom.server.utils.Utils; import org.jetbrains.annotations.NotNull; @@ -264,7 +263,14 @@ public class BinaryWriter extends OutputStream { } public void writeItemStack(@NotNull ItemStack itemStack) { - NBTUtils.writeItemStack(this, itemStack); + if (itemStack.isAir()) { + writeBoolean(false); + } else { + writeBoolean(true); + writeVarInt(itemStack.getMaterial().getId()); + writeByte((byte) itemStack.getAmount()); + write(itemStack.getMeta()); + } } public void writeNBT(@NotNull String name, @NotNull NBT tag) {