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

EntityDamageEvent setItemInHand not working

Discussion in 'Development' started by BruhLol, Jun 18, 2019.

  1. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    PHP:
    public function onKill(EntityDamageEvent $event){
      
    $player $event->getEntity();
      if(
    $event instance EntityDamageByEntityEvent){
        
    $damager $event->getDamager();
        if(
    $event->getFinalDamage() >= $player->getHealth()){
          
    $kitem $damager->getItemInHand();
          
    $kitem->setLore(["test""test1"]);
          
    $damager->getInventory()->setItemInHand($kitem);
        }
      }
    }
    Why wont this work? addItem() works..
     
    Last edited: Jun 19, 2019
  2. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    sendcontents
     
  3. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    didnt work
     
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    If you want to record player kills you should use PlayerDeathEvent
     
  5. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    i don't see player defined, and you have a useless variable $lore
     
  6. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    PHP:
    public function onDeath(PlayerDeathEvent $event)
    {
        
    $p $event->getEntity();
        
    $cause $p->getLastDamageCause();
        if (
    $cause instanceof EntityDamageByEntityEvent) {
          
    $killer $cause->getDamager();
          if (
    $killer instanceof Player) {
            
    $kitem $killer->getItemInHand();
            
    $kitem->setLore(["test""test1"]);
            
    $killer->getInventory()->setItemInHand($kitem);
            
    $killer->getInventory()->sendContents($killer);
          }
       }
    }
    This didn't work either :(
     
  7. Fritures

    Fritures Spider Jockey

    Messages:
    47
    $killer->getInventory()->sendContents();
    test this
     
  8. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    that wont work
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It won't work. This is roughly how pocketmine handles updating item in hand currently...
    Code:
    $item = item in hand;
    callEvent -> EntityDamageEvent
    if(event is cancelled){
        return;
    }
    
    Item->applyDamage(1);
    Player->getInventory()->setItemInHand(item);
    
    Any changes to item in hand will be overridden after the event has been handled by all event listeners. You'll need to somehow run the code after the event is called and handled, which surely can't be done during the event. So updating the item after a one tick delay seems to be the appropriate solution.

    Oh, and also... the player's item in hand may not be the same by the next tick, so you'll need to check if it's the same item. I suggest completely shifting the event handling into the task if you're merely monitoring the task.
    The player could also disconnect by the next tick, you can use an if(!$player->isConnected()) return;
     
    HimbeersaftLP and BruhLol like this.
  10. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    oh okay, ty <3
     
  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.