From 731f5716452e8a71e32d62344188eaebf2a212e5 Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 20 Dec 2021 20:15:26 +0100 Subject: [PATCH] Improve item creation from nbt --- .../java/net/minestom/server/item/ItemMeta.java | 4 +++- .../minestom/server/item/ItemMetaBuilder.java | 15 +++++++-------- .../net/minestom/server/item/ItemStack.java | 9 +++------ .../minestom/server/item/ItemStackBuilder.java | 17 ++++++++++------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index a7392b723..686eccfe6 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -152,7 +152,9 @@ public class ItemMeta implements TagReadable, Writeable { @Contract(value = "-> new", pure = true) protected @NotNull ItemMetaBuilder builder() { - return ItemMetaBuilder.fromNBT(metaBuilder, nbt); + ItemMetaBuilder result = metaBuilder.getSupplier().get(); + ItemMetaBuilder.resetMeta(result, nbt); + return result; } @Override diff --git a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java index 2122626ef..78dcebb61 100644 --- a/src/main/java/net/minestom/server/item/ItemMetaBuilder.java +++ b/src/main/java/net/minestom/server/item/ItemMetaBuilder.java @@ -11,6 +11,7 @@ import net.minestom.server.tag.Tag; import net.minestom.server.tag.TagWritable; import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.Utils; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -258,16 +259,14 @@ public abstract class ItemMetaBuilder implements TagWritable { }); } - @Contract(value = "_, _ -> new", pure = true) - public static @NotNull ItemMetaBuilder fromNBT(@NotNull ItemMetaBuilder src, @NotNull NBTCompound nbtCompound) { - ItemMetaBuilder dest = src.getSupplier().get(); - dest.nbt = nbtCompound.toMutableCompound(); - appendDefaultMeta(dest, nbtCompound); - return dest; + @ApiStatus.Internal + public static void resetMeta(@NotNull ItemMetaBuilder src, @NotNull NBTCompound nbtCompound) { + src.nbt = nbtCompound.toMutableCompound(); + appendMeta(src, nbtCompound); } - private static void appendDefaultMeta(@NotNull ItemMetaBuilder metaBuilder, - @NotNull NBTCompound nbt) { + private static void appendMeta(@NotNull ItemMetaBuilder metaBuilder, + @NotNull NBTCompound nbt) { if (nbt.get("Damage") instanceof NBTInt damage) metaBuilder.damage = damage.getValue(); if (nbt.get("Unbreakable") instanceof NBTByte unbreakable) metaBuilder.unbreakable = unbreakable.asBoolean(); if (nbt.get("HideFlags") instanceof NBTInt hideFlags) metaBuilder.hideFlag = hideFlags.getValue(); diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index 322a45e8a..5e30715e1 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -69,12 +69,9 @@ public final class ItemStack implements TagReadable, HoverEventSource ItemMetaBuilder.fromNBT(metaBuilder, nbtCompound)); - } - return itemBuilder.build(); + ItemMetaBuilder builder = ItemStackBuilder.getMetaBuilder(material); + if (nbtCompound != null) ItemMetaBuilder.resetMeta(builder, nbtCompound); + return new ItemStack(material, amount, builder.build(), null); } @Contract(value = "_, _ -> new", pure = true) diff --git a/src/main/java/net/minestom/server/item/ItemStackBuilder.java b/src/main/java/net/minestom/server/item/ItemStackBuilder.java index b663e4343..d45e6fff9 100644 --- a/src/main/java/net/minestom/server/item/ItemStackBuilder.java +++ b/src/main/java/net/minestom/server/item/ItemStackBuilder.java @@ -15,15 +15,14 @@ import java.util.function.Consumer; import java.util.function.Supplier; import java.util.function.UnaryOperator; -public class ItemStackBuilder { - +public final class ItemStackBuilder { private final Material material; private int amount; - protected ItemMetaBuilder metaBuilder; + private ItemMetaBuilder metaBuilder; private StackingRule stackingRule; - protected ItemStackBuilder(@NotNull Material material, @NotNull ItemMetaBuilder metaBuilder) { + ItemStackBuilder(@NotNull Material material, @NotNull ItemMetaBuilder metaBuilder) { this.material = material; this.amount = 1; this.metaBuilder = metaBuilder; @@ -55,9 +54,13 @@ public class ItemStackBuilder { MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_HORSE_ARMOR, LeatherArmorMeta.Builder::new); } - protected ItemStackBuilder(@NotNull Material material) { - this(material, - MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get()); + static ItemMetaBuilder getMetaBuilder(Material material) { + Supplier supplier = MATERIAL_SUPPLIER_MAP.get(material); + return supplier != null ? supplier.get() : new DefaultMeta(); + } + + ItemStackBuilder(@NotNull Material material) { + this(material, getMetaBuilder(material)); } @Contract(value = "_ -> this")