Item impl cleanup

This commit is contained in:
themode 2021-12-19 18:54:29 +01:00 committed by TheMode
parent c4190f544d
commit f1112fc865
10 changed files with 19 additions and 35 deletions

View File

@ -53,7 +53,7 @@ public class ItemMeta implements TagReadable, Writeable {
this.canPlaceOn = Set.copyOf(metaBuilder.canPlaceOn); this.canPlaceOn = Set.copyOf(metaBuilder.canPlaceOn);
this.metaBuilder = metaBuilder; this.metaBuilder = metaBuilder;
this.nbt = metaBuilder.nbt(); this.nbt = metaBuilder.nbt.toCompound();
} }
@Contract(value = "_, -> new", pure = true) @Contract(value = "_, -> new", pure = true)

View File

@ -15,17 +15,11 @@ import org.jglrxavpok.hephaistos.nbt.*;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound; import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
public abstract class ItemMetaBuilder implements TagWritable { public abstract class ItemMetaBuilder implements TagWritable {
MutableNBTCompound nbt = new MutableNBTCompound();
private static final AtomicReferenceFieldUpdater<ItemMetaBuilder, NBTCompound> NBT_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(ItemMetaBuilder.class, NBTCompound.class, "nbt");
protected volatile boolean built = false;
private volatile NBTCompound nbt = new NBTCompound();
protected int damage; protected int damage;
protected boolean unbreakable; protected boolean unbreakable;
@ -106,7 +100,7 @@ public abstract class ItemMetaBuilder implements TagWritable {
public @NotNull ItemMetaBuilder enchantments(@NotNull Map<Enchantment, Short> enchantments) { public @NotNull ItemMetaBuilder enchantments(@NotNull Map<Enchantment, Short> enchantments) {
this.enchantmentMap = new HashMap<>(enchantments); this.enchantmentMap = new HashMap<>(enchantments);
handleMap(enchantmentMap, "Enchantments", () -> { handleMap(enchantmentMap, "Enchantments", () -> {
MutableNBTCompound mutableCopy = new MutableNBTCompound(nbt); MutableNBTCompound mutableCopy = nbt.toMutableCompound();
NBTUtils.writeEnchant(mutableCopy, "Enchantments", enchantmentMap); NBTUtils.writeEnchant(mutableCopy, "Enchantments", enchantmentMap);
return mutableCopy.get("Enchantments"); return mutableCopy.get("Enchantments");
}); });
@ -205,24 +199,11 @@ public abstract class ItemMetaBuilder implements TagWritable {
protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier(); protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier();
protected synchronized void mutateNbt(Consumer<MutableNBTCompound> consumer) { protected void mutateNbt(Consumer<MutableNBTCompound> consumer) {
MutableNBTCompound copy = new MutableNBTCompound(nbt); consumer.accept(nbt);
consumer.accept(copy);
if (built) {
built = false;
final var currentNbt = nbt;
NBT_UPDATER.compareAndSet(this, currentNbt, copy.toCompound());
} else {
nbt = copy.toCompound();
}
}
protected synchronized NBTCompound nbt() {
return nbt;
} }
protected @NotNull ItemMeta generate() { protected @NotNull ItemMeta generate() {
this.built = true;
return build(); return build();
} }
@ -280,8 +261,8 @@ public abstract class ItemMetaBuilder implements TagWritable {
@Contract(value = "_, _ -> new", pure = true) @Contract(value = "_, _ -> new", pure = true)
public static @NotNull ItemMetaBuilder fromNBT(@NotNull ItemMetaBuilder src, @NotNull NBTCompound nbtCompound) { public static @NotNull ItemMetaBuilder fromNBT(@NotNull ItemMetaBuilder src, @NotNull NBTCompound nbtCompound) {
ItemMetaBuilder dest = src.getSupplier().get(); ItemMetaBuilder dest = src.getSupplier().get();
dest.nbt = nbtCompound; dest.nbt = nbtCompound.toMutableCompound();
NBTUtils.loadDataIntoMeta(dest, dest.nbt); NBTUtils.loadDataIntoMeta(dest, nbtCompound);
return dest; return dest;
} }

View File

@ -112,8 +112,7 @@ public class ItemStackBuilder {
@Contract(value = "-> new", pure = true) @Contract(value = "-> new", pure = true)
public @NotNull ItemStack build() { public @NotNull ItemStack build() {
if (amount < 1) if (amount < 1) return ItemStack.AIR;
return ItemStack.AIR;
return new ItemStack(material, amount, metaBuilder.generate(), stackingRule); return new ItemStack(material, amount, metaBuilder.generate(), stackingRule);
} }

View File

@ -14,7 +14,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class FireworkMeta extends ItemMeta implements ItemMetaBuilder.Provider<FireworkMeta.Builder> { public class FireworkMeta extends ItemMeta implements ItemMetaBuilder.Provider<FireworkMeta.Builder> {

View File

@ -16,7 +16,6 @@ import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<PotionMeta.Builder> { public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<PotionMeta.Builder> {

View File

@ -13,7 +13,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WritableBookMeta.Builder> { public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WritableBookMeta.Builder> {

View File

@ -14,7 +14,6 @@ import org.jglrxavpok.hephaistos.nbt.*;
import java.util.*; import java.util.*;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WrittenBookMeta.Builder> { public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WrittenBookMeta.Builder> {

View File

@ -19,8 +19,7 @@ public final class VanillaStackingRule implements StackingRule {
@Override @Override
public @NotNull ItemStack apply(@NotNull ItemStack item, int amount) { public @NotNull ItemStack apply(@NotNull ItemStack item, int amount) {
if (amount > 0) return item.withAmount(amount); return amount > 0 ? item.withAmount(amount) : ItemStack.AIR;
return ItemStack.AIR;
} }
@Override @Override

View File

@ -103,4 +103,14 @@ public class ItemTest {
enchantments = item.getMeta().getEnchantmentMap(); enchantments = item.getMeta().getEnchantmentMap();
assertEquals(enchantments.get(Enchantment.EFFICIENCY), (short) 10); assertEquals(enchantments.get(Enchantment.EFFICIENCY), (short) 10);
} }
@Test
public void testBuilderReuse() {
var builder = ItemStack.builder(Material.DIAMOND);
var item1 = builder.build();
var item2 = builder.displayName(Component.text("Name")).build();
assertNull(item1.getDisplayName());
assertNotNull(item2.getDisplayName());
assertNotEquals(item1, item2, "Item builder should be reusable");
}
} }

View File

@ -4,7 +4,6 @@ import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.item.ItemMetaBuilder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTShort;
import org.jglrxavpok.hephaistos.nbt.NBTString; import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound; import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;