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"
Nonono, why $this? Unless your class has the function getDamager(), it will give you an error. Use $event->getDamager(), not $this->getDamager()
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);
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);}
you need PHP: $head->setCustomName($playername." Head"); instead of PHP: $head->setCustomName("$playername Head");
No, because in your version $playerName is in the string, so the item will be named "$playername Head" and not "<name> Head": D
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); } }}
With my code $player is the player that is killed... $damager is the player who killed the $player.. So use $damager->getName()..
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); }}
You didnt define $player... $player = $ev->getPlayer(); The error says it all.. maybe you need to learn the API
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.
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); }}