Interface IReplacementRule

All Known Implementing Classes:
FullIngredientReplacementRule, IngredientReplacementRule, StackTargetingReplacementRule

public interface IReplacementRule
Represents a rule used for replacement of various ingredients inside a recipe.

Each replacement rule is responsible for replacing ingredients based on their type class and the rule that the implementation defines. It is not required for a replacement rule to apply the same way to different ingredient types nor to know how to apply itself to every possible ingredient type.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final IReplacementRule
    Represents a rule that does nothing.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Optional<T>
    chain(Optional<T>... optionals)
    Chains a series of Optionals together, returning the first non-empty one, if available.
    Describes in a short and simple sentence the behavior of this rule.
    <T, U extends net.minecraft.world.item.crafting.Recipe<?>>
    Optional<T>
    getReplacement(T ingredient, Class<T> type, U recipe)
    Attempts to replace the given ingredient, with type type, according to the rules defined by this replacement rule.
    static <T, U, S extends net.minecraft.world.item.crafting.Recipe<?>>
    Optional<T>
    withType(T ingredient, Class<T> type, S recipe, Class<U> targetedType, BiFunction<U,S,Optional<U>> producer)
    Attempts to apply the given producer if the ingredient type matches the specified one.
  • Field Details

  • Method Details

    • chain

      @SafeVarargs static <T> Optional<T> chain(Optional<T>... optionals)
      Chains a series of Optionals together, returning the first non-empty one, if available.
      Type Parameters:
      T - The type parameter of the various optionals.
      Parameters:
      optionals - The series of Optionals to check.
      Returns:
      The first non-empty optional, if present, or Optional.empty() otherwise.
      See Also:
    • withType

      static <T, U, S extends net.minecraft.world.item.crafting.Recipe<?>> Optional<T> withType(T ingredient, Class<T> type, S recipe, Class<U> targetedType, BiFunction<U,S,Optional<U>> producer)
      Attempts to apply the given producer if the ingredient type matches the specified one.

      The function effectively represents the replacement rule that will be applied to ingredient, as long as type is the same as targetedType. This effectively provides a self-contained method to perform these checks without having to resort to unchecked casts in the implementation of the replacement rule.

      If the ingredient doesn't match the given type, then it's assumed that the given producer cannot operate on the ingredient, and Optional.empty() is returned.

      Type Parameters:
      T - The type of the ingredient that is passed to the function; its value should match the one of getReplacement(Object, Class, Recipe).
      U - The type of the ingredient that the producer recognizes.
      S - The type of the recipe that is currently being replaced; its value should match the one of getReplacement(Object, Class, Recipe).
      Parameters:
      ingredient - The ingredient that should be replaced; its value should match the input of getReplacement(Object, Class, Recipe).
      type - The type of the ingredient that should be replaced; its value should match the input of getReplacement(Object, Class, Recipe).
      recipe - The recipe that is currently being acted upon, or null if this information cannot be provided; its value should match the input of getReplacement(Object, Class, Recipe).
      targetedType - The type the ingredient should have for it to be operated upon by the producer. This value will be compared to type with a direct equality check (i.e. type == targetedType).
      producer - A BiFunction that takes an ingredient of type targetedType and the targeted recipe as an input and replaces the ingredient, returning either an Optional with the ingredient, or Optional.empty() if the ingredient cannot be replaced.
      Returns:
      An Optional containing the replaced ingredient, if type matches targetedType and producer determines that a replacement of the ingredient is needed; Optional.empty() in all other cases.
      See Also:
    • getReplacement

      <T, U extends net.minecraft.world.item.crafting.Recipe<?>> Optional<T> getReplacement(T ingredient, Class<T> type, U recipe)
      Attempts to replace the given ingredient, with type type, according to the rules defined by this replacement rule.
      Type Parameters:
      T - The type of the ingredient that should be replaced.
      U - The type of the recipe that is currently being replaced.
      Parameters:
      ingredient - The ingredient that should be replaced.
      type - The type of the ingredient that should be replaced. Its value may or may not correspond to the actual ingredient's class, although it's guaranteed to be one of its superclasses (in other words, it is guaranteed that type.isAssignableFrom(ingredient.getClass()), but the equality check type == ingredient.getClass() is not guaranteed).
      recipe - The recipe that is currently being subjected to replacement, if any; null otherwise.
      Returns:
      An Optional containing the replaced ingredient, if this rule knows how to operate on the ingredient's type and deems that the ingredient should be replaced; Optional.empty() otherwise.
    • describe

      String describe()
      Describes in a short and simple sentence the behavior of this rule.
      Returns:
      The description of this rule.