Class NativeFirstItemStorage
- All Implemented Interfaces:
ItemStorage
This implementation uses Bukkit's native ItemMeta as the sole source of truth
for enchantment state. NBT is used only for auxiliary metadata through
NbtMetadataStorage.
Key design principles:
- All enchantment state lives in ItemMeta (via addEnchant/removeEnchant)
- No duplication of enchant state in NBT
- Atomic operations ensure complete success or no change
- Level validation uses enchantment definition bounds
- Since:
- 0.1.0
-
Constructor Summary
ConstructorsConstructorDescriptionNativeFirstItemStorage(@NotNull Supplier<EnchantmentRegistryManager> registrySupplier, @NotNull NbtMetadataStorage nbtStorage) Creates a new native-first item storage. -
Method Summary
Modifier and TypeMethodDescription@NotNull org.bukkit.inventory.ItemStackapplyEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull EnchantmentDefinition enchantment, int level) Applies an enchantment to an item at the specified level.@NotNull org.bukkit.inventory.ItemStackapplyEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.NamespacedKey key, int level) Applies an enchantment by its registry key.@NotNull org.bukkit.inventory.ItemStackclearAuxiliaryMetadata(@NotNull org.bukkit.inventory.ItemStack item) Clears all auxiliary metadata from an item.@Nullable StringgetAuxiliaryMetadata(@NotNull org.bukkit.inventory.ItemStack item, @NotNull String key) Gets an auxiliary metadata string value.getAuxiliaryMetadataKeys(@NotNull org.bukkit.inventory.ItemStack item) Gets the names of all auxiliary metadata keys stored for this item.getEnchantmentKeys(@NotNull org.bukkit.inventory.ItemStack item) Gets all artificial enchantment keys on an item as NamespacedKey to level mapping.intgetEnchantmentLevel(@NotNull org.bukkit.inventory.ItemStack item, @NotNull EnchantmentDefinition enchantment) Gets the level of a specific enchantment on an item.intgetEnchantmentLevel(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.NamespacedKey key) Gets the level of an enchantment by its registry key.@NotNull Map<EnchantmentDefinition, Integer> getEnchantments(@NotNull org.bukkit.inventory.ItemStack item) Gets all artificial enchantments on an item.booleanhasAuxiliaryMetadata(@NotNull org.bukkit.inventory.ItemStack item, @NotNull String key) Checks if auxiliary metadata with the given key exists.booleanhasEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull EnchantmentDefinition enchantment) Checks if an item has a specific enchantment.booleanhasEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.NamespacedKey key) Checks if an item has an enchantment by its registry key.@NotNull org.bukkit.inventory.ItemStackremoveAllEnchantments(@NotNull org.bukkit.inventory.ItemStack item) Removes all artificial enchantments from an item.@NotNull org.bukkit.inventory.ItemStackremoveEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull EnchantmentDefinition enchantment) Removes an enchantment from an item.@NotNull org.bukkit.inventory.ItemStackremoveEnchantment(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.NamespacedKey key) Removes an enchantment by its registry key.@NotNull org.bukkit.inventory.ItemStacksetAuxiliaryMetadata(@NotNull org.bukkit.inventory.ItemStack item, @NotNull String key, @Nullable String value) Sets an auxiliary metadata string value.
-
Constructor Details
-
NativeFirstItemStorage
public NativeFirstItemStorage(@NotNull @NotNull Supplier<EnchantmentRegistryManager> registrySupplier, @NotNull @NotNull NbtMetadataStorage nbtStorage) Creates a new native-first item storage.- Parameters:
registrySupplier- supplier for the enchantment registry managernbtStorage- the NBT metadata storage for auxiliary data- Since:
- 0.1.0
-
-
Method Details
-
applyEnchantment
@NotNull public @NotNull org.bukkit.inventory.ItemStack applyEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull EnchantmentDefinition enchantment, int level) Description copied from interface:ItemStorageApplies an enchantment to an item at the specified level.The enchantment level is validated against the definition's bounds before application. If the level is outside the valid range, the operation fails.
This operation is atomic - either the enchantment is fully applied with all auxiliary metadata updated, or the item remains unchanged.
- Specified by:
applyEnchantmentin interfaceItemStorage- Parameters:
item- the item to enchant (must not be null or air)enchantment- the enchantment definition to apply (must not be null)level- the level to apply (must be within enchantment's min/max bounds)- Returns:
- the enchanted item (may be the same instance if item is mutable)
-
applyEnchantment
@NotNull public @NotNull org.bukkit.inventory.ItemStack applyEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.NamespacedKey key, int level) Description copied from interface:ItemStorageApplies an enchantment by its registry key.This is a convenience method that looks up the definition in the registry before delegating to
ItemStorage.applyEnchantment(ItemStack, EnchantmentDefinition, int).- Specified by:
applyEnchantmentin interfaceItemStorage- Parameters:
item- the item to enchant (must not be null or air)key- the enchantment's namespaced key (must not be null)level- the level to apply- Returns:
- the enchanted item
- See Also:
-
removeEnchantment
@NotNull public @NotNull org.bukkit.inventory.ItemStack removeEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull EnchantmentDefinition enchantment) Description copied from interface:ItemStorageRemoves an enchantment from an item.If the item does not have the specified enchantment, this operation is a no-op and returns the item unchanged.
Auxiliary metadata associated with this enchantment is also cleaned up.
- Specified by:
removeEnchantmentin interfaceItemStorage- Parameters:
item- the item to modify (must not be null)enchantment- the enchantment to remove (must not be null)- Returns:
- the modified item (may be the same instance if item is mutable)
-
removeEnchantment
@NotNull public @NotNull org.bukkit.inventory.ItemStack removeEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.NamespacedKey key) Description copied from interface:ItemStorageRemoves an enchantment by its registry key.- Specified by:
removeEnchantmentin interfaceItemStorage- Parameters:
item- the item to modify (must not be null)key- the enchantment's namespaced key (must not be null)- Returns:
- the modified item
- See Also:
-
removeAllEnchantments
@NotNull public @NotNull org.bukkit.inventory.ItemStack removeAllEnchantments(@NotNull @NotNull org.bukkit.inventory.ItemStack item) Description copied from interface:ItemStorageRemoves all artificial enchantments from an item.This removes all enchantments registered through this API while preserving vanilla enchantments and the item's base properties (durability, lore, etc).
- Specified by:
removeAllEnchantmentsin interfaceItemStorage- Parameters:
item- the item to clear (must not be null)- Returns:
- the modified item
-
getEnchantmentLevel
public int getEnchantmentLevel(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull EnchantmentDefinition enchantment) Description copied from interface:ItemStorageGets the level of a specific enchantment on an item.- Specified by:
getEnchantmentLevelin interfaceItemStorage- Parameters:
item- the item to query (must not be null)enchantment- the enchantment to check (must not be null)- Returns:
- the enchantment level, or 0 if not present
-
getEnchantmentLevel
public int getEnchantmentLevel(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.NamespacedKey key) Description copied from interface:ItemStorageGets the level of an enchantment by its registry key.- Specified by:
getEnchantmentLevelin interfaceItemStorage- Parameters:
item- the item 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 or not registered
-
getEnchantments
@NotNull public @NotNull Map<EnchantmentDefinition,Integer> getEnchantments(@NotNull @NotNull org.bukkit.inventory.ItemStack item) Description copied from interface:ItemStorageGets all artificial enchantments on an item.Returns a map of enchantment definitions to their levels. Only enchantments registered through this API are included; vanilla enchantments are excluded.
The returned map is unmodifiable and represents a snapshot of the item's state at the time of the call.
- Specified by:
getEnchantmentsin interfaceItemStorage- Parameters:
item- the item to query (must not be null)- Returns:
- an unmodifiable map of enchantments to levels (never null, may be empty)
-
getEnchantmentKeys
@NotNull public @NotNull Map<org.bukkit.NamespacedKey,Integer> getEnchantmentKeys(@NotNull @NotNull org.bukkit.inventory.ItemStack item) Description copied from interface:ItemStorageGets all artificial enchantment keys on an item as NamespacedKey to level mapping.This is a lower-level alternative to
ItemStorage.getEnchantments(ItemStack)that returns raw keys rather than resolved definitions. Useful when definitions may not be available (e.g., cross-plugin compatibility).- Specified by:
getEnchantmentKeysin interfaceItemStorage- Parameters:
item- the item to query (must not be null)- Returns:
- an unmodifiable map of enchantment keys to levels
-
hasEnchantment
public boolean hasEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull EnchantmentDefinition enchantment) Description copied from interface:ItemStorageChecks if an item has a specific enchantment.- Specified by:
hasEnchantmentin interfaceItemStorage- Parameters:
item- the item to check (must not be null)enchantment- the enchantment to look for (must not be null)- Returns:
- true if the item has the enchantment at any level
-
hasEnchantment
public boolean hasEnchantment(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.NamespacedKey key) Description copied from interface:ItemStorageChecks if an item has an enchantment by its registry key.- Specified by:
hasEnchantmentin interfaceItemStorage- Parameters:
item- the item to check (must not be null)key- the enchantment's namespaced key (must not be null)- Returns:
- true if the item has the enchantment at any level
-
getAuxiliaryMetadataKeys
@NotNull public @NotNull Set<String> getAuxiliaryMetadataKeys(@NotNull @NotNull org.bukkit.inventory.ItemStack item) Description copied from interface:ItemStorageGets the names of all auxiliary metadata keys stored for this item.Auxiliary metadata is stored in NBT and used for compatibility markers, display flags, and other non-enchantment data.
- Specified by:
getAuxiliaryMetadataKeysin interfaceItemStorage- Parameters:
item- the item to query (must not be null)- Returns:
- a set of auxiliary metadata keys (never null, may be empty)
-
setAuxiliaryMetadata
@NotNull public @NotNull org.bukkit.inventory.ItemStack setAuxiliaryMetadata(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull String key, @Nullable @Nullable String value) Description copied from interface:ItemStorageSets an auxiliary metadata string value.This stores data in NBT that is not part of the core enchantment state. Use this for compatibility markers, display configuration, etc.
- Specified by:
setAuxiliaryMetadatain interfaceItemStorage- Parameters:
item- the item to modify (must not be null)key- the metadata key (must not be null or empty)value- the value to store, or null to remove- Returns:
- the modified item
-
getAuxiliaryMetadata
@Nullable public @Nullable String getAuxiliaryMetadata(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull String key) Description copied from interface:ItemStorageGets an auxiliary metadata string value.- Specified by:
getAuxiliaryMetadatain interfaceItemStorage- Parameters:
item- the item to query (must not be null)key- the metadata key (must not be null)- Returns:
- the stored value, or null if not present
-
hasAuxiliaryMetadata
public boolean hasAuxiliaryMetadata(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull String key) Description copied from interface:ItemStorageChecks if auxiliary metadata with the given key exists.- Specified by:
hasAuxiliaryMetadatain interfaceItemStorage- Parameters:
item- the item to query (must not be null)key- the metadata key (must not be null)- Returns:
- true if metadata exists for the key
-
clearAuxiliaryMetadata
@NotNull public @NotNull org.bukkit.inventory.ItemStack clearAuxiliaryMetadata(@NotNull @NotNull org.bukkit.inventory.ItemStack item) Description copied from interface:ItemStorageClears all auxiliary metadata from an item.This does not affect native enchantment state, only the NBT auxiliary store.
- Specified by:
clearAuxiliaryMetadatain interfaceItemStorage- Parameters:
item- the item to modify (must not be null)- Returns:
- the modified item
-