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

Solved Player Head

Discussion in 'Development' started by Amon28, May 23, 2017.

  1. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    I'm making a plug in when a player killed another player the player who killed him gets the player head with the name of the player
    public function onDeath(PlayerDeathEvent $event){
    $player = $this->getDamager()->getName();
    $name = $event->getName();
    $inv = $player->getInventory();
    $head = item::get(399,0,1);
    $head->setCustomName("$name Head");
    $inv->addItem($head)
    }
    I get this error "Call to undefined method HeadonDeath\Main::getDamager() on HeadonDeath\Main"
     
  2. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    USE CODE TAGS!!!
     
  3. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    use code tags, too...
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Nonono, why $this? Unless your class has the function getDamager(), it will give you an error. Use $event->getDamager(), not $this->getDamager()
     
  5. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    I got this error I change $this to $event "Call to undefined method pocketmine\event\PlayerDeathEvent::getDamager () on HeadonDeath\Main"
    PHP:
    public function onDeath(PlayerDeathEvent $event){
            
    $player $event->getDamager()->getName();
            
    $name $event->getName();
            
    $inv $player->getInventory();
            
    $head item::get(399,0,1);
            
    $head->setCustomName("$name Head");
            
    $inv->addItem($head);
     
  6. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    PHP:
    public function onDeath(PlayerDeathEvent $ev){

    $player $ev->getPlayer(); ## The player thats killed ##
    $playername $player->getName(); ## Death player name ##
    $head Item::get(399,0,1);
    $head->setCustomName("$playername Head");

    ## Dont think you can add items to death players inventory.. But you can set the drops after the player has been killed ##

    ## Assuming player drop their inventory items too ##

    $drops $ev->getDrops(); ## returns a array of items in player inventory ##

    ## Add the head to the drops ##
    array_push($drops$head);

    ## Set the items dropped ##
    $ev->setDrops($drops);
    }

     
    Last edited: May 23, 2017
    jasonwynn10, Zuruki and Sandertv like this.
  7. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    you need
    PHP:
    $head->setCustomName($playername." Head");
    instead of
    PHP:
    $head->setCustomName("$playername Head");
     
    corytortoise likes this.
  8. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    Both works lol
     
    [deleted] and Sandertv like this.
  9. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    No, because in your version $playerName is in the string, so the item will be named "$playername Head" and not "<name> Head": D
     
    corytortoise likes this.
  10. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    Well why dont you copy my code and try it yourself lol
     
  11. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Not drops the item head goes directly in the killers inventory
     
  12. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    PHP:
    public function onDeath(PlayerDeathEvent $ev){
    $player $ev->getPlayer();
    $cause $player->getLastDamageCause();
    $head =  ## The item ##

    if($cause instanceof EntityDamageByEntityEvent){
      
    $damager $cause->getDamager();
      if(
    $damager instanceof Player){
         
    $damager->getInventory()->addItem($head);
       }
      }
    }
     
  13. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    How do I get the killers name is it?
    PHP:
     $name $cause->getName()
     
  14. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    With my code
    $player is the player that is killed...
    $damager is the player who killed the $player..

    So use $damager->getName()..
     
  15. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Ok thanks
     
  16. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Ok now it says call to a member function getLastDamage() on null
    PHP:
    public function onDeath(PlayerDeathEvent $ev){
         
    $cause $player->getLastDamageCause();
         
    $head item::get(399,0,1);

         if(
    $cause instanceof EntityDamageByEntityEvent){
         
    $damager $cause->getDamager();
         
    $name $damager->getName();
         
    $head->setCustomName("$name Head");
         
    $damager->getInventory()->addItem($head);
      }
    }
     
  17. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    You didnt define $player...
    $player = $ev->getPlayer();

    The error says it all.. maybe you need to learn the API
     
  18. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Please learn some PHP. Double quoted strings allow variables to be added in the string directly. Your way is just a matter of preference in this case. If the string was SINGLE quoted, THEN it would be mandatory to use your method. Thing is, it isn't single quoted.
     
  19. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    This time it has no errors but whenever I kill someone it doesn't give me the head I thought the skulls are just bug so I changed it to Stone but I can't see the item
    PHP:
    public function onDeath(PlayerDeathEvent $ev){
         
    $player $ev->getPlayer();
         
    $cause $player->getLastDamageCause();

         if(
    $cause instanceof EntityDamageByEntityEvent){
         
    $damager $cause->getDamager();
         
    $inv $damager->getInventory();
         
    $name $damager->getName();
         
    $head item::get(1,1,1);
         
    $head->setCustomName("$name's Head");
         
    $inv->addItem($head);
      }
    }
     
  20. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I can't confirm this being the cause of the issue, but try Item::get instead of item::get.
     
  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.