Replacer
Link to replacer
Handles the replacing of ingredients in recipes for various IRecipeManager<T>s and Recipe<C>s.
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(craftingTable).replace(<item:minecraft:string>, <item:minecraft:diamond>).execute();
Importing the class
Link to 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 Copyimport crafttweaker.api.recipe.Replacer;
Static Methods
Link to static-methods
Name: 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();
Name: forAllTypesExcluding
Creates a Replacer
that will perform replacements on all IRecipeManager<T>s 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);
Parameter | Type | Description |
---|---|---|
Parameter managers | Type IRecipeManager[] | Description The managers to exclude from the replacer. |
Name: forCustomRecipeSet
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<C> that indicates the recipe that is currently
being tested, whereas the second is the IRecipeManager<T> 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.
Returns: A new Replacer
that uses the given function for whitelisting.
Return Type: Replacer
ZenScript Copy// Replacer.forCustomRecipeSet(function as BiPredicate<Recipe,IRecipeManager>) as Replacer
Replacer.forCustomRecipeSet(myPredicate);
Parameter | Type | Description |
---|---|---|
Parameter function | Type BiPredicate<Recipe,IRecipeManager> | Description The custom whitelisting function. |
Name: 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
.
Return Type: Replacer
ZenScript Copy// Replacer.forEverything() as Replacer
Replacer.forEverything();
Name: forMods
Creates a Replacer
that targets only the given mods.
In other words, the replacer will perform ingredient replacement across all
IRecipeManager<T>s, targeting only the recipes added by the specified mods.
Returns: A new Replacer
that targets only the specified mods.
Return Type: Replacer
ZenScript Copy// Replacer.forMods(mods as string[]) as Replacer
Replacer.forMods("minecraft");
Parameter | Type | Description |
---|---|---|
Parameter mods | Type string[] | Description The mods whose recipes should be targeted by the replacer. It must be at least one. |
Name: forOutput
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.
Returns: A new Replacer
for recipes with the given output and an optional whitelist.
Return Type: Replacer
ZenScript Copy// Replacer.forOutput(output as IIngredient, whitelist as IRecipeManager[]) as Replacer
Replacer.forOutput(<tag:items:forge:rods/wooden>, stoneCutter);
Parameter | Type | Description |
---|---|---|
Parameter output | Type IIngredient | Description The output that should be matched. |
Parameter whitelist | Type IRecipeManager[] | Description An optional list of managers that should be whitelisted in the replacement. |
Name: forRecipes
Creates a Replacer
that targets only the specified Recipe<C>s.
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 Recipe[]) as Replacer
Replacer.forRecipes(craftingTable.getRecipeByName("minecraft:emerald_block"));
Parameter | Type | Description |
---|---|---|
Parameter recipes | Type Recipe[] | Description The recipes that should be targeted by the replacer. It must be at least one. |
Name: forRegexRecipes
Creates a Replacer
that will perform replacement on all recipes whose names match the given regular
expression.
Returns: A new Replacer
for recipes that satisfy the given regular expression.
Return Type: Replacer
ZenScript Copy// Replacer.forRegexRecipes(regex as string) as Replacer
Replacer.forRegexRecipes("\\d_\\d");
Parameter | Type | Description |
---|---|---|
Parameter regex | Type string | Description The regular expression that should be used for matching. |
Name: forRegexTypes
Creates a Replacer
that will perform replacement on all IRecipeManager<T>s 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.
Returns: A new Replacer
for managers that satisfy the given regular expression.
Return Type: Replacer
ZenScript Copy// Replacer.forRegexTypes(regex as string) as Replacer
Replacer.forRegexTypes("^minecraft:[a-z]*ing");
Parameter | Type | Description |
---|---|---|
Parameter regex | Type string | Description The regular expression that should be used for matching. |
Name: forTypes
Creates a Replacer
that targets only the specified IRecipeManager<T>s.
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);
Parameter | Type | Description |
---|---|---|
Parameter managers | Type IRecipeManager[] | Description The managers that will be targeted by the replacer. It must be at least one. |
Methods
Link to methods
Name: excluding
Excludes a set of managers from undergoing replacement.
Returns: A Replacer that excludes the given set of recipe managers.
Return Type: Replacer
ZenScript Copy// Replacer.excluding(managers as IRecipeManager[]) as Replacer
Replacer.forEverything().excluding(stoneCutter);
Parameter | Type | Description |
---|---|---|
Parameter managers | Type IRecipeManager[] | Description The list of managers that should be excluded. |
Name: excluding
Excludes a set of recipes, identified by their name, from undergoing replacement.
Returns: A Replacer that excludes the given set of recipes.
Return Type: Replacer
ZenScript Copy// Replacer.excluding(recipes as ResourceLocation[]) as Replacer
Replacer.forEverything().excluding(<resource:minecraft:comparator>);
Parameter | Type | Description |
---|---|---|
Parameter recipes | Type ResourceLocation[] | Description The list of recipes that should be excluded. |
Name: excludingMods
Excludes all recipes that are under the given modids from undergoing replacement.
Returns: A Replacer that excludes the given set of modids.
Return Type: Replacer
ZenScript Copy// Replacer.excludingMods(modids as string[]) as Replacer
Replacer.forEverything().excludingMods("mekanism");
Parameter | Type | Description |
---|---|---|
Parameter modids | Type string[] | Description The list of modids that should be excluded. |
Name: execute
Executes all replacements that have been queued on this replacer, if any.
ZenScript Copy// Replacer.execute()
Replacer.forEverything().execute();
Name: 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 NameUtil#fixing(String).
Returns: A Replacer that will rename the recipe according to the specified rule.
Return Type: Replacer
ZenScript Copy// Replacer.explicitlyRename(oldName as ResourceLocation, newName as string) as Replacer
Replacer.forEverything().explicitlyRename(<resource:minecraft:birch_sign>, "damn_hard_birch_sign");
Parameter | Type | Description |
---|---|---|
Parameter oldName | Type ResourceLocation | Description The ResourceLocation of the name of the recipe that should be renamed. |
Parameter newName | Type string | Description The new name of the recipe. |
Name: 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.
Returns: A Replacer that will carry out the specified operation.
Return Type: Replacer
ZenScript Copy// Replacer.replace(from as IIngredient, to as IIngredient) as Replacer
Replacer.forEverything().replace(<tag:items:forge:storage_blocks/redstone>, <item:minecraft:diamond_block>);
Parameter | Type | Description |
---|---|---|
Parameter from | Type IIngredient | Description An IIngredient that will be used to match stacks that need to be replaced. |
Parameter to | Type IIngredient | Description The replacement IIngredient. |
Name: 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.
Returns: A Replacer that will carry out the specified operation.
Return Type: Replacer
ZenScript Copy// Replacer.replace(from as IItemStack, to as IIngredient) as Replacer
Replacer.forEverything().replace(<item:minecraft:coal_block>, <item:minecraft:diamond_block>);
Parameter | Type | Description |
---|---|---|
Parameter from | Type IItemStack | Description An IItemStack that will be used to match stacks that need to be replaced. |
Parameter to | Type IIngredient | Description The replacement IIngredient. |
Name: replaceFully
Replaces every instance of the target from
IIngredient with the to
one.
This replacement behavior is not recursive, meaning that the IIngredient
s 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.
Returns: A Replacer that will carry out the specified operation.
Return Type: Replacer
ZenScript Copy// Replacer.replaceFully(from as IIngredient, to as IIngredient) as Replacer
Replacer.forEverything().replaceFully(<tag:items:minecraft:anvil>, <tag:items:minecraft:flowers>);
Parameter | Type | Description |
---|---|---|
Parameter from | Type IIngredient | Description An IIngredient that will be used to match to specify the ingredient to replace. |
Parameter to | Type IIngredient | Description The replacement IIngredient. |
Name: 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 this#excluding(ResourceLocation...) would prove to be too cumbersome.
Returns: A Replacer with replacement warnings suppressed.
Return Type: Replacer
ZenScript Copy// Replacer.suppressWarnings() as Replacer
Replacer.forEverything().suppressWarnings();
Name: useForRenaming
Specifies the BiFunction<T,U,R> 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.
Returns: A Replacer that will use the given function for renaming.
Return Type: Replacer
ZenScript Copy// Replacer.useForRenaming(function as BiFunction<ResourceLocation,string,string>) as Replacer
Replacer.forEverything().useForRenaming(myFunction);
Parameter | Type | Description |
---|---|---|
Parameter function | Type BiFunction<ResourceLocation,string,string> | Description The renaming function. |