Minestom/src/main/java/net/minestom/server/item/ItemMeta.java

130 lines
3.5 KiB
Java
Raw Normal View History

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;
import net.minestom.server.utils.NBTUtils;
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
import java.lang.ref.SoftReference;
2021-04-02 18:13:02 +02:00
import java.util.ArrayList;
2021-04-02 14:02:24 +02:00
import java.util.Collections;
2021-04-01 20:02:03 +02:00
import java.util.List;
2021-04-02 18:13:02 +02:00
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
public class ItemMeta implements Cloneable {
private final ItemMetaBuilder builder;
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-02 22:14:48 +02:00
private final @Nullable NBTCompound originalNbt;
private SoftReference<NBTCompound> cache;
2021-04-02 18:13:02 +02:00
2021-04-01 20:02:03 +02:00
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
this.builder = metaBuilder.clone();
2021-04-02 18:13:02 +02:00
this.damage = 0;
this.unbreakable = false;
this.hideFlag = 0;
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);
this.attributes = new ArrayList<>();
this.customModelData = 0;
2021-04-02 22:14:48 +02:00
// Can be null
this.originalNbt = metaBuilder.originalNBT;
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-02 18:13:02 +02:00
public int getDamage() {
return damage;
}
public boolean isUnbreakable() {
return unbreakable;
}
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-02 18:25:20 +02:00
public @NotNull NBTCompound toNBT() {
2021-04-02 22:14:48 +02:00
if (originalNbt != null) {
// Return the nbt this meta has been created with
return originalNbt;
}
var nbt = cache != null ? cache.get() : null;
if (nbt == null) {
nbt = NBTUtils.metaToNBT(this);
2021-04-02 22:14:48 +02:00
this.builder.write(nbt);
this.cache = new SoftReference<>(nbt);
2021-04-02 18:13:02 +02:00
}
2021-04-02 22:14:48 +02:00
return nbt;
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;
return toNBT().equals(itemMeta.toNBT());
}
@Override
public int hashCode() {
return toNBT().hashCode();
}
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() {
return builder.clone();
}
}