Repairing Items

Discussion in 'Plugin Development' started by DatCookiez, Nov 10, 2013.

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

    DatCookiez

    Hello all!

    I don't know why this isn't working as I've never worked with item durability or anything like that. There are no errors and I don't know why it's doing it :) Am I heading in the complete opposite direction? I am wanting to repair the players item in their hand if it's damage (or can I just constantly repair all items when damage in inventory and armour contents?)

    Code:
    Code:
        @EventHandler
        public void onPlayerItemBrake(PlayerMoveEvent evt){
            Player player = evt.getPlayer();
            ItemStack item = player.getItemInHand();
            EntityEquipment item1 = player.getEquipment();
            if (player.getInventory().getContents().equals(item) || player.getInventory().getContents().equals(item1)){
                if (item.getDurability() > 0){
                    item.setDurability((short) 0);
                    player.updateInventory();
                    if (item1.getBoots().getDurability() > 0 && (item1.getHelmet().getDurability() > 0 && (item1.getChestplate().getDurability() > 0 || (item1.getLeggings().getDurability() > 0)))){
                        item1.getBoots().setDurability((short) 0);
                        player.updateInventory();
                        item1.getHelmet().setDurability((short) 0);
                        player.updateInventory();
                        item1.getLeggings().setDurability((short) 0);
                        player.updateInventory();
                        item1.getChestplate().setDurability((short) 0);
                        player.updateInventory();
                    }
                }
            }
        }
     
  2. Offline

    amhokies

    realiez
    It probably isn't a good idea to run this code everytime on a PlayerMoveEvent. Literally everytime a player moves or moves their mouse, it will trigger this event, which has the potential to be a be more inefficient than it probably could be. Maybe instead, you can use a PlayerInteractEvent, so the code will run when a player right or left clicks.

    Another thing that doesn't make sense is:
    Code:java
    1. player.getInventory().getContents().equals(item) || player.getInventory().getContents().equals(item1)


    You're trying to compare an array of ItemStacks to a single ItemStack, which will never work. I'm not even really sure what you're trying to do there. To me it seems like you don't need it for what you're trying to do.

    Anyway, everything else seems to look alright. Best of luck! :)
     
  3. Offline

    Alshain01

    To expand on amhokies reply. Use

    Code:java
    1. player.getInventory().contains(item)


    and put all of this in the EntityDamageEvent. EntityDamageEvent is pretty broad so you will need to do some checking to see if the entity being damaged is one that concerns you and return from the event quickly to not waste time.

    Code:java
    1. if (!(event.getEntity() instanceof Item)) {
    2. return;
    3. }
     
Thread Status:
Not open for further replies.

Share This Page