Subtract durability in crafting

Discussion in 'Plugin Development' started by AwesomelyToad, Dec 12, 2018.

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

    AwesomelyToad

    In many mods, there are tools used in crafting that instead of being consumed simply lose one durability point each time they are crafted with. Is this possible with custom recipes in a Bukkit plugin? I am very much a plugin-writing noob but if this is possible, I'd love some info on how to do it!
     
  2. Offline

    torpkev

    Do you mean you want to put say.. a pickaxe in the crafting table as an ingredient and then get it back with some durability lost?
     
  3. Offline

    AwesomelyToad

    Yeah ! Ideally the pickaxe would remain in the crafting square and just lose durability when you click the crafting result (the other ingredients would be consumed) but if that's not possible the pickaxe (or other tool) could just return to the crafters inventory and lose durability.
     
  4. Offline

    torpkev

    Got ya. Well I guess after you create your shapedrecipe you could go ahead and watch the PrepareItemCraftEvent event.

    From there you should be able to loop through the items and use setDamage() on the meta damageable to change the durability of the item. Not sure if it would stay in the crafting table, but getting it to drop or appear in inventory with an updated durability shouldn't be too tough.

    Maybe starting with something like this (very rough and very incomplete):
    Code:
        @EventHandler
        public void onPrepareCraft(PrepareItemCraftEvent  evt) {
            CraftingInventory ci = evt.getInventory();
            if (ci.getMatrix().length > 4) // Make sure its a crafting table and not inventory crafting
            {
                if (ci.getMatrix()[4] != null) // Check the inventory at position 4
                {
                    if (ci.getMatrix()[4] instanceof ItemStack) // Check to make sure it's an itemstack
                    {
                        ItemStack i4 = ci.getMatrix()[4]; // Set the itemstack
                        if (i4 != null) // Check if it's null because java is super needy about null values
                        {
                            switch (i4.getType())
                            {
                                case IRON_PICKAXE:
                                    ItemMeta meta = i4.getItemMeta(); // Get the item meta
                                    if (meta != null) // Check if it's null again
                                    {
                                        Damageable d = (Damageable) meta; // Create the damageable (make sure its org.bukkit.inventory.meta.Damageable and not Entity.Damageable)
                                        int dmg = d.getDamage(); // Get the damage if you ened it
                                        d.setDamage(d.getDamage() - some_value); // Set the damage
                                        i4.setItemMeta(meta); // Set the meta back to the itemstack
                                      
                                        // .. do whatever else you need
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
    }
     
  5. @torpkev
    @AwesomelyToad

    Don't use PrepareItemCraftEvent for this! That is fired when the result appears in the crafting result slot. It doesn't necessarily mean that the player will actually craft the item!
    You should use InventoryClickEvent for this and check if the slot type is RESULT and do a lot of additional checks.
     
    torpkev likes this.
Thread Status:
Not open for further replies.

Share This Page