1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Item::setDamage() doesn't fully work

Discussion in 'Development' started by matcracker, Aug 7, 2017.

  1. matcracker

    matcracker Spider

    Messages:
    13
    GitHub:
    matcracker
    Before you give me an answer read the preamble. Thank you!

    Preamble:
    • I'm sure that the player is correctly handling the bow in the same inventory slot.
    • I already debugged it in various ways.
    That said, now I can talk about the problem!
    Initially, when a player spears an arrow, the bow's durability is set to 340 using the EntityShootBowEvent.
    After that, there is already in execution a repeating task that repairs the bow but here appears the problem. The durability's value doesn't change while the task tries to fix the bow.

    How can I fix the problem? Thank you!

    PHP:
    public function onEntityShootBow(EntityShootBowEvent $event)
        {
            
    $entity $event->getEntity();

            if (
    $entity instanceof Player) {
                
    $arena $this->main->getArenaByLevel($entity->getLevel());
                if (
    $arena !== null) {
                    if (
    $arena->isInArena($entity)) {
                        
    $bow $event->getBow();
                        
    $bow->setDamage(340);
                        
    $entity->getInventory()->sendSlot($entity->getInventory()->getHeldItemSlot(), $entity);
                    }
                }

            }
        }
    PHP:
        public function onRun(int $currentTick)
        {
            foreach (
    $this->arena->getPlayers() as $player) {
                  
    $bow $player->getInventory()->getItem(1);
                    if (
    $bow->getId() === Item::BOW) {
                        if (
    $bow->getDamage() > 0) {
                            if ((
    $bow->getDamage() - 70) < 0)
                                
    $bow->setDamage(0);
                            else
                                
    $bow->setDamage(($bow->getDamage() - 70)); //TODO: I need to understand why the damage value isn't set..
                            
    $player->getInventory()->sendSlot(1$player);
                        }
                    }
                }
            }
        }
    }
    PluginTask started
    Bow damage = 0
    EntityShootBowEvent called -> Bow damage = 340
    /*Task cycle*/
    Bow damage > 0 ? Yes! -> Bow damage = 340 - 70 (270)
    /*Another task cycle*/
    What should it do:
    Bow damage > 0 ? Yes! -> Bow damage = 270 - 70 (200)
    What it does:
    Bow damage > 0 ? Yes! -> Bow damage = 340 - 70 (270) //And it continues forever without changing damage value...
     
  2. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    When you use getItem() you are operating on a clone of the item which is in the inventory, not the actual item itself. You need to set the modified version back into the inventory using setItem() for your changes to appear.
     
    Jack Noordhuis, SOFe and matcracker like this.
  3. matcracker

    matcracker Spider

    Messages:
    13
    GitHub:
    matcracker
    I didn't expect that... Thank you so much!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.