1.12.2 Duplication on custom crafting recipe

Discussion in 'Spigot Plugin Development' started by SeanTMG, Mar 31, 2020.

  1. So I'm basically having the exact same issue as https://www.spigotmc.org/threads/pr...when-more-than-2-of-them-is-presented.408754/. The only problem is that their fix isn't what I need at all.

    I'm basically trying to create custom recipes that require specific metadata. I need the crafting to only be possible if it's a player skull with a specific SkullOwner and a specific displayName/Lore. Now I have this working perfectly, but as mentioned by the creator of that thread if you put more items into the crafting inventory than needed to craft the item some weird duplication happens.

    I'd appreciate any help on this, I've tried every possible way of doing this for hours and I can't figure it out.

    I didn't even know this was possible, and I've been playing Minecraft since 2013.

    [​IMG]
     
  2. Did you use a ShapedRecipe or maybe a ShapelessRecipe?
    Show your code so people can help you further.
     
  3. Neither, as I said I need recipes using custom ItemStacks and Shaped/ShapelessRecipes only take materials and material data.
     
  4. Show us your code.
     
  5. Yeah sorry about that, I'm aware of how messy this code is, I prefer to get things working before cleaning them up.

    This code *does work*, it's the weird duplication that happens on craft if there's more than the amount needed that I'm trying to get rid of.

    Code (Text):
        @EventHandler
        public void onPrepareCraft(PrepareItemCraftEvent event) {
            ItemStack[] recipe = event.getInventory().getMatrix();
            if (recipe[0] == null
                    && recipe[1] == null
                    && recipe[2] == null
                    && recipeEquals(recipe[3], DragonRecipes.getDragonFragment())
                    && recipe[4] == null
                    && recipeEquals(recipe[5], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[6], DragonRecipes.getDragonFragment())
                    && recipe[7] == null
                    && recipeEquals(recipe[8], DragonRecipes.getDragonFragment())) {
                System.out.println("Found boots crafting recipe");
                event.getInventory().setResult(DragonRecipes.getDragonBoots());
            }
            if (recipeEquals(recipe[0], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[1], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[2], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[3], DragonRecipes.getDragonFragment())
                    && recipe[4] == null
                    && recipeEquals(recipe[5], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[6], DragonRecipes.getDragonFragment())
                    && recipe[7] == null
                    && recipeEquals(recipe[8], DragonRecipes.getDragonFragment())) {
                System.out.println("Found leggings crafting recipe");
                event.getInventory().setResult(DragonRecipes.getDragonLeggings());
            }
            if (recipeEquals(recipe[0], DragonRecipes.getDragonFragment())
                    && recipe[1] == null
                    && recipeEquals(recipe[2], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[3], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[4], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[5], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[6], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[7], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[8], DragonRecipes.getDragonFragment())) {
                System.out.println("Found chestplate crafting recipe");
                event.getInventory().setResult(DragonRecipes.getDragonChestplate());
            }
            if (recipeEquals(recipe[0], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[1], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[2], DragonRecipes.getDragonFragment())
                    && recipeEquals(recipe[3], DragonRecipes.getDragonFragment())
                    && recipe[4] == null
                    && recipeEquals(recipe[5], DragonRecipes.getDragonFragment())
                    && recipe[6] == null
                    && recipe[7] == null
                    && recipe[8] == null) {
                System.out.println("Found helmet crafting recipe");
                event.getInventory().setResult(DragonRecipes.getDragonHelmet());
            }
        }
     
  6. If a player is placing too much into the crafting bench, a simple fix for this custom recipe would be adding checks on each ItemStack and make sure the user is only using 1 of each material. If the user isn't, don't set the crafting result. The code would like something like this:
    Code (Text):
    for(int i = 0; i < recipe.length; i++) {
      ItemStack is = recipe[i];
      if(is == null) {
        continue;
      }
      if(is.getAmount() > 1) {
        return;
      }
    }
    Hopefully this helps.
     
  7. Yeah this is actually what I did to get it working, there were a few issues with making it just not set the item so instead I made it stop the crafting if they tried to craft it with too many items, but same idea. Thanks!
     
    • Like Like x 1