Class EnchantedBookImpl

java.lang.Object
io.artificial.enchantments.internal.book.EnchantedBookImpl
All Implemented Interfaces:
EnchantedBook

public class EnchantedBookImpl extends Object implements EnchantedBook
Implementation of EnchantedBook using native ItemMeta storage.

This implementation follows the native-first policy, storing enchantments directly in the book's EnchantmentStorageMeta. No auxiliary NBT is used for enchantment data.

Since:
0.2.0
  • Constructor Details

    • EnchantedBookImpl

      public EnchantedBookImpl(@NotNull @NotNull ItemStorage itemStorage)
      Creates a new enchanted book implementation.
      Parameters:
      itemStorage - the item storage for enchantment operations
  • Method Details

    • isEnchantedBook

      public boolean isEnchantedBook(@NotNull @NotNull org.bukkit.inventory.ItemStack item)
      Checks if an item is an enchanted book (vanilla or custom).

      This returns true for both vanilla ENCHANTED_BOOK items and books created through this API.

      Specified by:
      isEnchantedBook in interface EnchantedBook
      Parameters:
      item - the item to check (must not be null)
      Returns:
      true if the item is an enchanted book
    • hasCustomEnchantments

      public boolean hasCustomEnchantments(@NotNull @NotNull org.bukkit.inventory.ItemStack book)
      Checks if an enchanted book contains custom (artificial) enchantments.

      This distinguishes between books with only vanilla enchantments and books that have enchantments registered through this API.

      Specified by:
      hasCustomEnchantments in interface EnchantedBook
      Parameters:
      book - the book to check (must not be null)
      Returns:
      true if the book has at least one artificial enchantment
    • getEnchantments

      @NotNull public @NotNull Map<EnchantmentDefinition,Integer> getEnchantments(@NotNull @NotNull org.bukkit.inventory.ItemStack book)
      Gets all artificial enchantments stored on a book.

      Returns a map of enchantment definitions to their levels. Only enchantments registered through this API are included.

      Specified by:
      getEnchantments in interface EnchantedBook
      Parameters:
      book - the book to query (must not be null)
      Returns:
      unmodifiable map of enchantments to levels (never null, may be empty)
    • getEnchantmentLevel

      public int getEnchantmentLevel(@NotNull @NotNull org.bukkit.inventory.ItemStack book, @NotNull @NotNull EnchantmentDefinition enchantment)
      Gets the level of a specific artificial enchantment on a book.
      Specified by:
      getEnchantmentLevel in interface EnchantedBook
      Parameters:
      book - the book to query (must not be null)
      enchantment - the enchantment to look for (must not be null)
      Returns:
      the enchantment level, or 0 if not present
    • getEnchantmentLevel

      public int getEnchantmentLevel(@NotNull @NotNull org.bukkit.inventory.ItemStack book, @NotNull @NotNull org.bukkit.NamespacedKey key)
      Gets the level of an artificial enchantment by its key.
      Specified by:
      getEnchantmentLevel in interface EnchantedBook
      Parameters:
      book - the book to query (must not be null)
      key - the enchantment's namespaced key (must not be null)
      Returns:
      the enchantment level, or 0 if not present
    • getAllEnchantmentKeys

      @NotNull public @NotNull Map<org.bukkit.NamespacedKey,Integer> getAllEnchantmentKeys(@NotNull @NotNull org.bukkit.inventory.ItemStack book)
      Gets all enchantment keys on a book as NamespacedKey to level mapping.

      This includes both vanilla and artificial enchantments.

      Specified by:
      getAllEnchantmentKeys in interface EnchantedBook
      Parameters:
      book - the book to query (must not be null)
      Returns:
      unmodifiable map of all enchantment keys to levels
    • hasEnchantment

      public boolean hasEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack book, @NotNull @NotNull EnchantmentDefinition enchantment)
      Checks if a specific artificial enchantment exists on a book.
      Specified by:
      hasEnchantment in interface EnchantedBook
      Parameters:
      book - the book to check (must not be null)
      enchantment - the enchantment to look for (must not be null)
      Returns:
      true if the book has the enchantment at any level
    • hasEnchantment

      public boolean hasEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack book, @NotNull @NotNull org.bukkit.NamespacedKey key)
      Checks if an artificial enchantment exists by its key.
      Specified by:
      hasEnchantment in interface EnchantedBook
      Parameters:
      book - the book to check (must not be null)
      key - the enchantment's namespaced key (must not be null)
      Returns:
      true if the book has the enchantment at any level
    • builder

      @NotNull public @NotNull EnchantedBook.Builder builder()
      Gets a builder for creating enchanted books.

      The returned builder can be used to construct books with one or more enchantments. Books are created as ENCHANTED_BOOK items with enchantments stored in native ItemMeta.

      Specified by:
      builder in interface EnchantedBook
      Returns:
      a new book builder instance