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(); JsonObject obj = GsonComponentSerializer.gson().serializer().toJsonTree(Component.empty().hoverEvent(event)).getAsJsonObject();
obj = obj.get("hoverEvent").getAsJsonObject().get("contents").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())); obj.add("tag", new JsonPrimitive(compound.toSNBT()));
return new ChatHoverEvent("show_item", obj); return new ChatHoverEvent("show_item", obj);

View File

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

View File

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

View File

@ -88,7 +88,7 @@ public final class NBTUtils {
final ItemStack stack = inventory.getItemStack(i); final ItemStack stack = inventory.getItemStack(i);
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
NBTCompound tag = stack.getMeta().nbt(); NBTCompound tag = stack.getMeta().toNBT();
nbt.set("tag", tag); nbt.set("tag", tag);
nbt.setByte("Slot", (byte) i); nbt.setByte("Slot", (byte) i);
@ -288,7 +288,7 @@ public final class NBTUtils {
packet.writeVarInt(itemStack.getMaterial().getId()); packet.writeVarInt(itemStack.getMaterial().getId());
packet.writeByte((byte) itemStack.getAmount()); 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 @Override
public NBTCompound toNbt() { public NBTCompound toNbt() {
//todo test count might be wrong type //todo test count might be wrong type
NBTCompound nbtCompound = item.getMeta().nbt(); NBTCompound nbtCompound = item.getMeta().toNBT();
nbtCompound.setString("type", type); nbtCompound.setString("type", type);
return nbtCompound; return nbtCompound;
} }