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

174 lines
4.7 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-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
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
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-07-19 04:34:42 +02:00
private final ItemMetaBuilder metaBuilder;
private final NBTCompound nbt;
2021-04-02 18:13:02 +02:00
private String cachedSNBT;
private ByteBuffer cachedBuffer;
2021-04-10 00:55:18 +02:00
2021-04-01 20:02:03 +02:00
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
this.damage = metaBuilder.damage;
this.unbreakable = metaBuilder.unbreakable;
this.hideFlag = metaBuilder.hideFlag;
2021-04-01 20:02:03 +02:00
this.displayName = metaBuilder.displayName;
this.lore = List.copyOf(metaBuilder.lore);
this.enchantmentMap = Map.copyOf(metaBuilder.enchantmentMap);
this.attributes = List.copyOf(metaBuilder.attributes);
this.customModelData = metaBuilder.customModelData;
this.canDestroy = Set.copyOf(metaBuilder.canDestroy);
this.canPlaceOn = Set.copyOf(metaBuilder.canPlaceOn);
2021-07-19 04:34:42 +02:00
this.metaBuilder = metaBuilder;
2021-12-19 18:54:29 +01:00
this.nbt = metaBuilder.nbt.toCompound();
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() {
return 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() {
return 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 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 canDestroy;
2021-04-11 00:42:09 +02:00
}
@Contract(pure = true)
public @NotNull Set<@NotNull Block> getCanPlaceOn() {
return canPlaceOn;
2021-04-11 00:42:09 +02:00
}
2021-05-17 17:46:56 +02:00
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return tag.read(nbt);
}
public @NotNull NBTCompound toNBT() {
2021-12-13 16:41:30 +01:00
return nbt;
2021-04-02 18:13:02 +02:00
}
2021-04-10 17:01:50 +02:00
public @NotNull String toSNBT() {
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;
return nbt.equals(itemMeta.nbt);
2021-04-02 18:25:20 +02:00
}
@Override
public int hashCode() {
return nbt.hashCode();
2021-04-02 18:25:20 +02:00
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"nbt=" + nbt +
'}';
}
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() {
ItemMetaBuilder result = metaBuilder.createEmpty();
2021-12-20 20:15:26 +01:00
ItemMetaBuilder.resetMeta(result, nbt);
return result;
2021-04-01 20:02:03 +02:00
}
2021-04-10 00:55:18 +02:00
@Override
public synchronized void write(@NotNull BinaryWriter writer) {
if (nbt.isEmpty()) {
writer.writeByte((byte) 0);
return;
}
if (cachedBuffer == null) {
2021-04-10 00:55:18 +02:00
BinaryWriter w = new BinaryWriter();
w.writeNBT("", nbt);
this.cachedBuffer = w.getBuffer();
}
writer.write(cachedBuffer.flip());
2021-04-10 00:55:18 +02:00
}
2021-04-01 20:02:03 +02:00
}