2021-04-01 20:02:03 +02:00
|
|
|
package net.minestom.server.item;
|
|
|
|
|
2021-04-14 01:40:21 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2021-04-01 20:02:03 +02:00
|
|
|
import net.kyori.adventure.text.Component;
|
2021-04-11 00:42:09 +02:00
|
|
|
import net.minestom.server.instance.block.Block;
|
2021-04-02 18:13:02 +02:00
|
|
|
import net.minestom.server.item.attribute.ItemAttribute;
|
2021-05-17 17:46:56 +02:00
|
|
|
import net.minestom.server.tag.Tag;
|
2021-05-17 18:26:38 +02:00
|
|
|
import net.minestom.server.tag.TagReadable;
|
2021-04-10 00:55:18 +02:00
|
|
|
import net.minestom.server.utils.binary.BinaryWriter;
|
|
|
|
import net.minestom.server.utils.binary.Writeable;
|
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-11 00:42:09 +02:00
|
|
|
import java.util.*;
|
2021-04-01 22:28:32 +02:00
|
|
|
import java.util.function.Consumer;
|
2021-04-01 20:02:03 +02:00
|
|
|
|
2021-05-17 18:26:38 +02:00
|
|
|
public class ItemMeta implements TagReadable, Writeable {
|
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-11 00:42:09 +02:00
|
|
|
private final Set<Block> canDestroy;
|
|
|
|
private final Set<Block> canPlaceOn;
|
|
|
|
|
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-05-13 08:32:26 +02:00
|
|
|
private String cachedSNBT;
|
|
|
|
private ByteBuf cachedBuffer;
|
2021-04-10 00:55:18 +02:00
|
|
|
|
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-10 18:36:20 +02:00
|
|
|
this.lore = new ArrayList<>(metaBuilder.lore);
|
|
|
|
this.enchantmentMap = new HashMap<>(metaBuilder.enchantmentMap);
|
|
|
|
this.attributes = new ArrayList<>(metaBuilder.attributes);
|
2021-04-09 23:16:57 +02:00
|
|
|
this.customModelData = metaBuilder.customModelData;
|
2021-04-11 00:42:09 +02:00
|
|
|
this.canDestroy = new HashSet<>(metaBuilder.canDestroy);
|
|
|
|
this.canPlaceOn = new HashSet<>(metaBuilder.canPlaceOn);
|
2021-04-09 23:16:57 +02:00
|
|
|
|
2021-06-27 17:36:37 +02:00
|
|
|
this.nbt = metaBuilder.nbt();
|
2021-04-09 23:57:05 +02:00
|
|
|
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-11 00:42:09 +02:00
|
|
|
return Collections.unmodifiableList(lore);
|
2021-04-01 20:02:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
public @NotNull Map<Enchantment, Short> getEnchantmentMap() {
|
2021-04-11 00:42:09 +02:00
|
|
|
return Collections.unmodifiableMap(enchantmentMap);
|
2021-04-02 18:13:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 22:14:48 +02:00
|
|
|
@Contract(pure = true)
|
2021-04-11 00:42:09 +02:00
|
|
|
public @NotNull List<@NotNull ItemAttribute> getAttributes() {
|
|
|
|
return Collections.unmodifiableList(attributes);
|
2021-04-02 18:13:02 +02:00
|
|
|
}
|
|
|
|
|
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-11 00:42:09 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
public @NotNull Set<@NotNull Block> getCanDestroy() {
|
|
|
|
return Collections.unmodifiableSet(canDestroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Contract(pure = true)
|
|
|
|
public @NotNull Set<@NotNull Block> getCanPlaceOn() {
|
|
|
|
return Collections.unmodifiableSet(canPlaceOn);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:46:56 +02:00
|
|
|
@Override
|
|
|
|
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
|
|
|
return tag.read(nbt);
|
|
|
|
}
|
|
|
|
|
2021-04-10 00:24:29 +02:00
|
|
|
public @NotNull NBTCompound toNBT() {
|
2021-04-10 00:25:50 +02:00
|
|
|
return nbt.deepClone();
|
2021-04-02 18:13:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 17:01:50 +02:00
|
|
|
public @NotNull String toSNBT() {
|
2021-05-13 08:32:26 +02:00
|
|
|
if (cachedSNBT == null) {
|
|
|
|
this.cachedSNBT = nbt.toSNBT();
|
|
|
|
}
|
|
|
|
return cachedSNBT;
|
2021-04-10 17:01:50 +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
|
|
|
}
|
2021-04-10 00:55:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void write(@NotNull BinaryWriter writer) {
|
2021-05-13 08:32:26 +02:00
|
|
|
if (cachedBuffer == null) {
|
2021-04-10 00:55:18 +02:00
|
|
|
BinaryWriter w = new BinaryWriter();
|
|
|
|
w.writeNBT("", nbt);
|
2021-05-13 08:32:26 +02:00
|
|
|
this.cachedBuffer = w.getBuffer();
|
|
|
|
}
|
|
|
|
writer.write(cachedBuffer);
|
|
|
|
this.cachedBuffer.resetReaderIndex();
|
2021-04-10 00:55:18 +02:00
|
|
|
}
|
2021-04-01 20:02:03 +02:00
|
|
|
}
|