2021-04-01 20:02:03 +02:00
|
|
|
package net.minestom.server.item;
|
|
|
|
|
|
|
|
import net.kyori.adventure.text.Component;
|
2021-04-02 18:13:02 +02:00
|
|
|
import net.minestom.server.item.attribute.ItemAttribute;
|
2021-04-01 20:02:03 +02:00
|
|
|
import org.jetbrains.annotations.Contract;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-04-02 18:13:02 +02:00
|
|
|
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
2021-04-01 20:02:03 +02:00
|
|
|
|
2021-04-10 00:24:29 +02:00
|
|
|
import java.lang.ref.SoftReference;
|
2021-04-09 23:16:57 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2021-04-01 22:28:32 +02:00
|
|
|
import java.util.function.Consumer;
|
2021-04-01 20:02:03 +02:00
|
|
|
|
2021-04-03 04:07:27 +02:00
|
|
|
public class ItemMeta {
|
2021-04-01 20:02:03 +02:00
|
|
|
|
2021-04-02 18:13:02 +02:00
|
|
|
private final int damage;
|
|
|
|
private final boolean unbreakable;
|
|
|
|
private final int hideFlag;
|
2021-04-01 20:02:03 +02:00
|
|
|
private final Component displayName;
|
|
|
|
private final List<Component> lore;
|
|
|
|
|
2021-04-02 18:13:02 +02:00
|
|
|
private final Map<Enchantment, Short> enchantmentMap;
|
|
|
|
private final List<ItemAttribute> attributes;
|
|
|
|
|
|
|
|
private final int customModelData;
|
|
|
|
|
2021-04-09 23:16:57 +02:00
|
|
|
private final NBTCompound nbt;
|
|
|
|
private final ItemMetaBuilder emptyBuilder;
|
2021-04-02 18:13:02 +02:00
|
|
|
|
2021-04-10 00:24:29 +02:00
|
|
|
private SoftReference<NBTCompound> nbtCache;
|
|
|
|
|
2021-04-01 20:02:03 +02:00
|
|
|
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
2021-04-09 23:16:57 +02:00
|
|
|
this.damage = metaBuilder.damage;
|
|
|
|
this.unbreakable = metaBuilder.unbreakable;
|
|
|
|
this.hideFlag = metaBuilder.hideFlag;
|
2021-04-01 20:02:03 +02:00
|
|
|
this.displayName = metaBuilder.displayName;
|
2021-04-02 14:02:24 +02:00
|
|
|
this.lore = Collections.unmodifiableList(metaBuilder.lore);
|
2021-04-02 18:13:02 +02:00
|
|
|
this.enchantmentMap = Collections.unmodifiableMap(metaBuilder.enchantmentMap);
|
2021-04-09 23:16:57 +02:00
|
|
|
this.attributes = Collections.unmodifiableList(metaBuilder.attributes);
|
|
|
|
this.customModelData = metaBuilder.customModelData;
|
|
|
|
|
2021-04-09 23:57:05 +02:00
|
|
|
this.nbt = metaBuilder.nbt;
|
|
|
|
this.emptyBuilder = metaBuilder.getSupplier().get();
|
2021-04-01 20:02:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-01 22:28:32 +02:00
|
|
|
@Contract(value = "_, -> new", pure = true)
|
|
|
|
public @NotNull ItemMeta with(@NotNull Consumer<@NotNull ItemMetaBuilder> builderConsumer) {
|
|
|
|
var builder = builder();
|
|
|
|
builderConsumer.accept(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:21:23 +02:00
|
|
|
@Contract(pure = true)
|
2021-04-02 18:13:02 +02:00
|
|
|
public int getDamage() {
|
|
|
|
return damage;
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:21:23 +02:00
|
|
|
@Contract(pure = true)
|
2021-04-02 18:13:02 +02:00
|
|
|
public boolean isUnbreakable() {
|
|
|
|
return unbreakable;
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:21:23 +02:00
|
|
|
@Contract(pure = true)
|
2021-04-02 18:13:02 +02:00
|
|
|
public int getHideFlag() {
|
|
|
|
return hideFlag;
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:02:03 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
public @Nullable Component getDisplayName() {
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Contract(pure = true)
|
2021-04-02 22:14:48 +02:00
|
|
|
public @NotNull List<@NotNull Component> getLore() {
|
2021-04-01 20:02:03 +02:00
|
|
|
return lore;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
public @NotNull Map<Enchantment, Short> getEnchantmentMap() {
|
2021-04-02 18:13:02 +02:00
|
|
|
return enchantmentMap;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
public @NotNull List<ItemAttribute> getAttributes() {
|
2021-04-02 18:13:02 +02:00
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(pure = true)
|
2021-04-02 18:13:02 +02:00
|
|
|
public int getCustomModelData() {
|
|
|
|
return customModelData;
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:45:46 +02:00
|
|
|
public <T> T getOrDefault(@NotNull ItemTag<T> tag, @Nullable T defaultValue) {
|
2021-04-03 15:50:34 +02:00
|
|
|
var key = tag.getKey();
|
|
|
|
if (nbt.containsKey(key)) {
|
2021-04-10 00:24:29 +02:00
|
|
|
return tag.read(toNBT());
|
2021-04-03 15:50:34 +02:00
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2021-04-03 15:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public <T> @Nullable T get(@NotNull ItemTag<T> tag) {
|
2021-04-10 00:24:29 +02:00
|
|
|
return tag.read(toNBT());
|
2021-04-03 15:45:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 00:24:29 +02:00
|
|
|
public @NotNull NBTCompound toNBT() {
|
|
|
|
NBTCompound cache = nbtCache.get();
|
|
|
|
if (cache == null) {
|
|
|
|
cache = nbt.deepClone();
|
|
|
|
nbtCache = new SoftReference<>(cache);
|
|
|
|
}
|
|
|
|
return cache;
|
2021-04-02 18:13:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 18:25:20 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
ItemMeta itemMeta = (ItemMeta) o;
|
2021-04-10 00:24:29 +02:00
|
|
|
return nbt.equals(itemMeta.nbt);
|
2021-04-02 18:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2021-04-09 23:16:57 +02:00
|
|
|
return nbt.hashCode();
|
2021-04-02 18:25:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(value = "-> new", pure = true)
|
2021-04-01 20:02:03 +02:00
|
|
|
protected @NotNull ItemMetaBuilder builder() {
|
2021-04-09 23:16:57 +02:00
|
|
|
return ItemMetaBuilder.fromNBT(emptyBuilder, nbt);
|
2021-04-01 20:02:03 +02:00
|
|
|
}
|
|
|
|
}
|