Recipes
Register crafting, cooking, smithing and stonecutter recipes, use your own custom items as ingredients, remove recipes and read the ones the server has.
Register a recipe
The recipe module has a builder for each kind: ShapedRecipeBuilder, ShapelessRecipeBuilder, CookingRecipeBuilder (smelting, blasting, smoking and campfire cooking), SmithingRecipeBuilder, SmithingTrimRecipeBuilder and StonecuttingRecipeBuilder. Build the recipe and register it, for example with register_to_context(&context) in on_load, or through server.get_recipe_manager().
An ingredient is an item id (minecraft:stick) or a tag. Write a tag as #minecraft:planks, or use Ingredient::tag("planks"). The recipe reference lists every call.
- A recipe id is in your script's namespace (your script's name in lowercase). A bare name is put there, and the same id again replaces your recipe.
- An id in
minecraft:, in another namespace, or of a data pack's or another script's recipe is refused. So is a script with 4096 recipes. Where a call returns nothing, the server console says why. - A recipe's result keeps its components, so a recipe can make a custom item.
- Online players see a new recipe at once.
Use only an exact item
A plain ingredient takes any stack of that item. An exact ingredient takes only stacks equal to one of the stacks you give (Paper's RecipeChoice.ExactChoice):
- shaped:
ShapedRecipeBuilder::exact_key('M', [stack])makes the symbolMtake only that stack; - shapeless:
ShapelessRecipeBuilder::exact_ingredient([stack])adds an ingredient that takes only that stack; - cooking:
CookingRecipeBuilder::exact(id, CookingType::Smelting, [stack], result)cooks only that stack, with.experience(xp)and.cooking_time(ticks)as usual.
You can give several stacks, and any one of them is accepted. A stack is accepted when it is the same item with the same components (its name, model and data); the count does not matter. So a custom item made with content::create_item crafts or cooks, while its plain base item and other custom items of that base item do not.
- Exact and plain ingredients can be mixed in one recipe.
- An exact key or ingredient with no stack is refused (
MissingKey,NoIngredients). - A plain ingredient takes a custom item only when the item's settings say
acts_as = true. This holds for vanilla's recipes too: a ruby on a paper base is not paper in a recipe.
The recipe book shows an exact ingredient as its item. A click on the recipe moves into the exact slot only a stack equal to it, never the plain base item, and a shift-click places as many crafts as the exact stacks allow. A player who has only the base item sees the recipe as not craftable.
Smithing recipes
SmithingRecipeBuilder::new(id, base, result) makes a smithing table recipe (Paper's SmithingTransformRecipe). .template(..) and .addition(..) say what goes in those slots; a slot left out must stay empty. Each slot takes an item or a tag, or exact stacks such as your custom items with .exact_base([..]), .exact_template([..]) and .exact_addition([..]).
By default the result keeps the base item's enchantments, name and other data, as the netherite upgrade does; .copy_base_components(false) turns that off.
let manager = context.get_recipe_manager();
SmithingRecipeBuilder::new("ruby_sword", "diamond_sword", ruby_sword)
.template("netherite_upgrade_smithing_template")
.exact_addition([ruby])
.register(&manager)?;Armor trim templates
SmithingTrimRecipeBuilder makes an item of your script the smithing template of a trim pattern: one of the game's, or one your script registered with content::register_trim_pattern. The game's own templates keep working, and a pattern that does not exist is refused with its name.
Where two smithing recipes take the same slots, the one whose id comes first wins, as in vanilla.
Stonecutter recipes
StonecuttingRecipeBuilder::new(id, input, result) cuts one input into the result, with the result's count. StonecuttingRecipeBuilder::exact(id, [stack], result) shows in the stonecutter for that stack's item but only cuts your exact stack.
Cooking recipes
Furnaces, blast furnaces, smokers, campfires and loot tables that smelt their drops cook with every recipe the server has: the game's own, a data pack's and a script's, with the cooking time and experience of the recipe.
- Where a data pack recipe and a script's recipe cook the same item, the script's wins. Where an exact recipe and another one both take a stack, the recipe added last wins.
- A script's cooking recipe takes experience from 0 to 32767 and a cooking time from 1 to 32767 ticks.
Remove a recipe
manager.remove_recipe(id) removes a recipe of any kind by its id until the next /reload (Paper's Bukkit.removeRecipe). It returns Ok(true) when it was removed and Ok(false) when no recipe has that id. An id without a namespace is minecraft:.
- Your script can always remove its own recipes, and never another script's.
- A recipe of the game or of a data pack needs the permission
recipes.remove(permissions::RECIPES_REMOVE). - Online players' recipe books lose it at once.
Reloads
/reloaddrops the scripts' recipes. After a data reload (/reload,/datapack, a script's data pack)ServerLoadEventof the reload kind fires once: register your recipes again there.- When your script is hot-reloaded, the recipes of the previous version are removed before the new version starts, so only the new version's recipes craft. If the new version fails to start, none of its recipes stay.
- Unloading a script without reloading it keeps its recipes until the next
/reloador restart.
Read the recipes
server.get_recipe_manager() reads the recipes the server has: the built-in ones together with those of data packs and scripts.
recipes_for(item, start)returns the recipes that make an item (Paper'sBukkit.getRecipesFor), ordered by id: at most 64 in one answer, fromstarton, withtotalsaying how many there are. Call it again withstart64 for the next ones.get_recipe(id)returns one recipe by its id (Paper'sBukkit.getRecipe).
Each recipe has its id, its kind (shaped, shapeless, transmute, smelting, blasting, smoking, campfire, stonecutting, smithing transform), the pattern and key of a shaped recipe, the ingredients of the other kinds in their slots' order, the result, its group, its category and, for cooking, the experience and cooking time. Tags come without #. Special recipes whose result depends on what you put in (dyeing, fireworks, map and book cloning, armor trims) are not listed.
Read the recipes when you need them instead of keeping your own copy: a data pack or another script may change one.
Unlocking in the recipe book
PlayerRecipeDiscoverEvent fires when the server unlocks a recipe for a player: once for each recipe that is new to the player's recipe book, for Java and Bedrock players. It comes from an advancement reward, /recipe give, a knowledge book, crafting or smelting something for the first time, or picking up an item that unlocks a recipe. recipe_id is the recipe's id, such as minecraft:oak_planks.
Cancel it to keep the recipe out of the player's book; it is offered again the next time the server would unlock it. The other recipes of the same unlock are still added.
A Bedrock player's recipe book does not show your script's recipes.