Replacer

Handles the replacing of ingredients in recipes for various IRecipeManagers and WrapperRecipes.

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 this#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 this#useForRenaming(BiFunction), or just for a specific set of recipes via this#explicitlyRename(ResourceLocation, String).

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

Importing the class

It might be required for you to import the package if you encounter any issues (like casting an Array), so better be safe than sorry and add the import at the very top of the file.

ZenScript
Copy
import crafttweaker.api.recipe.Replacer;

Static Methods

forAllTypes

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.
Return Type: Replacer

ZenScript
Copy
// Replacer.forAllTypes() as Replacer

Replacer.forAllTypes();
forAllTypesExcluding

Creates a Replacer that will perform replacements on all IRecipeManagers except the ones
specified.

Returns: A new Replacer that targets all managers except the ones specified.
Return Type: Replacer

ZenScript
Copy
// Replacer.forAllTypesExcluding(managers as IRecipeManager[]) as Replacer

Replacer.forAllTypesExcluding(stoneCutter);
ParameterTypeDescription
managersIRecipeManager[]The managers to exclude from the replacer.
forRecipes

Creates a Replacer that targets only the specified WrapperRecipes.

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

Returns: A new Replacer that targets only the specified recipes.
Return Type: Replacer

ZenScript
Copy
// Replacer.forRecipes(recipes as WrapperRecipe[]) as Replacer

Replacer.forRecipes(craftingTable.getRecipeByName("minecraft:emerald_block"));
ParameterTypeDescription
recipesWrapperRecipe[]The recipes that should be targeted by the replacer. It must be at least one.
forTypes

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.

Returns: A new Replacer that targets only the specified managers.
Return Type: Replacer

ZenScript
Copy
// Replacer.forTypes(managers as IRecipeManager[]) as Replacer

Replacer.forTypes(smithing);
ParameterTypeDescription
managersIRecipeManager[]The managers that will be targeted by the replacer. It must be at least one.

Methods

excluding

Excludes a set of recipes, identified by their name, from undergoing replacement.

Return Type: Replacer

ZenScript
Copy
// Replacer.excluding(recipes as MCResourceLocation[]) as Replacer

Replacer.forAllTypes().excluding(<resource:minecraft:comparator>);
ParameterTypeDescription
recipesMCResourceLocation[]The list of recipes that should be excluded.
execute

Executes all replacements that have been queued on this replacer, if any.

Return Type: void

ZenScript
Copy
// Replacer.execute() as void

Replacer.forAllTypes().execute();
explicitlyRename

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 NameUtils#fixing(String).

Return Type: Replacer

ZenScript
Copy
// Replacer.explicitlyRename(oldName as MCResourceLocation, newName as string) as Replacer

Replacer.forAllTypes().explicitlyRename(<resource:minecraft:birch_sign>, "damn_hard_birch_sign");
ParameterTypeDescription
oldNameMCResourceLocationThe MCResourceLocation of the name of the recipe that should be renamed.
newNamestringThe new name of the recipe.
replace

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 this#replaceFully(IIngredient, IIngredient) instead.

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

Return Type: Replacer

ZenScript
Copy
// Replacer.replace(from as IIngredient, to as IIngredient) as Replacer

Replacer.forAllTypes().replace(<tag:items:forge:storage_blocks/redstone>, <item:minecraft:diamond_block>);
ParameterTypeDescription
fromIIngredientAn IIngredient that will be used to match stacks that need to be replaced.
toIIngredientThe replacement IIngredient.
replace

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 this#replaceFully(IIngredient, IIngredient) instead.

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

Return Type: Replacer

ZenScript
Copy
// Replacer.replace(from as IItemStack, to as IIngredient) as Replacer

Replacer.forAllTypes().replace(<item:minecraft:coal_block>, <item:minecraft:diamond_block>);
ParameterTypeDescription
fromIItemStackAn IItemStack that will be used to match stacks that need to be replaced.
toIIngredientThe replacement IIngredient.
replaceFully

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 this#replace(IIngredient, IIngredient) instead.

Return Type: Replacer

ZenScript
Copy
// Replacer.replaceFully(from as IIngredient, to as IIngredient) as Replacer

Replacer.forAllTypes().replaceFully(<tag:items:minecraft:anvil>, <tag:items:minecraft:flowers>);
ParameterTypeDescription
fromIIngredientAn IIngredient that will be used to match to specify the ingredient to replace.
toIIngredientThe replacement IIngredient.
useForRenaming

Specifies the BiFunction<T,U,R> that will be used for renaming all recipes.

The first argument to the function is the MCResourceLocation 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.

Return Type: Replacer

ZenScript
Copy
// Replacer.useForRenaming(function as BiFunction<MCResourceLocation,string,string>) as Replacer

Replacer.forAllTypes().useForRenaming(myFunction);
ParameterTypeDescription
functionBiFunction<MCResourceLocation,string,string>The renaming function.