mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 17:08:30 +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();
|
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().toNBT();
|
NBTCompound compound = itemStack.getMeta().nbt();
|
||||||
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);
|
||||||
|
@ -8,14 +8,14 @@ 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.*;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class ItemMeta {
|
public class ItemMeta {
|
||||||
|
|
||||||
private final ItemMetaBuilder builder;
|
|
||||||
|
|
||||||
private final int damage;
|
private final int damage;
|
||||||
private final boolean unbreakable;
|
private final boolean unbreakable;
|
||||||
private final int hideFlag;
|
private final int hideFlag;
|
||||||
@ -27,22 +27,26 @@ public class ItemMeta {
|
|||||||
|
|
||||||
private final int customModelData;
|
private final int customModelData;
|
||||||
|
|
||||||
private final @Nullable NBTCompound originalNbt;
|
private final NBTCompound nbt;
|
||||||
private SoftReference<NBTCompound> cache;
|
private final ItemMetaBuilder emptyBuilder;
|
||||||
|
|
||||||
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
||||||
this.builder = metaBuilder.clone();
|
this.damage = metaBuilder.damage;
|
||||||
this.damage = 0;
|
this.unbreakable = metaBuilder.unbreakable;
|
||||||
this.unbreakable = false;
|
this.hideFlag = metaBuilder.hideFlag;
|
||||||
this.hideFlag = 0;
|
|
||||||
this.displayName = metaBuilder.displayName;
|
this.displayName = metaBuilder.displayName;
|
||||||
this.lore = Collections.unmodifiableList(metaBuilder.lore);
|
this.lore = Collections.unmodifiableList(metaBuilder.lore);
|
||||||
this.enchantmentMap = Collections.unmodifiableMap(metaBuilder.enchantmentMap);
|
this.enchantmentMap = Collections.unmodifiableMap(metaBuilder.enchantmentMap);
|
||||||
this.attributes = new ArrayList<>();
|
this.attributes = Collections.unmodifiableList(metaBuilder.attributes);
|
||||||
this.customModelData = 0;
|
this.customModelData = metaBuilder.customModelData;
|
||||||
|
|
||||||
// Can be null
|
// nbt
|
||||||
this.originalNbt = metaBuilder.originalNBT;
|
{
|
||||||
|
this.nbt = Objects.requireNonNullElseGet(metaBuilder.originalNBT, NBTCompound::new);
|
||||||
|
NBTUtils.writeMetaNBT(this, nbt);
|
||||||
|
|
||||||
|
this.emptyBuilder = metaBuilder.getSupplier().get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_, -> new", pure = true)
|
@Contract(value = "_, -> new", pure = true)
|
||||||
@ -93,28 +97,19 @@ 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 nbt = toNBT();
|
|
||||||
var key = tag.getKey();
|
var key = tag.getKey();
|
||||||
if (nbt.containsKey(key)) {
|
if (nbt.containsKey(key)) {
|
||||||
return tag.read(toNBT());
|
return tag.read(nbt);
|
||||||
} 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(toNBT());
|
return tag.read(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull NBTCompound toNBT() {
|
public @NotNull NBTCompound nbt() {
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return nbt;
|
return nbt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,16 +119,16 @@ 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 toNBT().equals(itemMeta.toNBT());
|
return nbt.equals(itemMeta.nbt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return toNBT().hashCode();
|
return nbt.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
@Contract(value = "-> new", pure = true)
|
||||||
protected @NotNull ItemMetaBuilder builder() {
|
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;
|
return this;
|
||||||
} else {
|
} else {
|
||||||
// Create item meta based on nbt
|
// Create item meta based on nbt
|
||||||
var currentNbt = build().toNBT();
|
var currentNbt = build().nbt();
|
||||||
return fromNBT(this, currentNbt).set(tag, value);
|
return fromNBT(this, currentNbt).set(tag, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,8 +122,6 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
|||||||
|
|
||||||
public abstract void write(@NotNull NBTCompound nbtCompound);
|
public abstract void write(@NotNull NBTCompound nbtCompound);
|
||||||
|
|
||||||
protected abstract void deepClone(@NotNull ItemMetaBuilder metaBuilder);
|
|
||||||
|
|
||||||
protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier();
|
protected abstract @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier();
|
||||||
|
|
||||||
@Contract(value = "_, _ -> new", pure = true)
|
@Contract(value = "_, _ -> new", pure = true)
|
||||||
@ -137,11 +135,10 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
|||||||
@Override
|
@Override
|
||||||
protected ItemMetaBuilder clone() {
|
protected ItemMetaBuilder clone() {
|
||||||
try {
|
try {
|
||||||
|
NBTCompound nbtCompound = new NBTCompound();
|
||||||
|
write(nbtCompound);
|
||||||
var builder = (ItemMetaBuilder) super.clone();
|
var builder = (ItemMetaBuilder) super.clone();
|
||||||
builder.displayName = displayName;
|
builder.read(nbtCompound);
|
||||||
builder.lore = new ArrayList<>(lore);
|
|
||||||
builder.enchantmentMap = new HashMap<>(enchantmentMap);
|
|
||||||
deepClone(builder);
|
|
||||||
return builder;
|
return builder;
|
||||||
} catch (CloneNotSupportedException e) {
|
} catch (CloneNotSupportedException e) {
|
||||||
// Should never happen, because ItemMetaBuilder implements Cloneable
|
// 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
|
@Override
|
||||||
protected @NotNull Supplier<ItemMetaBuilder> getSupplier() {
|
protected @NotNull Supplier<ItemMetaBuilder> getSupplier() {
|
||||||
return Builder::new;
|
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
|
@Override
|
||||||
protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() {
|
protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() {
|
||||||
return Builder::new;
|
return Builder::new;
|
||||||
|
@ -286,7 +286,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().toNBT());
|
packet.writeNBT("", itemStack.getMeta().nbt());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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().toNBT();
|
NBTCompound nbtCompound = item.getMeta().nbt();
|
||||||
nbtCompound.setString("type", type);
|
nbtCompound.setString("type", type);
|
||||||
return nbtCompound;
|
return nbtCompound;
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,8 @@ import net.minestom.server.utils.Vector;
|
|||||||
import net.minestom.server.utils.time.TimeUnit;
|
import net.minestom.server.utils.time.TimeUnit;
|
||||||
import net.minestom.server.world.DimensionType;
|
import net.minestom.server.world.DimensionType;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
@ -243,10 +243,9 @@ public class PlayerInit {
|
|||||||
.displayName(Component.text("test"))
|
.displayName(Component.text("test"))
|
||||||
.lore(Component.text("lore"))
|
.lore(Component.text("lore"))
|
||||||
.build();
|
.build();
|
||||||
inventory.setChestplate(item.withLore(components -> {
|
inventory.setChestplate(item.with(itemStackBuilder -> {
|
||||||
var list = new ArrayList<>(components);
|
itemStackBuilder.lore(Collections.emptyList())
|
||||||
list.add(Component.text("hey"));
|
.displayName(null);
|
||||||
return list;
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user