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

175 lines
5.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-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;
2022-04-13 17:57:15 +02:00
import net.minestom.server.tag.Taggable;
2021-04-10 00:55:18 +02:00
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;
2022-03-10 16:07:56 +01:00
import org.jetbrains.annotations.UnknownNullability;
2021-04-02 18:13:02 +02:00
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
2021-04-01 20:02:03 +02:00
2022-04-13 17:57:15 +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
2022-04-13 17:57:15 +02:00
public sealed interface ItemMeta extends TagReadable, Writeable
permits ItemMetaImpl {
@Override
<T> @UnknownNullability T getTag(@NotNull Tag<T> tag);
2021-04-10 00:55:18 +02:00
2022-04-13 17:57:15 +02:00
@Contract(value = "_, -> new", pure = true)
@NotNull ItemMeta with(@NotNull Consumer<@NotNull Builder> builderConsumer);
2022-04-13 17:57:15 +02:00
@NotNull NBTCompound toNBT();
2021-04-01 20:02:03 +02:00
2022-04-13 17:57:15 +02:00
@NotNull String toSNBT();
2021-04-01 22:28:32 +02:00
2021-04-03 00:21:23 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default int getDamage() {
return getTag(ItemTags.DAMAGE);
2021-04-02 18:13:02 +02:00
}
2021-04-03 00:21:23 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default boolean isUnbreakable() {
return getTag(ItemTags.UNBREAKABLE);
2021-04-02 18:13:02 +02:00
}
2021-04-03 00:21:23 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default int getHideFlag() {
return getTag(ItemTags.HIDE_FLAGS);
2021-04-02 18:13:02 +02:00
}
2021-04-01 20:02:03 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @Nullable Component getDisplayName() {
return getTag(ItemTags.NAME);
2021-04-01 20:02:03 +02:00
}
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @NotNull List<@NotNull Component> getLore() {
return getTag(ItemTags.LORE);
2021-04-01 20:02:03 +02:00
}
2021-04-02 22:14:48 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @NotNull Map<Enchantment, Short> getEnchantmentMap() {
return getTag(ItemTags.ENCHANTMENTS);
2021-04-02 18:13:02 +02:00
}
2021-04-02 22:14:48 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @NotNull List<@NotNull ItemAttribute> getAttributes() {
return getTag(ItemTags.ATTRIBUTES);
2021-04-02 18:13:02 +02:00
}
2021-04-02 22:14:48 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default int getCustomModelData() {
return getTag(ItemTags.CUSTOM_MODEL_DATA);
2021-04-02 18:13:02 +02:00
}
2021-04-11 00:42:09 +02:00
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @NotNull Set<@NotNull Block> getCanDestroy() {
return Set.copyOf(getTag(ItemTags.CAN_DESTROY));
2021-04-11 00:42:09 +02:00
}
@Contract(pure = true)
2022-04-13 17:57:15 +02:00
default @NotNull Set<@NotNull Block> getCanPlaceOn() {
return Set.copyOf(getTag(ItemTags.CAN_PLACE_ON));
2021-04-11 00:42:09 +02:00
}
sealed interface Builder extends Taggable
permits ItemMetaImpl.Builder, ItemMetaView.Builder {
2022-04-13 17:57:15 +02:00
@NotNull ItemMeta build();
2021-05-17 17:46:56 +02:00
2022-04-13 17:57:15 +02:00
default <T> @NotNull Builder set(@NotNull Tag<T> tag, @Nullable T value) {
setTag(tag, value);
return this;
}
2021-04-02 18:13:02 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder damage(int damage) {
return set(ItemTags.DAMAGE, damage);
}
2021-04-10 17:01:50 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder unbreakable(boolean unbreakable) {
return set(ItemTags.UNBREAKABLE, unbreakable);
}
2021-04-02 18:25:20 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder hideFlag(int hideFlag) {
return set(ItemTags.HIDE_FLAGS, hideFlag);
}
2021-04-02 18:25:20 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder hideFlag(@NotNull ItemHideFlag... hideFlags) {
int result = 0;
for (ItemHideFlag hideFlag : hideFlags) result |= hideFlag.getBitFieldPart();
return hideFlag(result);
}
2021-04-02 18:25:20 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder displayName(@Nullable Component displayName) {
return set(ItemTags.NAME, displayName);
}
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder lore(@NotNull List<? extends Component> lore) {
return set(ItemTags.LORE, lore.isEmpty() ? null : List.class.cast(lore));
}
2021-04-10 00:55:18 +02:00
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder lore(Component... lore) {
return lore(Arrays.asList(lore));
}
@Contract("_ -> this")
default @NotNull Builder enchantments(@NotNull Map<Enchantment, Short> enchantments) {
return set(ItemTags.ENCHANTMENTS, Map.copyOf(enchantments));
}
@Contract("_, _ -> this")
default @NotNull Builder enchantment(@NotNull Enchantment enchantment, short level) {
var enchantments = new HashMap<>(getTag(ItemTags.ENCHANTMENTS));
enchantments.put(enchantment, level);
return enchantments(enchantments);
}
@Contract("-> this")
default @NotNull Builder clearEnchantment() {
return enchantments(Map.of());
}
2022-04-13 17:57:15 +02:00
@Contract("_ -> this")
default @NotNull Builder attributes(@NotNull List<@NotNull ItemAttribute> attributes) {
return set(ItemTags.ATTRIBUTES, attributes.isEmpty() ? null : attributes);
}
@Contract("_ -> this")
default @NotNull Builder customModelData(int customModelData) {
return set(ItemTags.CUSTOM_MODEL_DATA, customModelData);
}
@Contract("_ -> this")
default @NotNull Builder canPlaceOn(@NotNull Set<@NotNull Block> blocks) {
return set(ItemTags.CAN_PLACE_ON, blocks.stream().map(block -> Block.fromNamespaceId(block.name())).toList());
2022-04-13 17:57:15 +02:00
}
@Contract("_ -> this")
default @NotNull Builder canPlaceOn(@NotNull Block... blocks) {
return canPlaceOn(Set.of(blocks));
}
@Contract("_ -> this")
default @NotNull Builder canDestroy(@NotNull Set<@NotNull Block> blocks) {
return set(ItemTags.CAN_DESTROY, blocks.stream().map(block -> Block.fromNamespaceId(block.name())).toList());
2022-04-13 17:57:15 +02:00
}
@Contract("_ -> this")
default @NotNull Builder canDestroy(@NotNull Block... blocks) {
return canDestroy(Set.of(blocks));
}
2021-04-10 00:55:18 +02:00
}
2021-04-01 20:02:03 +02:00
}