Rename nbt method to remove confusion about the object being mutable or not

This commit is contained in:
TheMode 2021-04-10 00:24:29 +02:00
parent 64e70c3b64
commit 5e8e7cbaf4
5 changed files with 18 additions and 10 deletions

View File

@ -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().nbt();
NBTCompound compound = itemStack.getMeta().toNBT();
obj.add("tag", new JsonPrimitive(compound.toSNBT()));
return new ChatHoverEvent("show_item", obj);

View File

@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -28,6 +29,8 @@ public class ItemMeta {
private final NBTCompound nbt;
private final ItemMetaBuilder emptyBuilder;
private SoftReference<NBTCompound> nbtCache;
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
this.damage = metaBuilder.damage;
this.unbreakable = metaBuilder.unbreakable;
@ -92,18 +95,23 @@ public class ItemMeta {
public <T> T getOrDefault(@NotNull ItemTag<T> tag, @Nullable T defaultValue) {
var key = tag.getKey();
if (nbt.containsKey(key)) {
return tag.read(nbt);
return tag.read(toNBT());
} else {
return defaultValue;
}
}
public <T> @Nullable T get(@NotNull ItemTag<T> tag) {
return tag.read(nbt);
return tag.read(toNBT());
}
public @NotNull NBTCompound nbt() {
return nbt;
public @NotNull NBTCompound toNBT() {
NBTCompound cache = nbtCache.get();
if (cache == null) {
cache = nbt.deepClone();
nbtCache = new SoftReference<>(cache);
}
return cache;
}
@Override
@ -112,7 +120,7 @@ public class ItemMeta {
if (o == null || getClass() != o.getClass()) return false;
ItemMeta itemMeta = (ItemMeta) o;
return nbt.equals(itemMeta.nbt());
return nbt.equals(itemMeta.nbt);
}
@Override

View File

@ -193,7 +193,7 @@ public class CrossbowMeta extends ItemMeta {
@NotNull
private NBTCompound getItemCompound(@NotNull ItemStack itemStack) {
NBTCompound compound = itemStack.getMeta().nbt();
NBTCompound compound = itemStack.getMeta().toNBT();
compound.setByte("Count", (byte) itemStack.getAmount());
compound.setString("id", itemStack.getMaterial().getName());
return compound;

View File

@ -88,7 +88,7 @@ public final class NBTUtils {
final ItemStack stack = inventory.getItemStack(i);
NBTCompound nbt = new NBTCompound();
NBTCompound tag = stack.getMeta().nbt();
NBTCompound tag = stack.getMeta().toNBT();
nbt.set("tag", tag);
nbt.setByte("Slot", (byte) i);
@ -288,7 +288,7 @@ public final class NBTUtils {
packet.writeVarInt(itemStack.getMaterial().getId());
packet.writeByte((byte) itemStack.getAmount());
packet.writeNBT("", itemStack.getMeta().nbt());
packet.writeNBT("", itemStack.getMeta().toNBT());
}
}

View File

@ -100,7 +100,7 @@ public class BiomeParticles {
@Override
public NBTCompound toNbt() {
//todo test count might be wrong type
NBTCompound nbtCompound = item.getMeta().nbt();
NBTCompound nbtCompound = item.getMeta().toNBT();
nbtCompound.setString("type", type);
return nbtCompound;
}