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

98 lines
2.6 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
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;
private NBTCompound cache = null;
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-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)
public @Nullable List<@NotNull Component> getLore() {
return lore;
}
2021-04-02 18:13:02 +02:00
public Map<Enchantment, Short> getEnchantmentMap() {
return enchantmentMap;
}
public List<ItemAttribute> getAttributes() {
return attributes;
}
public int getCustomModelData() {
return customModelData;
}
public NBTCompound toNBT() {
if (cache == null) {
this.cache = NBTUtils.metaToNBT(this);
}
return cache;
}
2021-04-01 20:02:03 +02:00
protected @NotNull ItemMetaBuilder builder() {
return builder.clone();
}
}