java.lang.Object
com.blamejared.crafttweaker.api.recipe.replacement.Replacer

@ZenRegister public final class Replacer extends Object
Handles the replacing of ingredients in recipes for various IRecipeManagers and Recipes.

Differently from various other mechanisms in CraftTweaker, each replacement that gets specified from within a Replacer doesn't get run immediately, rather it gets stored and all replacements are then applied together when the execute() method is called. This change is done so that multiple replacements can be performed at the same time, with a net gain on performance.

Note that replacement must be explicitly supported by modded recipe types and managers, meaning that a Replacer may not be able to perform replacement on a certain set of recipes. If this is the case, a warning will be printed in the logs, so that you may review it.

Creating a Replacer gets done via the various forXxx methods, where Xxx identifies any suffix. Refer to the specific documentation to get more information on their behavior.

The various replace methods, listed both in this page and in other mod's possible expansions, then allow you to specify what should be the replacements that need to be carried out by the Replacer itself.

All recipes that get replaced by a Replacer get renamed according to a set naming scheme. You can modify completely by providing a lambda via useForRenaming(BiFunction), or just for a specific set of recipes via explicitlyRename(ResourceLocation, String).

An example usage of a Replacer could be Replacer.forTypes(craftingTable).replace(<item:minecraft:string>, <item:minecraft:diamond>).execute();

DocParam:
this Replacer.forEverything()
  • Method Details

    • forRecipes

      public static Replacer forRecipes(net.minecraft.world.item.crafting.Recipe<?>... recipes)
      Creates a Replacer that targets only the specified Recipes.

      In other words, the replacer will perform ingredient replacement only on the recipes that are given in this list.

      Parameters:
      recipes - The recipes that should be targeted by the replacer. It must be at least one.
      Returns:
      A new Replacer that targets only the specified recipes.
      DocParam:
      recipes craftingTable.getRecipeByName("minecraft:emerald_block")
    • forMods

      public static Replacer forMods(String... mods)
      Creates a Replacer that targets only the given mods.

      In other words, the replacer will perform ingredient replacement across all IRecipeManagers, targeting only the recipes added by the specified mods.

      Parameters:
      mods - The mods whose recipes should be targeted by the replacer. It must be at least one.
      Returns:
      A new Replacer that targets only the specified mods.
      DocParam:
      mods "minecraft"
    • forTypes

      public static Replacer forTypes(IRecipeManager<?>... managers)
      Creates a Replacer that targets only the specified IRecipeManagers.

      In other words, the replacer will perform ingredient replacement only on the managers that are given in this list.

      Parameters:
      managers - The managers that will be targeted by the replacer. It must be at least one.
      Returns:
      A new Replacer that targets only the specified managers.
      DocParam:
      managers smithing
    • forAllTypes

      @Deprecated public static Replacer forAllTypes()
      Deprecated.
      Use forEverything() instead.
      Creates a Replacer that will perform replacements globally.

      In other words, the replacer will perform ingredient replacement on every recipe manager in the game, as long as it supports replacement.

      Returns:
      A new global Replacer.
    • forEverything

      public static Replacer forEverything()
      Creates a Replacer that will perform replacements globally.

      In other words, the replacer will perform ingredient replacement on every recipe manager in the game, as long as it supports replacement.

      Returns:
      A new global Replacer.
    • forAllTypesExcluding

      @Deprecated public static Replacer forAllTypesExcluding(IRecipeManager<?>... managers)
      Deprecated.
      Use forEverything() to create a replacer then use excluding(IRecipeManager...) to exclude the various unwanted managers.
      Creates a Replacer that will perform replacements on all IRecipeManagers except the ones specified.
      Parameters:
      managers - The managers to exclude from the replacer.
      Returns:
      A new Replacer that targets all managers except the ones specified.
      DocParam:
      managers stoneCutter
    • forOutput

      public static Replacer forOutput(IIngredient output, IRecipeManager<?>... whitelist)
      Creates a Replacer that will perform replacement only on recipes with the given output, optionally restricted to a set of whitelisted managers.

      The passed in whitelist may also be empty, in which case it'll be treated as meaning every possible recipe manager. If the whitelist is not empty, on the other hand, only the selected recipe managers will be considered when replacing ingredients.

      Parameters:
      output - The output that should be matched.
      whitelist - An optional list of managers that should be whitelisted in the replacement.
      Returns:
      A new Replacer for recipes with the given output and an optional whitelist.
      DocParam:
      output <tag:items:forge:rods/wooden>, whitelist stoneCutter
    • forRegexTypes

      public static Replacer forRegexTypes(String regex)
      Creates a Replacer that will perform replacement on all IRecipeManagers that match the given regular expression.

      The managers will be matched on their bracket identifier, which corresponds to their bracket expression stripped of <recipetype: and >. E.g., a manager obtained in a script via <recipetype:minecraft:crafting> will be matched on minecraft:crafting only.

      Parameters:
      regex - The regular expression that should be used for matching.
      Returns:
      A new Replacer for managers that satisfy the given regular expression.
      DocParam:
      regex "^minecraft:[a-z]*ing"
    • forRegexRecipes

      public static Replacer forRegexRecipes(String regex)
      Creates a Replacer that will perform replacement on all recipes whose names match the given regular expression.
      Parameters:
      regex - The regular expression that should be used for matching.
      Returns:
      A new Replacer for recipes that satisfy the given regular expression.
      DocParam:
      regex "\\d_\\d"
    • forCustomRecipeSet

      public static Replacer forCustomRecipeSet(BiPredicate<net.minecraft.world.item.crafting.Recipe<?>,IRecipeManager<?>> function)
      Creates a Replacer that will perform replacements only on the recipes whitelisted by the given function.

      The first parameter of the predicate is a Recipe that indicates the recipe that is currently being tested, whereas the second is the IRecipeManager that is responsible for handling that particular type of recipes. The function should then return a boolean that either whitelists the recipe for replacement (true) or blacklists it (false).

      The given function must be a pure function, which means that the output must be the same given the same set of inputs. In other words, you should not rely on external state for this function, since it may be called multiple times on the same set of inputs in the same replacer run.

      Parameters:
      function - The custom whitelisting function.
      Returns:
      A new Replacer that uses the given function for whitelisting.
      DocParam:
      function myPredicate
    • excluding

      public Replacer excluding(net.minecraft.resources.ResourceLocation... recipes)
      Excludes a set of recipes, identified by their name, from undergoing replacement.
      Parameters:
      recipes - The list of recipes that should be excluded.
      Returns:
      A Replacer that excludes the given set of recipes.
      DocParam:
      recipes <resource:minecraft:comparator>
    • excluding

      public Replacer excluding(IRecipeManager<?>... managers)
      Excludes a set of managers from undergoing replacement.
      Parameters:
      managers - The list of managers that should be excluded.
      Returns:
      A Replacer that excludes the given set of recipe managers.
      DocParam:
      managers stoneCutter
    • excludingMods

      public Replacer excludingMods(String... modids)
      Excludes all recipes that are under the given modids from undergoing replacement.
      Parameters:
      modids - The list of modids that should be excluded.
      Returns:
      A Replacer that excludes the given set of modids.
      DocParam:
      modids "mekanism"
    • replace

      public Replacer replace(IIngredient from, IIngredient to)
      Replaces every match of the from IIngredient with the one given in to.

      This replacement behavior is recursive, meaning that any IIngredient that gets found is looped recursively trying to identify matches. As an example, attempting to replace <item:minecraft:stone> with <item:minecraft:diamond> will perform this replacement even in compound ingredients, such as <item:minecraft:stone> | <item:minecraft:gold_ingot> or <tag:items:minecraft:stones> (supposing that minecraft:stones is a tag that contains minecraft:stone among other items).

      If this behavior is not desired, refer to replaceFully(IIngredient, IIngredient) instead.

      This method is a specialized by replace(IItemStack, IIngredient) for IItemStacks and should be preferred in these cases.

      Parameters:
      from - An IIngredient that will be used to match stacks that need to be replaced.
      to - The replacement IIngredient.
      Returns:
      A Replacer that will carry out the specified operation.
      DocParam:
      from <tag:items:forge:storage_blocks/redstone>, to <item:minecraft:diamond_block>
    • replace

      public Replacer replace(IItemStack from, IIngredient to)
      Replaces every match of the from IItemStack with the one given in to.

      This replacement behavior is recursive, meaning that any IIngredient that gets found is looped recursively trying to identify matches. As an example, attempting to replace <item:minecraft:stone> with <item:minecraft:diamond> will perform this replacement even in compound ingredients, such as <item:minecraft:stone> | <item:minecraft:gold_ingot> or <tag:items:minecraft:stones> (supposing that minecraft:stones is a tag that contains minecraft:stone among other items).

      If this behavior is not desired, refer to replaceFully(IIngredient, IIngredient) instead.

      This method is a specialization of replace(IIngredient, IIngredient) for IItemStacks and should be preferred in these cases.

      Parameters:
      from - An IItemStack that will be used to match stacks that need to be replaced.
      to - The replacement IIngredient.
      Returns:
      A Replacer that will carry out the specified operation.
      DocParam:
      from <item:minecraft:coal_block>, to <item:minecraft:diamond_block>
    • replaceFully

      public Replacer replaceFully(IIngredient from, IIngredient to)
      Replaces every instance of the target from IIngredient with the to one.

      This replacement behavior is not recursive, meaning that the IIngredients will be matched closely instead of recursively. As an example, attempting to replace fully <item:minecraft:stone> will only replace ingredients that explicitly specify <item:minecraft:stone> as an input, while compound ingredients such as <item:minecraft:stone> | <item:minecraft:gold_ingot> won't be replaced.

      If this behavior is not desired, refer to replace(IIngredient, IIngredient) instead.

      Parameters:
      from - An IIngredient that will be used to match to specify the ingredient to replace.
      to - The replacement IIngredient.
      Returns:
      A Replacer that will carry out the specified operation.
      DocParam:
      from <tag:items:minecraft:anvil>, to <tag:items:minecraft:flowers>
    • explicitlyRename

      public Replacer explicitlyRename(net.minecraft.resources.ResourceLocation oldName, String newName)
      Indicates that the recipe with the given oldName should be renamed to the newName.

      This rename will only be applied if a replacement is carried out. Moreover, the given new name will also be fixed according to NameUtil.fixing(String).

      Parameters:
      oldName - The ResourceLocation of the name of the recipe that should be renamed.
      newName - The new name of the recipe.
      Returns:
      A Replacer that will rename the recipe according to the specified rule.
      DocParam:
      oldName <resource:minecraft:birch_sign>, newName "damn_hard_birch_sign"
    • useForRenaming

      public Replacer useForRenaming(BiFunction<net.minecraft.resources.ResourceLocation,String,String> function)
      Specifies the BiFunction that will be used for renaming all recipes.

      The first argument to the function is the ResourceLocation that uniquely identifies its name, whereas the second represents the default name for the recipe according to the default replacement rules. The return value of the function will then represent the new name of the recipe.

      Parameters:
      function - The renaming function.
      Returns:
      A Replacer that will use the given function for renaming.
      DocParam:
      function myFunction
    • suppressWarnings

      public Replacer suppressWarnings()
      Suppresses warnings that arise when trying to replace unsupported recipes.

      Additional warnings will not be suppressed. Note that it is suggested to keep this disabled while testing and enable it only if excluding the problematic recipes via excluding(ResourceLocation...) would prove to be too cumbersome.

      Returns:
      A Replacer with replacement warnings suppressed.
    • execute

      public void execute()
      Executes all replacements that have been queued on this replacer, if any.
    • addReplacementRule

      public Replacer addReplacementRule(IReplacementRule rule)