Package wtf.choco.alchema.crafting
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 Summary
Constructors Constructor Description CauldronRecipeRegistry()
-
Method Summary
Modifier and Type Method Description void
clearIngredientTypes()
Clear all registered ingredient types.void
clearRecipes()
Clear all recipes in the manager.@Nullable CauldronRecipe
getApplicableRecipe(@NotNull java.util.List<@NotNull CauldronIngredient> ingredients)
Get theCauldronRecipe
that applies given a set of ingredients.@Nullable CauldronRecipe
getApplicableRecipe(@NotNull java.util.List<@NotNull CauldronIngredient> ingredients, boolean mostComplex)
Get theCauldronRecipe
that applies given a set of ingredients.@NotNull java.util.List<@NotNull CauldronRecipe>
getApplicableRecipes(@NotNull java.util.List<@NotNull CauldronIngredient> ingredients)
Get a list ofCauldronRecipes
that apply given a set of ingredients sorted by their complexity (0th index = most complex, last index = least complex).@NotNull java.util.Collection<@NotNull CauldronRecipe>
getRecipes()
Get a collection of all registered recipes.@NotNull java.util.concurrent.CompletableFuture<@NotNull RecipeLoadResult>
loadCauldronRecipes(@NotNull Alchema plugin, @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 theCauldronRecipeRegisterEvent
.@Nullable CauldronIngredient
parseIngredientType(@NotNull org.bukkit.NamespacedKey key, @NotNull com.google.gson.JsonObject object)
Parse aCauldronIngredient
with the ingredient type matching the providedNamespacedKey
from aJsonObject
.void
registerCauldronRecipe(@NotNull CauldronRecipe recipe)
Register aCauldronRecipe
to be used by anyAlchemicalCauldron
.void
registerIngredientType(@NotNull org.bukkit.NamespacedKey key, @NotNull java.util.function.Function<@NotNull com.google.gson.JsonObject,@NotNull ? extends CauldronIngredient> ingredientProvider)
Register a new type ofCauldronIngredient
.void
stopAcceptingIngredientRegistrations()
Declare that this registry is no longer accepting ingredient registrations.@Nullable CauldronRecipe
unregisterCauldronRecipe(@NotNull org.bukkit.NamespacedKey key)
Unregister aCauldronRecipe
associated with the provided ID.void
unregisterCauldronRecipe(@NotNull CauldronRecipe recipe)
Unregister aCauldronRecipe
.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
CauldronRecipeRegistry
public CauldronRecipeRegistry()
-
-
Method Details
-
registerCauldronRecipe
Register aCauldronRecipe
to be used by anyAlchemicalCauldron
.- Parameters:
recipe
- the recipe to register
-
unregisterCauldronRecipe
Unregister aCauldronRecipe
. 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 aCauldronRecipe
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 theCauldronRecipe
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 ingredientsmostComplex
- 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 theCauldronRecipe
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 ofCauldronRecipes
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
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 inJavaPlugin.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 ofCauldronIngredient
. 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 inJavaPlugin.onEnable()
and an IllegalStateException will be thrown.- Parameters:
key
- the ingredient key. Should match that ofCauldronIngredient.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 aCauldronIngredient
with the ingredient type matching the providedNamespacedKey
from aJsonObject
.- Parameters:
key
- the key of the ingredient type to parseobject
- 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 theCauldronRecipeRegisterEvent
. The returnedCompletableFuture
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
-