mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 00:17:58 +01:00
Rename ItemMeta#nbt, remove unnecessary cache
This commit is contained in:
parent
e3867c659b
commit
e4a26df2b6
@ -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().toNBT();
|
||||
NBTCompound compound = itemStack.getMeta().nbt();
|
||||
obj.add("tag", new JsonPrimitive(compound.toSNBT()));
|
||||
|
||||
return new ChatHoverEvent("show_item", obj);
|
||||
|
@ -8,14 +8,14 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ItemMeta {
|
||||
|
||||
private final ItemMetaBuilder builder;
|
||||
|
||||
private final int damage;
|
||||
private final boolean unbreakable;
|
||||
private final int hideFlag;
|
||||
@ -27,22 +27,26 @@ public class ItemMeta {
|
||||
|
||||
private final int customModelData;
|
||||
|
||||
private final @Nullable NBTCompound originalNbt;
|
||||
private SoftReference<NBTCompound> cache;
|
||||
private final NBTCompound nbt;
|
||||
private final ItemMetaBuilder emptyBuilder;
|
||||
|
||||
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
this.builder = metaBuilder.clone();
|
||||
this.damage = 0;
|
||||
this.unbreakable = false;
|
||||
this.hideFlag = 0;
|
||||
this.damage = metaBuilder.damage;
|
||||
this.unbreakable = metaBuilder.unbreakable;
|
||||
this.hideFlag = metaBuilder.hideFlag;
|
||||
this.displayName = metaBuilder.displayName;
|
||||
this.lore = Collections.unmodifiableList(metaBuilder.lore);
|
||||
this.enchantmentMap = Collections.unmodifiableMap(metaBuilder.enchantmentMap);
|
||||
this.attributes = new ArrayList<>();
|
||||
this.customModelData = 0;
|
||||
this.attributes = Collections.unmodifiableList(metaBuilder.attributes);
|
||||
this.customModelData = metaBuilder.customModelData;
|
||||
|
||||
// Can be null
|
||||
this.originalNbt = metaBuilder.originalNBT;
|
||||
// nbt
|
||||
{
|
||||
this.nbt = Objects.requireNonNullElseGet(metaBuilder.originalNBT, NBTCompound::new);
|
||||
NBTUtils.writeMetaNBT(this, nbt);
|
||||
|
||||
this.emptyBuilder = metaBuilder.getSupplier().get();
|
||||
}
|
||||
}
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
@ -93,28 +97,19 @@ public class ItemMeta {
|
||||
}
|
||||
|
||||
public <T> T getOrDefault(@NotNull ItemTag<T> tag, @Nullable T defaultValue) {
|
||||
var nbt = toNBT();
|
||||
var key = tag.getKey();
|
||||
if (nbt.containsKey(key)) {
|
||||
return tag.read(toNBT());
|
||||
return tag.read(nbt);
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> @Nullable T get(@NotNull ItemTag<T> tag) {
|
||||
return tag.read(toNBT());
|
||||
return tag.read(nbt);
|
||||
}
|
||||
|
||||
public @NotNull NBTCompound toNBT() {
|
||||
var nbt = cache != null ? cache.get() : null;
|
||||
if (nbt == null) {
|
||||
nbt = Objects.requireNonNullElseGet(originalNbt, NBTCompound::new);
|
||||
NBTUtils.writeMetaNBT(this, nbt);
|
||||
this.builder.write(nbt);
|
||||
this.cache = new SoftReference<>(nbt);
|
||||
}
|
||||
|
||||
public @NotNull NBTCompound nbt() {
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@ -124,16 +119,16 @@ public class ItemMeta {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ItemMeta itemMeta = (ItemMeta) o;
|
||||
return toNBT().equals(itemMeta.toNBT());
|
||||
return nbt.equals(itemMeta.nbt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toNBT().hashCode();
|
||||
return nbt.hashCode();
|
||||
}
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
protected @NotNull ItemMetaBuilder builder() {
|
||||
return ItemMetaBuilder.fromNBT(builder, toNBT());
|
||||
return ItemMetaBuilder.fromNBT(emptyBuilder, nbt);
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
||||
return this;
|
||||
} else {
|
||||
// Create item meta based on nbt
|
||||
var currentNbt = build().toNBT();
|
||||
var currentNbt = build().nbt();
|
||||
return fromNBT(this, currentNbt).set(tag, value);
|
||||
}
|
||||
}
|
||||
@ -122,8 +122,6 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
||||
|
||||
public abstract void write(@NotNull NBTCompound nbtCompound);
|
||||
|
||||
protected abstract void deepClone(@NotNull ItemMetaBuilder metaBuilder);
|
||||
|
||||
protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier();
|
||||
|
||||
@Contract(value = "_, _ -> new", pure = true)
|
||||
@ -137,11 +135,10 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
||||
@Override
|
||||
protected ItemMetaBuilder clone() {
|
||||
try {
|
||||
NBTCompound nbtCompound = new NBTCompound();
|
||||
write(nbtCompound);
|
||||
var builder = (ItemMetaBuilder) super.clone();
|
||||
builder.displayName = displayName;
|
||||
builder.lore = new ArrayList<>(lore);
|
||||
builder.enchantmentMap = new HashMap<>(enchantmentMap);
|
||||
deepClone(builder);
|
||||
builder.read(nbtCompound);
|
||||
return builder;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
// Should never happen, because ItemMetaBuilder implements Cloneable
|
||||
|
@ -97,14 +97,6 @@ public class CompassMeta extends ItemMeta implements ItemMetaBuilder.Provider<Co
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deepClone(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
var compassBuilder = (CompassMeta.Builder) metaBuilder;
|
||||
compassBuilder.lodestoneTracked = lodestoneTracked;
|
||||
compassBuilder.lodestoneDimension = lodestoneDimension;
|
||||
compassBuilder.lodestonePosition = lodestonePosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull Supplier<ItemMetaBuilder> getSupplier() {
|
||||
return Builder::new;
|
||||
|
@ -188,15 +188,6 @@ public class MapMeta extends ItemMeta {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deepClone(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
var mapBuilder = (MapMeta.Builder) metaBuilder;
|
||||
mapBuilder.mapId = mapId;
|
||||
mapBuilder.mapScaleDirection = mapScaleDirection;
|
||||
mapBuilder.decorations = decorations;
|
||||
mapBuilder.mapColor = mapColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() {
|
||||
return Builder::new;
|
||||
|
@ -286,7 +286,7 @@ public final class NBTUtils {
|
||||
packet.writeVarInt(itemStack.getMaterial().getId());
|
||||
packet.writeByte((byte) itemStack.getAmount());
|
||||
|
||||
packet.writeNBT("", itemStack.getMeta().toNBT());
|
||||
packet.writeNBT("", itemStack.getMeta().nbt());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class BiomeParticles {
|
||||
@Override
|
||||
public NBTCompound toNbt() {
|
||||
//todo test count might be wrong type
|
||||
NBTCompound nbtCompound = item.getMeta().toNBT();
|
||||
NBTCompound nbtCompound = item.getMeta().nbt();
|
||||
nbtCompound.setString("type", type);
|
||||
return nbtCompound;
|
||||
}
|
||||
|
@ -37,8 +37,8 @@ import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
import net.minestom.server.world.DimensionType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
@ -243,10 +243,9 @@ public class PlayerInit {
|
||||
.displayName(Component.text("test"))
|
||||
.lore(Component.text("lore"))
|
||||
.build();
|
||||
inventory.setChestplate(item.withLore(components -> {
|
||||
var list = new ArrayList<>(components);
|
||||
list.add(Component.text("hey"));
|
||||
return list;
|
||||
inventory.setChestplate(item.with(itemStackBuilder -> {
|
||||
itemStackBuilder.lore(Collections.emptyList())
|
||||
.displayName(null);
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user