Improve item creation from nbt

This commit is contained in:
themode 2021-12-20 20:15:26 +01:00 committed by TheMode
parent b8e6d95880
commit 731f571645
4 changed files with 23 additions and 22 deletions

View File

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

View File

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

View File

@ -69,12 +69,9 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
@Contract(value = "_, _, _ -> new", pure = true)
public static @NotNull ItemStack fromNBT(@NotNull Material material, @Nullable NBTCompound nbtCompound, int amount) {
var itemBuilder = ItemStack.builder(material)
.amount(amount);
if (nbtCompound != null) {
itemBuilder.meta(metaBuilder -> 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)

View File

@ -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<ItemMetaBuilder> supplier = MATERIAL_SUPPLIER_MAP.get(material);
return supplier != null ? supplier.get() : new DefaultMeta();
}
ItemStackBuilder(@NotNull Material material) {
this(material, getMetaBuilder(material));
}
@Contract(value = "_ -> this")