Tutorial How to make custom crafting recipes...

Discussion in 'Resources' started by JTGaming2012, Aug 9, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    JTGaming2012

    Hello guys,
    Today I want to show you how to add some custom recipes in to your Minecraft server. Custom recipes add a lot of uniqueness to your server, especially RPGs and survivals! Also, this tutorial is aimed at the beginners who have just started to learn the basics of plugin development!

    What we are creating:

    Before we start, let's just get an idea of what our plugin is going to do... I want to add the ability to craft an item which can't currently be crafted in Vanilla Minecraft. How about an Exp Bottle? You can choose a different item like a cobweb or anything!

    So, we are going to simply add a crafting recipe for an Exp Bottle - here's how!

    Basics:

    This is what I currently have inside my new, empty project called ExpRecipe (see, it rhymes!), this is a class I called "Recipe". It is going to be our main class so it must contain our two, "onEnable" and "onDisable" methods.

    Code:java
    1. package me.jack.exprecipe;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Recipe extends JavaPlugin {
    6.  
    7. public void onEnable() {
    8.  
    9. }
    10.  
    11. public void onDisable() {
    12.  
    13. }
    14.  
    15. }


    If you don't know already, the onEnable method gets triggered when the server starts, so it can be used to load in data from a config file. The onDisable method is triggered when the server closes, so in here, we can save data and stuff!

    Because we want to add a recipe when the server starts, we are going to place the code we are about to write, in our "onEnable" method.

    The Exp Bottle:

    Now then, it is going to be useful to create a new ItemStack variable to store the item we want to be the outcome of the crafting recipe.

    We can do this like this:

    Code:java
    1. ItemStack bottle = new ItemStack(Material.EXP_BOTTLE);


    - So, here we are creating a variable which is an ItemStack. This is one of the most commonly used variables!

    - We are calling it "bottle". So when we want to change stuff about this ItemStack, we can refer to it by saying, "bottle".


    - And we are giving it a value of Material Exp Bottle.

    Creating the recipe:

    To create the actually recipe, we need to create a new variable, which is a ShapedRecipe type of variable.

    Code:java
    1. ShapedRecipe expBottle = new ShapedRecipe(bottle);


    - So, we are creating a variable which is a ShapedRecipe.

    - We are calling it "expBottle". So when we want to change things about the new recipe, we can refer to it by saying, "expBottle".

    - This new "expBottle" is going to have the value of a new ShapedRecipe with the outcome of the recipe being the "bottle". Remember, "bottle" is a ItemStack of ExpBottle. So when the player crafts our recipe, the outcome is the Exp Bottle.

    Setting the crafting recipe!

    So now we have a recipe, called, "expBottle". But we haven't told it what shape it is and what items there are!

    There is a very clever way of doing this, simple too!

    Code:java
    1. expBottle.shape("*%*","%B%","*%*");


    - So we are getting the "expBottle" and going to set its shape to those symbols. It looks confusing but is simple to understand! Each string represents a layer of the crafting table and each character represents a different item. We can later set each character to an item!

    Congratz! You just set the shape of your recipe to... Whatever you chose!
    However, now we need to tell the code what each symbol means!

    Code:java
    1.  
    2. expBottle.setIngredient('*', Material.INK_SACK, 2);
    3. expBottle.setIngredient('%', Material.SUGAR);
    4. expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    5.  


    - Once again, we are referring to the, "expBottle" crafting recipe we made, and we are setting what each Item the symbol is.

    Let's have a closer look at one of these.

    Code:java
    1.  
    2. expBottle.setIngredient('*', Material.INK_SACK, 2);
    3.  


    - Ok, so we are getting the "expBottle" and setting the ingredient.

    - The, '*' is referring to that symbol that we set in the shape.

    - And the argument after it, "Material.INK_SACK" is setting that symbol to the material, "Ink Sack".

    - The final argument is the data value of the item. The Ink Sack with the data value of 2 is green dye. Only use this if you know what you're doing!

    Finishing off:

    To finish, we need to add the new crafting recipe to the server, otherwise, it won't work! Here's how!

    Code:java
    1. getServer().addRecipe(expBottle);


    - Simply just gets the server and adds the recipe, "expBottle".

    Final code:

    Code:java
    1. package me.jack.exprecipe;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.inventory.ItemStack;
    5. import org.bukkit.inventory.ShapedRecipe;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Recipe extends JavaPlugin {
    9.  
    10. public void onEnable() {
    11.  
    12. ItemStack bottle = new ItemStack(Material.EXP_BOTTLE, 1);
    13.  
    14. ShapedRecipe expBottle = new ShapedRecipe(bottle);
    15.  
    16. expBottle.shape("*%*","%B%","*%*");
    17.  
    18. expBottle.setIngredient('*', Material.INK_SACK, 2);
    19. expBottle.setIngredient('%', Material.SUGAR);
    20. expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    21.  
    22. getServer().addRecipe(expBottle);
    23.  
    24. }
    25.  
    26. public void onDisable() {
    27.  
    28. }
    29.  
    30. }


    Plugin yml:

    Code:
    name: ExpRecipe
    main: me.jack.exprecipe.Recipe
    version: 1.0
    description: >
                Crafting recipes!
    commands:
     
  2. Offline

    lukasmcd14

    you seem to be missing expBottle in your final code :p
     
  3. Offline

    JTGaming2012

    lukasmcd14 Ah, thanks! Would never have noticed that. :)
     
    lukasmcd14 likes this.
  4. Offline

    gabe4356

    How would you make it have a custom name?
     
  5. Offline

    codex01

    gabe4356 I think you have to give the ItemStack bottle a custom name
     
  6. Offline

    gabe4356

    codex01 Nevermind, I found out a way.
     
  7. Offline

    moo3oo3oo3

    How about the crafting in player inventory?
     
  8. Offline

    PocketMines

    Okay, so I followed this, but for an unknown reason it will not work. It does for other recipes on another plugin I tried...
    Code:java
    1. package com.mcpestuffs.dreamtime;
    2.  
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.inventory.ShapedRecipe;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public void onEnable(){
    12. ItemStack rum = new ItemStack(Material.MILK_BUCKET, 1);
    13. ShapedRecipe craftRum = new ShapedRecipe(rum);
    14. craftRum.shape("%*%","%$%","%$%");
    15. craftRum.setIngredient('%', Material.EMERALD);
    16. craftRum.setIngredient('$', Material.SUGAR_CANE);
    17. craftRum.setIngredient('*', Material.BUCKET);
    18. getServer().addRecipe(craftRum);
    19. }
    20. }
    21.  
     
  9. Offline

    AronTheGamer

    PocketMines Are you sure your plugin.yml is set up correctly? Can you post it here?
     
  10. Offline

    PocketMines

    Ohhhh, thanks for bringing that up. I had a stray capital letter- whoops :D
     
  11. Offline

    BrickBoy55

    I know this thread is a little bit old but I would like an answer to the following question.
    When you put in a custom recipe, how would I make it go to a new event for the item that will be crafted?
     
  12. Offline

    Gater12

    BrickBoy55
    Could you elaborate on "go to a new event for the item that will be crafted"?
     
  13. Offline

    BrickBoy55

    I guess maybe go to a new event like:
    public void craft() {
    Random random = new Random();
    int number = random.nextInt(5);

    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);

    if (number == 1) {
    sword.addEnchantment(Enchantment.UNBREAKING, 3);
    player.getInventory().addItem(sword);
    }

    etc...
    }
     
  14. Offline

    BrickBoy55

  15. Offline

    PappaProntaTeam

    How to create this recipe ?
    [​IMG]

    I try this code but not function :/
    Code:
    ItemStack mazza = new ItemStack(Material.WOOD_SWORD, 1);
    
            ShapedRecipe ferrata = new ShapedRecipe(mazza);
    
            ferrata.shape(" * "," * ","%*%");
    
            ferrata.setIngredient('*', Material.STICK);
            ferrata.setIngredient('%', Material.IRON_INGOT);
    
            getServer().addRecipe(ferrata);
     
  16. @PappaProntaTeam You don't need to add a amount of one in the ItemStack, it is already defaultly one.
     
  17. Offline

    yupie_123

    In ferrata.Shape(), the first string is the TOP layer of the crafting table, the last string is the bottom one. You got the 2 mixed up!
    @PappaProntaTeam
     
  18. Offline

    MarkStam

    Try this:
    Code:
    ferrata.shape("%*%"," * "," * ");
     
  19. Offline

    Frostblockers

    how do you put permissions that player needs on this
     
  20. Offline

    BrickBoy55

    Try the CraftItemEvent
     
  21. Offline

    DeletedUser

    I am making a 2x2 Crafting recipe. How do I make it so I can put it any 4 corners of the table?

    Also I found a bug in the code. I use a plugin called plugman that allows me to reload plugins individually making it easier to reload a plugin without reloading the whole server. I reloaded the plugin when I went to update a crafting recipe but it didn't change because it didn't remove the old recipe in the onDisable(). If I get it to clear recipes then no crafting recipe is craftable at all till I reload the entire server (because bukkit then reloads the recipes.). Is there a way to remove a specific crafting recipe?

    --

    May someone assist me with this?


    {{Posts merged by Myrathi}}
     
    Last edited by a moderator: Jun 11, 2015
  22. Offline

    Worthless_Hobo

    Reloading a plugin really isn't good practice, I know that plugman causes many errors with other plugins and I suggest that you restart the server instead of trying to reload a plugin like that.
    The answer to your question is to simply have the recipe set up like this:
    Code:
    recipe.shape("xy", "xy");
     
  23. @BowWhalley
    Save your recipes somewhere random, and when your plugin disables, iterate over Bukkit.recipeIterator() and check if each recipe is one of your custom ones, if it is, remove it. (Iterator#remove())
     
  24. Offline

    RBHB16

    I pretty much did exactly what he did but changed up the ingredients and it doesn't work for me.
    Code:
    package me.redstery.recipe;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;
    public class main extends JavaPlugin {
    
    public void onEnable() {
    ItemStack bottle = new ItemStack(Material.EXP_BOTTLE, 1);
    ShapedRecipe expBottle = new ShapedRecipe(bottle);
    expBottle.shape("   ", " * ", "   ");
    expBottle.setIngredient('*', Material.DIAMOND);
    getServer().addRecipe(expBottle);
    }
    public void onDisable() {
        getServer().clearRecipes();
    }
    }
    https://gyazo.com/73f8bad8ba3a5ec59ace1168b97b8359

    Can someone explain to me what is going wrong?
     
  25. Offline

    BrickBoy55

    @RBHB16 Is your plugin loading?
     
  26. Offline

    RBHB16

    Yes, I figured it out now.. But thanks for the help I appreciate it
     
  27. Offline

    Grany_poppins28

    I'm making a custom plugin that requires players to craft a spaceship the ItemStacks SmallShip.smallShip and LargeShip.largeShip are static variables of the desired result of the crafting.

    A picture of an attempt to craft SmallShip.smallShip is attached.

    I know the plugin is working because the commands are working.

    Here is my code. The addRecipes() method is called in onEnable(). Both methods are in the main class.
    Code:
    public void addRecipes(){
             
            ShapedRecipe craftSmallShip = new ShapedRecipe(SmallShip.smallShip);
            craftSmallShip.shape("IGI","ICI","IRI");
            craftSmallShip.setIngredient('I', Material.IRON_INGOT);
            craftSmallShip.setIngredient('G', Material.GLASS);
            craftSmallShip.setIngredient('R', Material.REDSTONE_BLOCK);
            craftSmallShip.setIngredient('C', Material.CHEST);
         
            getServer().addRecipe(craftSmallShip);
            //////////////////////////////////////////
            ShapedRecipe craftLargeShip = new ShapedRecipe(LargeShip.largeShip);
            craftLargeShip.shape("ISI","DCD","GRG");
            craftLargeShip.setIngredient('I', Material.IRON_BLOCK);
            craftLargeShip.setIngredient('S', Material.GLASS);
            craftLargeShip.setIngredient('D', Material.DIAMOND_BLOCK);
            craftLargeShip.setIngredient('C', Material.CHEST);
            craftLargeShip.setIngredient('G', Material.GOLD_BLOCK);
            craftLargeShip.setIngredient('R', Material.REDSTONE_BLOCK);
         
            getServer().addRecipe(craftLargeShip);
        }
    Please help! The server I am coding this for needs this plugin to move forward.

    Thanks in advance.

    UPDATE:
    I think I may have found the issue. It is possible that an earlier method in onEnable() is throwing errors and the addRecipes() method isn't getting called. However, I'd still like some help if you can find any flaws in this my code.
     

    Attached Files:

    Last edited: Dec 30, 2015
  28. Offline

    crazicrafter1

    How would you craft with items that have a custom name? Like:
    3 iron ingots renamed: "&bSteel Ingots", to craft a steel Pickaxe.
     
  29. Offline

    Zombie_Striker

    @crazicrafter1
    This is what you set the shaped recipe to craft. Itemstacks have ItemMeta where you can change the durability, lore or displaynames. Just set the bottles metadata to contain the new display name.
     
  30. Offline

    Ikeetjeop

    how do you this in a anvil? Thanks :F
     
Thread Status:
Not open for further replies.

Share This Page