Class O2Potions

java.lang.Object
net.pottercraft.ollivanders2.potion.O2Potions

public class O2Potions extends Object
Manager for all Ollivanders2 potions and potion-related operations.

Handles potion brewing in cauldrons, potion instantiation from types, ingredient management, and mapping of Minecraft potion effects to magic difficulty levels.

Potions can be disabled if they depend on optional plugins (e.g., LibsDisguises).

See Also:
  • Field Details

    • potionTypeKey

      public static org.bukkit.NamespacedKey potionTypeKey
      Namespace key for storing potion type in item NBT tags.

      Used to store the potion type name on potion ItemStack metadata to identify potions when retrieved from player inventory.

    • ingredients

      public static final List<O2ItemType> ingredients
      List of all valid potion ingredients.

      These are the O2ItemType values that can be used in cauldron recipes to brew potions.

  • Constructor Details

    • O2Potions

      public O2Potions(@NotNull @NotNull Ollivanders2 plugin)
      Constructor for initializing the O2Potions manager.

      Creates the NamespacedKey used for storing potion type information in item NBT tags. Call onEnable() during plugin startup to load all available potions.

      Parameters:
      plugin - the Ollivanders2 plugin instance
  • Method Details

    • onEnable

      public void onEnable()
      Load all potions when the plugin enables.

      Populates the potion cache with all available potion types from O2PotionType enum, excluding any that depend on optional plugins (e.g., LibsDisguises) that are not loaded except in test mode.

    • onDisable

      public void onDisable()
      Cleanup when the plugin disables.

      Called when the Ollivanders2 plugin is being shut down. The O2Potions manager performs no cleanup on disable because potion data is read-only and loaded from configuration on startup. No persistent state needs to be saved.

    • getAllPotionTypes

      public static List<O2PotionType> getAllPotionTypes()
      Get all currently loaded potion types.

      Returns the potion types that are active in the O2PotionMap (may exclude potions that depend on unavailable optional plugins).

      Returns:
      a list of all currently loaded potion types
    • getAllPotions

      @NotNull public @NotNull Collection<O2Potion> getAllPotions()
      Get instances of all currently loaded potions.

      Creates one instance of each O2Potion subclass for all loaded potion types. Excludes potions that depend on optional plugins that are not loaded.

      Returns:
      a collection containing one instance of each loaded potion
    • getAllPotionNames

      @NotNull public @NotNull List<String> getAllPotionNames()
      Get the display names of all potion types.

      Returns the display names for all potion types defined in the O2PotionType enum, including those that may be disabled due to missing optional dependencies.

      Returns:
      a list of all potion display names
    • brewPotion

      @Nullable public @Nullable org.bukkit.inventory.ItemStack brewPotion(@NotNull @NotNull org.bukkit.block.Block cauldron, @NotNull @NotNull org.bukkit.entity.Player brewer)
      Brew a potion in a cauldron.

      Attempts to match cauldron ingredients to a known potion recipe. Returns null if the cauldron is not a water cauldron or contains no ingredients. If ingredients don't match any known recipe, returns a bad potion and sends a message to the brewer.

      Parameters:
      cauldron - the cauldron block containing the ingredients
      brewer - the player brewing this potion
      Returns:
      a potion ItemStack if successful, a bad potion if recipe unknown, or null if cauldron is invalid/empty
    • findPotionByIngredients

      @Nullable public @Nullable O2Potion findPotionByIngredients(@NotNull @NotNull Map<O2ItemType,Integer> ingredientsInCauldron)
      Match ingredient amounts to a known potion recipe.

      Compares the provided ingredients against all known potion recipes to find an exact match. Both ingredient types and amounts must match a recipe exactly for the match to succeed.

      Parameters:
      ingredientsInCauldron - a map of ingredient types to their amounts
      Returns:
      the matching potion if found, null if no recipe matches
    • getIngredientsInCauldron

      @NotNull public @NotNull Map<O2ItemType,Integer> getIngredientsInCauldron(@NotNull @NotNull org.bukkit.block.Block cauldron)
      Get all O2Item ingredients near a cauldron.

      Searches for O2Items in a 1-block radius around the cauldron. Only counts items that are valid potion ingredients (items in the ingredients list). Non-O2Items are ignored. Aggregates multiple stacks of the same ingredient by amount.

      Parameters:
      cauldron - the brewing cauldron block
      Returns:
      a map of ingredient types to their total amounts
    • findPotionByItemStack

      @Nullable public @Nullable O2Potion findPotionByItemStack(@NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Find an O2Potion by looking up an ItemStack's NBT metadata.

      Extracts the ItemMeta from the ItemStack and looks up the potion type from its persistent data container. This is a convenience method that delegates to findPotionByItemMeta(ItemMeta).

      Parameters:
      itemStack - the ItemStack to look up
      Returns:
      the O2Potion if found and loaded, null otherwise
    • findPotionByItemMeta

      @Nullable public @Nullable O2Potion findPotionByItemMeta(@NotNull @NotNull org.bukkit.inventory.meta.ItemMeta meta)
      Find an O2Potion by looking up its type in item NBT tags.

      Retrieves the potion type from the item's persistent data container, handling both current (display name) and legacy (enum name) potion type formats.

      Parameters:
      meta - the ItemMeta containing the potion type NBT tag
      Returns:
      the O2Potion instance if the type is found and currently loaded, null otherwise
    • getPotionFromType

      @Nullable public @Nullable O2Potion getPotionFromType(@NotNull @NotNull O2PotionType potionType)
      Create an O2Potion instance from its type using reflection.

      Instantiates the potion class defined in the O2PotionType enum using reflection, calling the constructor that takes an Ollivanders2 plugin parameter.

      Parameters:
      potionType - the potion type to instantiate
      Returns:
      the O2Potion instance if created successfully, null if instantiation fails
    • getAllIngredientNames

      @NotNull public static @NotNull List<String> getAllIngredientNames()
      Get the display names of all valid potion ingredients.
      Returns:
      a list of all potion ingredient display names
    • isLoaded

      public boolean isLoaded(@NotNull @NotNull O2PotionType potionType)
      Check if a potion type is currently loaded.

      A potion may not be loaded if it depends on an optional plugin (e.g., LibsDisguises) that is not installed on the server.

      Parameters:
      potionType - the potion type to check
      Returns:
      true if the potion type is loaded, false if disabled due to missing dependencies
    • getPotionEffectMagicLevel

      @NotNull public static @NotNull MagicLevel getPotionEffectMagicLevel(@NotNull @NotNull org.bukkit.potion.PotionEffectType potionEffectType)
      Get the magic difficulty level for a Minecraft potion effect type.

      Used by FINITE_INCANTATEM counter-spell to determine if a potion effect can be dispelled. If an effect is not explicitly defined, defaults to OWL level.

      Parameters:
      potionEffectType - the Minecraft potion effect type to get the level of
      Returns:
      the magic difficulty level (defaults to OWL if not explicitly defined)
    • getPotionItemStackByType

      @Nullable public @Nullable org.bukkit.inventory.ItemStack getPotionItemStackByType(@NotNull @NotNull O2PotionType potionType, int amount)
      Create an ItemStack for a potion type without brewing checks.

      Creates a potion ItemStack of the specified amount. This bypasses all brewing requirements and experience checks. For player-initiated brewing, use brewPotion(Block, Player) instead, which validates player experience levels and handles bad potions.

      Parameters:
      potionType - the type of potion to create
      amount - the number of potions in the ItemStack
      Returns:
      a potion ItemStack with the specified amount, or null if instantiation fails