Shapeless Recipes

Link to shapeless-recipes

Shapeless Recipes are recipes where the ingredients can be placed in any order in the Crafting Table.
An example of a Shapeless recipe is the Flint and Steel recipe, whre you can place the Flint and Iron Ingot in any slot and in any order in the Crafting Table and it will still produce the same output.

Adding Shapeless Recipes

Link to adding-shapeless-recipes

This method is used to add a Shapeless Crafting recipe to the game.

The basic syntax for a shapeless recipe is:

ZenScript
Copy
// craftingTable.addShapeless(recipeName as string,
//      output as IItemStack,
//      ingredients as IIngredient[],
//      recipeFunction as @optional RecipeFunctionArray);

The optional RecipeFunctionArray at the end of the method is only needed for advanced recipe where the output changes depending on the input, an example being transfering NBT data from one of the input ingredients to the output item.

In general, most of your recipes are not going to be using the function, but it is there if you need it.

The recipeName has some restrictions that should be kept in mind:

  1. The name you provide is the "path" value of the name of the recipe, all recipes added through CraftTweaker will have the crafttweaker modid as part of the name.
    For example, if you provide "boat" as the name, the full recipe name will be crafttweaker:boat.
  2. It needs to be unique, using a non-unique recipe name will cause your recipe to replace the previous recipe.
  3. It cannot contain spaces or colons (:).
  4. It must be all lowercased.
  5. It cannot start with "autogenerated/" as it is a reserved CraftTweaker folder.

For the output, something to note is that it has to be an IItemStack, passing in an IIngredient will not work, so using <tag:items:minecraft:wool> or <item:minecraft:diamond> | <item:minecraft:dirt> will not work as they are both IIngredients, but using <item:minecraft:diamond> is valid and will work.

Here is an example of how to add a Shapeless recipe that will accept a single Dirt in any slot of the Crafting Grid and produce 2 Diamonds.

For a recipe like this, you don't need the recipe function, so it is simply left out.

ZenScript
Copy
craftingTable.addShapeless("dirt2diamond", <item:minecraft:diamond> * 2, [<item:minecraft:dirt>]);

This next Shapeless recipe is an example of how to use the RecipeFunction to change the output based on the input items.

The Shapeless recipe will give a piece of Dirt when a single Diamond is put into any slot in the Crafting Grid, however, if the input Diamond has the name "Super Diamond", it will produce 8 Dirt from a single Diamond.

ZenScript
Copy
craftingTable.addShapeless("diamond2dirt", <item:minecraft:dirt>, [<item:minecraft:diamond>],
(usualOut as IItemStack, inputs as IItemStack[]) => {

    if <item:minecraft:diamond>.withTag({display: {Name: "{\"text\":\"Super Diamond\"}"}}).matches(inputs[0]) {
        return usualOut * 8;
    }

    return usualOut;

});

Another example of how the Recipefunction can be used is to repair items.

The following script will accept a damaged Iron Sword and a Clay Ball and will remove 10 damage from the Iron Sword.

Something to note with this recipe, we are returning the input itemstack as the output Item.
This allows us return the exact itemstack that was used as the input, so if the itemstack had any enchantments or was renamed, it will retain those enchants and name.

Something else to note is the use of onlyDamage() on the Iron Sword input.
This makes it that the recipe will only accept a damaged Iron Sword, if the Iron Sword has full durability, it won't be accepted.

ZenScript
Copy
craftingTable.addShapeless("iron_sword_repair", <item:minecraft:iron_sword>, 
[<item:minecraft:iron_sword>.onlyDamaged(), <item:minecraft:clay_ball>],
(usualOut as IItemStack, inputs as IItemStack[]) => {

	var newDamage = inputs[0].damage - 10;
	if newDamage < 0 {

		newDamage = 0;
	}
    	return inputs[0].withDamage(newDamage);
});