Arrows duplicating in PrepareItemCraftEvent

Discussion in 'Plugin Development' started by torpkev, Nov 19, 2018.

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

    torpkev

    I have some shaped recipes, and want to use the resulting item as a crafting item.

    After doing some reading, it seems like the PrepareItemCraftEvent is the correct place to do this, however I'm running into an issue where when I use multiple arrows, they remain on the crafting table and their count jumps after I take the item from crafting table.

    Images below show what I mean

    [​IMG]

    [​IMG]

    As you can see, I have 32 "custom" arrows in-hand - but now I suddenly have 62 arrows sitting in the crafting table.

    I'm using the code below, can someone please point me in the right direction of why this is happening and how to avoid it?

    Code:
    @EventHandler
        public void onPrepareCraft(PrepareItemCraftEvent  evt) {
            CraftingInventory ci = evt.getInventory();
            ItemStack istack4 = ci.getMatrix()[4];
            if (istack4 != null)
            {
                if (istack4.getType() == Material.ROTTEN_FLESH && istack4.getAmount() == 1 && istack4.getItemMeta() != null)
                {
                    ItemMeta im = istack4.getItemMeta();
                    if (im.getDisplayName().equals("§aInfected Flesh"))
                    {
                        ItemStack istack5 = ci.getMatrix()[5];
                        if (istack5 != null)
                        {
                            if (istack5.getType() == Material.ARROW)
                            {
                                ItemStack istack = new ItemStack(Material.ARROW);
                                ItemMeta imeta = istack.getItemMeta();
                                List<String> lore = new ArrayList<String>();                                       
                                lore.add("Infected Arrow to spread infection");
                                imeta.setLore(lore);
                                imeta.setDisplayName("§aInfected Arrow");
                                istack.setItemMeta(imeta); // Set the meta data to the item
                                istack.setAmount(istack5.getAmount());
                                ci.setResult(istack);
                            }
                        }
                    }
                }
            }
         }
    
    Also, as a quick follow-up - is there a way to tell if I'm crafting in the 2x2 or the 3x3?

    Thanks
     
  2. @torpkev

    I have encountered the same problem a while ago. There are 2 ways to solve this:

    Register your recipe properly with Bukkit.addRecipe and forget about this event.
    That is the nice and clean solution and it should be possible in your case since you don't have any complex ingredients.

    The other way (which I did and you should only do if there is no alternative) is to schedule a delayed task that divides the amount of the remaining ingredients by 2.
     
  3. Offline

    torpkev

    Hi @knokko - I'm using an ingredient which I've created from a shapedrecipe - is there some way to add a custom ingredient to a recipe?

    When I'm adding my ingredients to my recipe, the ingredient Item is a Material - my Itemstack has a material, but would lose its metadata I'd think?
     
  4. @torpkev

    There is a very important difference between an ingredient of a recipe and the result.
    The result can be any ItemStack and it will keep all of it's lore, attribute modifiers, enchantments...
    The ingredient system however, is very limited. If you need custom items as ingredients, you might want to consider option 2...
     
  5. Offline

    torpkev

    @knokko I get the difference.. just a shame I can't just get it working. For now I've been very selective in my crafting recipe so that I'm not returning a result unless arrows = 1 in each slot (basically just surrounding my middle item with arrows).

    Is this a bug in spigot?

    Also.. is there a way to tell the difference between 2x2 crafting and 3x3?

    Thanks
     
  6. @torpkev

    As far as I can see, the only ingredients you are using are arrows and rotten flesh. Those are no complex ingredients (even though the result is complex), so you should be able to create an instance of ShapedRecipe in the onEnable method of your plug-in and register it with Bukkit.addRecipe.

    This also happens in Bukkit, so spigot has little to do with it (its just a copy of bukkit with a few extra things). It is not considered a bug because this only happens if you don't register your recipes properly. Everything would work fine if you would use the proper Bukkit.addRecipe.

    If you want to see the difference between 2x2 and 3x3 crafting, you should use event.getInventory().getContents().length.
    Since I don't know exactly what they return, you should first print those values before using them to see the difference.
     
  7. Offline

    torpkev

    Got ya. I'm actually using rotten flesh that is the result of a shapedrecipe (infected flesh).

    Thanks for the tip with the 2x2 / 3x3 - It got me started and I found the following works nicely for it:

    Code:
    CraftingInventory ci = event.getInventory();
    int craftSize = ci.getMatrix().length;
    // craftSize will be 4 if its a 2x2 and 9 if its the 3x3 grid
    
     
  8. @torpkev

    Nice that the crafting size works.
    Do you still need help with the duplicate glitch?
     
  9. Offline

    torpkev

    If there was a proper fix, sure - but i can work around it for now, thanks!
     
Thread Status:
Not open for further replies.

Share This Page