Adding to ItemStack glitchy

Discussion in 'Plugin Development' started by Diemex, Mar 6, 2013.

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

    Diemex

    I'm multiplying the amount of tnt you can craft in the CraftEvent. The first time the event is called it doesn't show the item that is added to the cursor by the event. Say I add 2 items to the cursor, then it only shows 2 items instead of 3 (+ the one from the event). Now if I drop the Itemstack into a slot and pick it up again it works. I'm not modifying the crafting recipe, because I would like to have it on a per-world basis.
    Code:
    public void onItemCrafted(CraftItemEvent event)
        {
        ItemStack cursorStack = event.getCursor();
     
                        int currentAmount = cursorStack.getAmount();
                        int newAmount = currentAmount + amountToAddPerCycle;
                        int maxStackSize = event.getRecipe().getResult().getMaxStackSize();
     
                        if (maxStackSize >= newAmount)
                        {
                            cursorStack.setType(event.getRecipe().getResult().getType());
                            cursorStack.setAmount(newAmount);
                            event.setCursor(cursorStack);
                        }
                        else
                        {
                            event.setCancelled(true);
                        }
    }
    There are a couple of checks that I left out for simplicity.
     
  2. If you want to "increase the amount of TNT crafted" then modify the recipe to craft more. You can remove the old TNT recipe and add a new one. I've never done it myself but I've seen others do it.
     
  3. Offline

    minoneer

    Try to schedule a player.updateInventory() with a 1 tick delay. I'm not sure if that will effect the ItemStack on your cursor aswell, but it does sount to me like a display issue (which is quite common when it comes to custom crafting)
     
  4. If you just want to change the amount of TNT that is crafted the very easy way (but not so plugin friendly, it will mess with other plugins that craft TNT) you can use PrepareCraftItemEvent instead and check if event.getInventory().getResult().getType() is TNT then just event.getInventory().getResult().setAmount().

    The proper way would be to find the recipe by using Bukkit.recipeIterator() and looping through them until you find the exact recipe you need, by checking ingredients and result, then remove it and add your own replacement then break the loop.
     
Thread Status:
Not open for further replies.

Share This Page