Where am I going wrong? PHP: public function onDeath(EntityDeathEvent $ev){ $entity = $ev->getEntity(); if($entity instanceof Villager){ $drop = [Item::get(49, 0, 2)]; $ev->setDrops($drop); }}
Code: public function onDeath(EntityDeathEvent $ev){ $entity = $ev->getEntity(); if($entity instanceof Villager){ $drop = $this->Item::get(49, 0, 2); $ev->setDrops($drop); } } posiblemente
The brackets around your Item::get isn't necessary. However, I think this might help: PHP: public function onDeath(EntityDeathEvent $ev){ $entity = $ev->getEntity(); if($entity instanceof Villager){ $drop = Item::get(49, 0, 2); $finalDrops = array_push($ev->getDrops(), $drop); $ev->setDrops($finalDrops); }}
Use vector3 and level to drop items, It will be easy,,,, Example: PHP: $x = $event->getPlayer()->getX();$y = $event->getPlayer()->getY();$z = $event->getPlayer()->getZ();// Configuring x,y,Z$level = $event->getPlayer()->getLevel();//Now check if the entity who died is villagerIf($player instanceof Villager){ $level->dropItem(new Vector3($x,$y,$z), Item::get($id,$meta,$amount);}}// drops the item on villager death, you can set numbers at $id,$meta or $amount also, use a config.yml if you want :) Don't forget to write use pocketmine\item\Item; use pocketmine\entity\Villager; use pocketmine\level\Level; use pocketmine\math\Vector3;
Read the original post. He wants to set the drops when a Villager dies. When a Villager dies, EntityDeathEvent is called, not PlayerDeathEvent. So you should have used $event->getEntity() in your code instead of $event->getPlayer(), as @DanielYTK pointed out.
Is this solved? Make sure your imports are correct PHP: use pocketmine\entity\Villager; This is a bad code. First, do you really need to save those coordinates into variables? You also do it in any case scenario, even if the entity isn't a villager. Good practice: PHP: if($entity instanceof Villager === false) return;# Code... Create variables holding method returns only when you use them more than once. I see this very often PHP: $x = $player->getX();$y = $player->getY();$z = $player->getZ();$level = $player->getLevel();$level->dropItem(new Vector3($x, $y, $z), Item::get(Item::GLASS)); And it's painful to eyes. Above you see the bad practice and below the way it should be done in this case PHP: $player->getLevel()->dropItem($player, Item::get(Item::GLASS)); *claps* *claps*
If you want to use it that way, Ahh k, use this then PHP: $player = $event->getEntity();if($player instanceof Villager){ $event->setDrops(array($item, $item2));}