mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 00:17:58 +01:00
Cache meta binary
This commit is contained in:
parent
f5b8f267d6
commit
d1f8e60a7e
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user