From e4a26df2b6445a0b0051362fa7179c0e16d8a90d Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 9 Apr 2021 23:16:57 +0200 Subject: [PATCH] Rename ItemMeta#nbt, remove unnecessary cache --- .../minestom/server/chat/ChatHoverEvent.java | 2 +- .../net/minestom/server/item/ItemMeta.java | 53 +++++++++---------- .../minestom/server/item/ItemMetaBuilder.java | 11 ++-- .../server/item/meta/CompassMeta.java | 8 --- .../minestom/server/item/meta/MapMeta.java | 9 ---- .../net/minestom/server/utils/NBTUtils.java | 2 +- .../server/world/biomes/BiomeParticles.java | 2 +- src/test/java/demo/PlayerInit.java | 9 ++-- 8 files changed, 35 insertions(+), 61 deletions(-) diff --git a/src/main/java/net/minestom/server/chat/ChatHoverEvent.java b/src/main/java/net/minestom/server/chat/ChatHoverEvent.java index 162447c38..a1e4dbd00 100644 --- a/src/main/java/net/minestom/server/chat/ChatHoverEvent.java +++ b/src/main/java/net/minestom/server/chat/ChatHoverEvent.java @@ -93,7 +93,7 @@ public class ChatHoverEvent { JsonObject obj = GsonComponentSerializer.gson().serializer().toJsonTree(Component.empty().hoverEvent(event)).getAsJsonObject(); obj = obj.get("hoverEvent").getAsJsonObject().get("contents").getAsJsonObject(); - NBTCompound compound = itemStack.getMeta().toNBT(); + NBTCompound compound = itemStack.getMeta().nbt(); obj.add("tag", new JsonPrimitive(compound.toSNBT())); return new ChatHoverEvent("show_item", obj); diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index 1755d6f42..f155a1135 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -8,14 +8,14 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; -import java.lang.ref.SoftReference; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.function.Consumer; public class ItemMeta { - private final ItemMetaBuilder builder; - private final int damage; private final boolean unbreakable; private final int hideFlag; @@ -27,22 +27,26 @@ public class ItemMeta { private final int customModelData; - private final @Nullable NBTCompound originalNbt; - private SoftReference cache; + private final NBTCompound nbt; + private final ItemMetaBuilder emptyBuilder; protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) { - this.builder = metaBuilder.clone(); - this.damage = 0; - this.unbreakable = false; - this.hideFlag = 0; + this.damage = metaBuilder.damage; + this.unbreakable = metaBuilder.unbreakable; + this.hideFlag = metaBuilder.hideFlag; this.displayName = metaBuilder.displayName; this.lore = Collections.unmodifiableList(metaBuilder.lore); this.enchantmentMap = Collections.unmodifiableMap(metaBuilder.enchantmentMap); - this.attributes = new ArrayList<>(); - this.customModelData = 0; + this.attributes = Collections.unmodifiableList(metaBuilder.attributes); + this.customModelData = metaBuilder.customModelData; - // Can be null - this.originalNbt = metaBuilder.originalNBT; + // nbt + { + this.nbt = Objects.requireNonNullElseGet(metaBuilder.originalNBT, NBTCompound::new); + NBTUtils.writeMetaNBT(this, nbt); + + this.emptyBuilder = metaBuilder.getSupplier().get(); + } } @Contract(value = "_, -> new", pure = true) @@ -93,28 +97,19 @@ public class ItemMeta { } public T getOrDefault(@NotNull ItemTag tag, @Nullable T defaultValue) { - var nbt = toNBT(); var key = tag.getKey(); if (nbt.containsKey(key)) { - return tag.read(toNBT()); + return tag.read(nbt); } else { return defaultValue; } } public @Nullable T get(@NotNull ItemTag tag) { - return tag.read(toNBT()); + return tag.read(nbt); } - public @NotNull NBTCompound toNBT() { - var nbt = cache != null ? cache.get() : null; - if (nbt == null) { - nbt = Objects.requireNonNullElseGet(originalNbt, NBTCompound::new); - NBTUtils.writeMetaNBT(this, nbt); - this.builder.write(nbt); - this.cache = new SoftReference<>(nbt); - } - + public @NotNull NBTCompound nbt() { return nbt; } @@ -124,16 +119,16 @@ public class ItemMeta { if (o == null || getClass() != o.getClass()) return false; ItemMeta itemMeta = (ItemMeta) o; - return toNBT().equals(itemMeta.toNBT()); + return nbt.equals(itemMeta.nbt()); } @Override public int hashCode() { - return toNBT().hashCode(); + return nbt.hashCode(); } @Contract(value = "-> new", pure = true) protected @NotNull ItemMetaBuilder builder() { - return ItemMetaBuilder.fromNBT(builder, toNBT()); + return ItemMetaBuilder.fromNBT(emptyBuilder, nbt); } } diff --git a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java index 49516ab37..487a03b4c 100644 --- a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java +++ b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java @@ -110,7 +110,7 @@ public abstract class ItemMetaBuilder implements Cloneable { return this; } else { // Create item meta based on nbt - var currentNbt = build().toNBT(); + var currentNbt = build().nbt(); return fromNBT(this, currentNbt).set(tag, value); } } @@ -122,8 +122,6 @@ public abstract class ItemMetaBuilder implements Cloneable { public abstract void write(@NotNull NBTCompound nbtCompound); - protected abstract void deepClone(@NotNull ItemMetaBuilder metaBuilder); - protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier(); @Contract(value = "_, _ -> new", pure = true) @@ -137,11 +135,10 @@ public abstract class ItemMetaBuilder implements Cloneable { @Override protected ItemMetaBuilder clone() { try { + NBTCompound nbtCompound = new NBTCompound(); + write(nbtCompound); var builder = (ItemMetaBuilder) super.clone(); - builder.displayName = displayName; - builder.lore = new ArrayList<>(lore); - builder.enchantmentMap = new HashMap<>(enchantmentMap); - deepClone(builder); + builder.read(nbtCompound); return builder; } catch (CloneNotSupportedException e) { // Should never happen, because ItemMetaBuilder implements Cloneable diff --git a/src/main/java/net/minestom/server/item/meta/CompassMeta.java b/src/main/java/net/minestom/server/item/meta/CompassMeta.java index 0007e2475..856f9df30 100644 --- a/src/main/java/net/minestom/server/item/meta/CompassMeta.java +++ b/src/main/java/net/minestom/server/item/meta/CompassMeta.java @@ -97,14 +97,6 @@ public class CompassMeta extends ItemMeta implements ItemMetaBuilder.Provider getSupplier() { return Builder::new; diff --git a/src/main/java/net/minestom/server/item/meta/MapMeta.java b/src/main/java/net/minestom/server/item/meta/MapMeta.java index e67d05e8e..73651fa4d 100644 --- a/src/main/java/net/minestom/server/item/meta/MapMeta.java +++ b/src/main/java/net/minestom/server/item/meta/MapMeta.java @@ -188,15 +188,6 @@ public class MapMeta extends ItemMeta { } } - @Override - protected void deepClone(@NotNull ItemMetaBuilder metaBuilder) { - var mapBuilder = (MapMeta.Builder) metaBuilder; - mapBuilder.mapId = mapId; - mapBuilder.mapScaleDirection = mapScaleDirection; - mapBuilder.decorations = decorations; - mapBuilder.mapColor = mapColor; - } - @Override protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() { return Builder::new; diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 23048a591..22b851dc8 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -286,7 +286,7 @@ public final class NBTUtils { packet.writeVarInt(itemStack.getMaterial().getId()); packet.writeByte((byte) itemStack.getAmount()); - packet.writeNBT("", itemStack.getMeta().toNBT()); + packet.writeNBT("", itemStack.getMeta().nbt()); } } diff --git a/src/main/java/net/minestom/server/world/biomes/BiomeParticles.java b/src/main/java/net/minestom/server/world/biomes/BiomeParticles.java index 721e89790..5ab2ec118 100644 --- a/src/main/java/net/minestom/server/world/biomes/BiomeParticles.java +++ b/src/main/java/net/minestom/server/world/biomes/BiomeParticles.java @@ -100,7 +100,7 @@ public class BiomeParticles { @Override public NBTCompound toNbt() { //todo test count might be wrong type - NBTCompound nbtCompound = item.getMeta().toNBT(); + NBTCompound nbtCompound = item.getMeta().nbt(); nbtCompound.setString("type", type); return nbtCompound; } diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index f066d5072..bee771b8e 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -37,8 +37,8 @@ import net.minestom.server.utils.Vector; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.world.DimensionType; -import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; @@ -243,10 +243,9 @@ public class PlayerInit { .displayName(Component.text("test")) .lore(Component.text("lore")) .build(); - inventory.setChestplate(item.withLore(components -> { - var list = new ArrayList<>(components); - list.add(Component.text("hey")); - return list; + inventory.setChestplate(item.with(itemStackBuilder -> { + itemStackBuilder.lore(Collections.emptyList()) + .displayName(null); })); } });