1.17.x how to make shaped crafting recipes

Discussion in 'Spigot Plugin Development' started by mason-walker, Jun 25, 2021.

  1. How can I make custom shaped crafting recipes? the only things I have found on it are deprecated.
     
  2. I'd like to help you, since I created one recently.
    Example:
    Code (Java):

    ShapedRecipe something = new ShapedRecipe(
        new NamespacedKey(this, /* the instance of your JavaPlugin class */ "put_a_unique_key_here") /* the key */,
        new ItemStack(Material.OAK_LOG /* put the item that will be crafted*/)
    );
    something.shape(" A ", "BA ", "  C"); /* put any letters you want, and put spaces when there's nothing */
    something.setIngredient('A', Material.ACACIA_DOOR); /* so in the first row in the middle, and in the second row in the middle, the item must be an acacia door */
    something.setIngredient('B', Material.COBBLESTONE); /* so in the second row in the beginning, the item must be cobblestone */
    something.setIngredient('C', Material.DIAMOND_SWORD); /* you got the drill*/
     
    The recipe would look like that:
    Code (Text):

    AIR        ACACIA_DOOR               AIR
    COBBLE     ACACIA_DOOR               AIR
    AIR        AIR                       DIAMOND_SWORD
     
    To register the recipe, do this: Bukkit.addRecipe(something)
    If I helped you, please give me a positive rating under my message.
     
    • Like Like x 1
  3. tysm
     
    • Like Like x 1
  4. If you don't need any help, mark the thread as solved and close it.
     
  5. ok, it didn't work. I Did this
    Code (Java):
    public class EnchantedDiamondRecipe {
        public ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
        public ShapedRecipe EDR = new ShapedRecipe(new NamespacedKey(String.valueOf(this), "key"), item);
        EDR.shape
    }
    but it gives me an error saying "cannot resolve symbol 'shape'" why is this happening?
     
  6. What are you trying to do? First of all, you don't need String.valueOf(this), what you need is to get an instance of your main class. I do it like this (not the best way):
    Code (Java):

    /* in my main class */
    public static Plugin instance;

    public void onEnable() {
        instance = this;
    }
     
    To reference it from other classes, use YourMainClass.instance. So place YourMainClass.instance instead of that valueOf thingy. And now, you are using "shape" without arguments. You just wrote shape and that's it. Pass arguments - I clearly showed that.
     
  7. thats not really the problem, the problem more is that I can't use .shape or .setingredient, and also, yes I know I have to pass arguments, but it still gives the error I said, even with passed arguments.
     
  8. Oh, I see. You are doing everything outside a method. You see, in Java you can't do this for basically no reason whatsoever, or I am dumb and there are millions of reasons for that. Nevertheless, put your entire code into a getter that will return the ShapedRecipe (EDR one), and inside that method you do everything with that recipe.
     
  9. now how am I supposed to call the function I put it in so that it runs when the plugin is started, this is what I have
    Code (Text):
    public class EnchantedDiamondRecipe {
        public void AddRecipe()
        {
             ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
             ShapedRecipe EDR = new ShapedRecipe(new NamespacedKey((Plugin) this, "key"), item);
             EDR.shape(" A ", "AAA", " A ");
             EDR.setIngredient('A', Material.DIAMOND);
             Bukkit.addRecipe(EDR);
        }
    }
     
    • Funny Funny x 1
  10. Please learn the Java basics before trying to write a plugin, this is the last time I am helping you in this thread.
    1) You are not following the naming conventions, functions must be written in lowerCamelCase - the first word is lowercase and others start with a capital letter.
    2) You don't want to add a recipe like that - instead of returning void, return ShapedRecipe. Instead of Bukkit.addRecipe, use return EDR. Then, in your onEnable class, use Bukkit.addRecipe(EnchantedDiamondRecipe.getRecipe()).
    3) What the hell are you doing? I told you to not use "this" in this class. Use an instance of your main class that extends JavaPlugin.
    4) I wouldn't advice using the "key" key, even though that sounds weird. If someone, using your plugin, will use another plugin, that will also create a NamespacedKey with "key" as the key, it will rain errors. Make something unique, that is unlikely to be used by others.
    Now I've told you everything that is necessary to create a recipe and fix your errors. Good luck.