Trying to update AutoReplace to keep near broken items

Discussion in 'Plugin Development' started by SavageCore, Mar 13, 2012.

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

    SavageCore

    Hello

    I am a java noob, these are my first lines ever. Have knowledge of PHP but no OO.

    Anyway this is my code thus far:

    Code:
    public class ToolReplaceListener implements Listener {
        @EventHandler
        public void DetectDurability(BlockBreakEvent event){
            Player player = event.getPlayer();
            PlayerInventory inv = player.getInventory();
            Material tool = Material.getMaterial(inv.getItemInHand().getTypeId());
            ItemStack itemInHand = player.getItemInHand();
            //if item is nearly destroyed
            if(!tool.isBlock() && inv.getItemInHand().getDurability() == tool.getMaxDurability() - 1) {
                event.getPlayer().sendMessage(ChatColor.RED+"Almost destroyed.");
                //save item and the durability
                int durability = itemInHand.getDurability();
                int slot = inv.getHeldItemSlot();
                int ID = inv.getItemInHand().getTypeId();
                ItemStack[] items = inv.getContents();
                for (int i = 0; i < items.length; i++) {
                    if (i != slot) {
                        ItemStack obj = items[i];
                        //for each item in inventory, check if it's what we want, then we can break
                        if ((obj == null) || (obj.getTypeId() != ID) || (obj.getAmount() <= 0))
                            continue;
                        //set current item in hand to replacement (in effect replacing with new full durability)
                        inv.getItemInHand().setDurability((short)(obj.getDurability() - 1));
                        inv.addItem(obj.setDurability((short) durability));
                        inv.clear(i);
                        break;
                    }
                }
            }
        }
    }
    This is the line with the problem

    Code:
    inv.addItem(obj.setDurability((short) durability));
    I could not find any sort of move item within inventory so thoughts were set current near broken item to full durability, delete the full one from inventory (replacement) then add a new one with current ones damage value (current in hand item that's near broken). If that makes sense?
     
Thread Status:
Not open for further replies.

Share This Page