Class CauldronRecipeRegistry

java.lang.Object
wtf.choco.alchema.crafting.CauldronRecipeRegistry

public class CauldronRecipeRegistry
extends java.lang.Object
Represents a registry in which recipes and recipe types may be registered.
Author:
Parker Hawke - Choco
  • Constructor Details

    • CauldronRecipeRegistry

      public CauldronRecipeRegistry()
  • Method Details

    • registerCauldronRecipe

      public void registerCauldronRecipe​(@NotNull @NotNull CauldronRecipe recipe)
      Register a CauldronRecipe to be used by any AlchemicalCauldron.
      Parameters:
      recipe - the recipe to register
    • unregisterCauldronRecipe

      public void unregisterCauldronRecipe​(@NotNull @NotNull CauldronRecipe recipe)
      Unregister a CauldronRecipe. Upon unregistration, cauldrons will not longer be able to execute its recipe.
      Parameters:
      recipe - the recipe to unregister
    • unregisterCauldronRecipe

      @Nullable public @Nullable CauldronRecipe unregisterCauldronRecipe​(@NotNull @NotNull org.bukkit.NamespacedKey key)
      Unregister a CauldronRecipe associated with the provided ID. Upon unregistration, cauldrons will no longer be able to execute its recipe.
      Parameters:
      key - the key of the recipe to unregister
      Returns:
      the unregistered recipe. null if none
    • getApplicableRecipe

      @Nullable public @Nullable CauldronRecipe getApplicableRecipe​(@NotNull @NotNull java.util.List<@NotNull CauldronIngredient> ingredients, boolean mostComplex)
      Get the CauldronRecipe that applies given a set of ingredients. If no recipe can consume the ingredients, null is returned. If more than one recipe is valid, the first is selected.
      Parameters:
      ingredients - the available ingredients
      mostComplex - whether or not to find the most complex applicable recipe if more than one is applicable. Note that if this value is true, all recipes will be iterated over and will result in a fixed operation time of O(n) rather than O(n) worst case.
      Returns:
      the cauldron recipe that applies. null if none
      See Also:
      to get a list of all applicable recipes
    • getApplicableRecipe

      @Nullable public @Nullable CauldronRecipe getApplicableRecipe​(@NotNull @NotNull java.util.List<@NotNull CauldronIngredient> ingredients)
      Get the CauldronRecipe that applies given a set of ingredients. If no recipe can consume the ingredients, null is returned. If more than one recipe is valid, the first is selected.

      The complexity of the recipe is not taken into consideration. The recipe returned by this method is not guaranteed.

      Parameters:
      ingredients - the available ingredients
      Returns:
      the cauldron recipe that applies. null if none
      See Also:
      to get a list of all applicable recipes
    • getApplicableRecipes

      @NotNull public @NotNull java.util.List<@NotNull CauldronRecipe> getApplicableRecipes​(@NotNull @NotNull java.util.List<@NotNull CauldronIngredient> ingredients)
      Get a list of CauldronRecipes that apply given a set of ingredients sorted by their complexity (0th index = most complex, last index = least complex). If no recipe can consume the ingredients, an empty list is returned.
      Parameters:
      ingredients - the available ingredients
      Returns:
      all applicable cauldron recipes. empty list if none
    • getRecipes

      @NotNull public @NotNull java.util.Collection<@NotNull CauldronRecipe> getRecipes()
      Get a collection of all registered recipes. Changes made to the returned collection will be reflected internally to this instance.
      Returns:
      the collection of registered recipes
    • clearRecipes

      public void clearRecipes()
      Clear all recipes in the manager.
    • stopAcceptingIngredientRegistrations

      public void stopAcceptingIngredientRegistrations()
      Declare that this registry is no longer accepting ingredient registrations.

      This method should be called internally by Alchema to ensure that ingredient types are not registered after all plugins have been loaded. It is expected that all calls to registerIngredientType(NamespacedKey, Function) be made in JavaPlugin.onLoad().

    • registerIngredientType

      public void registerIngredientType​(@NotNull @NotNull org.bukkit.NamespacedKey key, @NotNull @NotNull java.util.function.Function<@NotNull com.google.gson.JsonObject,​@NotNull ? extends CauldronIngredient> ingredientProvider)
      Register a new type of CauldronIngredient. This registration should be done during the plugin's load phase (i.e. JavaPlugin.onLoad()).

      NOTE: This method should be called in JavaPlugin.onLoad(). Registrations will no longer be accepted in JavaPlugin.onEnable() and an IllegalStateException will be thrown.

      Parameters:
      key - the ingredient key. Should match that of CauldronIngredient.getKey()
      ingredientProvider - the ingredient provider
    • parseIngredientType

      @Nullable public @Nullable CauldronIngredient parseIngredientType​(@NotNull @NotNull org.bukkit.NamespacedKey key, @NotNull @NotNull com.google.gson.JsonObject object)
      Parse a CauldronIngredient with the ingredient type matching the provided NamespacedKey from a JsonObject.
      Parameters:
      key - the key of the ingredient type to parse
      object - the object from which to parse the ingredient
      Returns:
      the parsed ingredient. null if invalid
    • clearIngredientTypes

      public void clearIngredientTypes()
      Clear all registered ingredient types.
    • loadCauldronRecipes

      @NotNull public @NotNull java.util.concurrent.CompletableFuture<@NotNull RecipeLoadResult> loadCauldronRecipes​(@NotNull @NotNull Alchema plugin, @NotNull @NotNull java.io.File recipesDirectory)
      Asynchronously load all cauldron recipes from Alchema's file system, as well as any recipes from third-party plugins listening to the CauldronRecipeRegisterEvent. The returned CompletableFuture instance provides the load result.
      Parameters:
      plugin - the instance of Alchema (for logging purposes)
      recipesDirectory - the directory from which to load recipes
      Returns:
      a CompletableFuture where the supplied value is the amount of loaded recipes