|
| 1 | +--- |
| 2 | +slug: /dev/recipes |
| 3 | +description: How to create and manage recipes. |
| 4 | +--- |
| 5 | + |
| 6 | +# Recipe API |
| 7 | + |
| 8 | +Recipes are a way to define a way to craft a particular item. They are defined by a plugin or |
| 9 | +datapack, however we are only going to cover the plugin side of things here. |
| 10 | + |
| 11 | +## <Javadoc name={"org.bukkit.inventory.ShapedRecipe"}>`ShapedRecipe`</Javadoc> |
| 12 | + |
| 13 | +A shaped recipe is a recipe that requires a specific pattern of items in the crafting grid to craft an item. |
| 14 | +These are created using a pattern string and a map of characters to items. The pattern strings are 3, |
| 15 | +3-character strings that represent the rows of the crafting grid. They can be created as follows: |
| 16 | + |
| 17 | +```java |
| 18 | +public class TestPlugin extends JavaPlugin { |
| 19 | + @Override |
| 20 | + public void onEnable() { |
| 21 | + NamespacedKey key = new NamespacedKey(this, "WarriorSword"); |
| 22 | + ItemStack item = new ItemStack(Material.DIAMOND_SWORD); |
| 23 | + |
| 24 | + ShapedRecipe recipe = new ShapedRecipe(key, item); |
| 25 | + recipe.shape(" A ", "AAA", " B "); |
| 26 | + recipe.setIngredient('A', Material.DIAMOND); |
| 27 | + recipe.setIngredient('B', Material.STICK); |
| 28 | + |
| 29 | + getServer().addRecipe(recipe); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +This recipe would require a diamond sword to be crafted with a diamond in the top row, a stick in |
| 35 | +the middle row, and a diamond in the bottom row. The diamond sword would be in the middle column of |
| 36 | +the bottom row. The result would look like this in the crafting grid: |
| 37 | + |
| 38 | +``` |
| 39 | + A |
| 40 | +AAA |
| 41 | + B |
| 42 | +``` |
| 43 | + |
| 44 | +:::info |
| 45 | + |
| 46 | +You do not need to register the recipe within your plugin's `onEnable` method, You can register it |
| 47 | +at any time. However, if you do not register it after the plugin has been enabled and there are |
| 48 | +players online, you will need to either resend all the recipes to the players or use the boolean |
| 49 | +parameter in the <Javadoc name={"org.bukkit.Server#addRecipe(org.bukkit.inventory.Recipe,boolean)"}>`addRecipe`</Javadoc> |
| 50 | +method to update all players with the new recipe. |
| 51 | + |
| 52 | +::: |
| 53 | + |
| 54 | +:::warning |
| 55 | + |
| 56 | +You cannot use Air as a material in a shaped recipe, this will cause an error. |
| 57 | + |
| 58 | +::: |
| 59 | + |
| 60 | + |
| 61 | +## <Javadoc name={"org.bukkit.inventory.ShapelessRecipe"}>`ShapelessRecipe`</Javadoc> |
| 62 | + |
| 63 | +A shapeless recipe is a recipe that requires a specific number of items in the crafting grid to craft an item. |
| 64 | +These are created using a list of items. They can be created as follows: |
| 65 | + |
| 66 | +```java |
| 67 | +public class TestPlugin extends JavaPlugin { |
| 68 | + @Override |
| 69 | + public void onEnable() { |
| 70 | + NamespacedKey key = new NamespacedKey(this, "WarriorSword"); |
| 71 | + ItemStack item = new ItemStack(Material.DIAMOND_SWORD); |
| 72 | + |
| 73 | + ShapelessRecipe recipe = new ShapelessRecipe(key, item); |
| 74 | + recipe.addIngredient(3, Material.DIAMOND); |
| 75 | + recipe.addIngredient(2, Material.STICK); |
| 76 | + |
| 77 | + getServer().addRecipe(recipe); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +This recipe declares that you simply need 3 diamonds and 2 sticks to craft the item, without any specific |
| 83 | +orientation of the cross pattern in the crafting grid. This could be crafted in any of the following ways: |
| 84 | +``` |
| 85 | + DSS | SDS | S D |
| 86 | + D | D | D |
| 87 | + D | D | D S |
| 88 | +``` |
| 89 | +And, any other composition of the 5 items. |
0 commit comments