1.17.x Creating Recipe with Custom Items

Discussion in 'Spigot Plugin Development' started by Mxrlin1, Jun 28, 2021.

  1. Hello, I want to create a recipe with custom items, so I mean with custom displayNames. I found I can use MaterialData, but I've 2 questions about it:
    1 It is deprecated, is there anything other?
    2 How to use it? I saw that: https://github.com/SirCodalot/Vehic...2653597/src/Vehicraft/Objects/Recipe.java#L20 but didnt really understand why he get all the datas from the durability? Is In the durability all the stuff like displayname, lore ... in?
     
  2. MaterialData is deprecated since 1.13 because mojang changed magic number to datas (e.g. to setblock cobblestone stairs with a facing, before it was a number like 9 or 10 but now you have to add [facing=north] or something). Don't use MaterialData in 1.17, it's really deprecated and outdated.

    For recipe, specifically ShapedRecipe and ShapelessRecipe, you can easily add Material to the recipe, but you can also add a RecipeChoice instead of material. With RecipeChoice, you can :
    1. Add a MaterialChoice. For example if you want to add a craft that allows every wood logs, you can set add a Tag<Material> to represents Logs, or you can add a list of material if you want more particular and non-vanilla list of materials (it's like with WorldEdit mask, you can do e.g. "//replace ##logs air" to replace all material in tag Logs)
    2. Add an ExactChoice. The javadoc says : Only valid for shaped recipes. Here you can set ItemStack (a single item or more possibilities) that will be allowed in your recipe. That's exactly what you need : create or take an example of ItemStack you want in your recipe, create an object of RecipeChoice.ExactChoice with this (or those) item(s), then add the RecipeChoice in your recipe.


    I didn't read carefuly the github you linked but it seems to be a recipe loader by deserialize items from a .yml or something.
     
    • Like Like x 1
  3. First of all, this is a wonderful response, 100% accurate, works for me all the time.
    Second, I'd like to expand on this answer, more accurately when to choose each of the options:
    #1: use MaterialChoice when you want multiple kinds of Materials, so if you want a specific item (for example some specific block with a specific enchantment or whatever) then this is not the choice - use #2.
    #2: use this if just the Material of the item isn't enough. Do note though that this way requires the item to be EXACTLY the same as you define it in the code - literally any difference (different durability, enchantment, name, color - whatever) will not wield the result.

    If you do end up using #2 this is how to do it:
    Code (Java):
    ItemStack item;
    ShapedRecipe recipe;
    .
    .
    .
    recipe.setIngredient('?', new RecipeChoice.ExactChoice(item));
    Code (Java):
    List<ItemStack> items;
    ShapedRecipe recipe;
    .
    .
    .
    recipe.setIngredient('?', new RecipeChoice.ExactChoice(items));

    One last note: when using #2 there's a small problem with the recipe book with potions: when selecting a recipe in your recipe book (while having all of the correct items in the bag) the items should be placed in the crafting grid accordingly, but this will NOT put any specific potions in the grid if they're part of the recipe (no idea why - been trying to figure that out for a long time, just attributing this to Mojang-tardness...)
     
    • Like Like x 1
  4. How do you get the crafted item from this ShapedRecipe()?
    When using PrepareItemCraftEvent.getResult does not == ItemStack.customItem2()
    There seems to be a conflict between the 2 methods when using ShapedRecipe() with a customItem() to create a result == customItem2() and vanilla items in either result case.
     
    #4 Goldentoenail, Jun 29, 2021
    Last edited: Jun 29, 2021
  5. Don't do "==", do ".equals()"
    And to get the result just use ".getResult()"
     
    • Like Like x 1
  6. I solved my ShapedRecipe problem which I posted here