I am trying to alter the drops of Primed TNT Entity. The problem I seem to be having is that I can't attach a player to the event. I need to know the player who activated the TNT block so I can alter the drops based on the players ability. Any idea on how I can attach a Player to the EntityExplodeEvent? PHP: public function onPlayerInteract(PlayerInteractEvent $event) : void { $item = $event->getItem(); $player = $event->getPlayer(); if($player->isSneaking() && $this->config->isFlintSteel($item) && $this->config->last_tnt_drop !== null) { $pos = $this->config->last_tnt_drop; $level = $player->getLevel(); $block = $level->getBlockAt($pos->x, $pos->y, $pos->z); if($this->config->isTNT($block)) { $skill = $this->plugin->getSkillManager($player)->getSkill(self::BLAST_MINING); if($skill->hasAbility()) { $block->ignite(0)); // Tell this now "PRIMED TNT ENTITY" which player activated it } } } } // Need to be able to know the player in this event that activated the tnt block public function onExplosion(EntityExplodeEvent $event) : void { var_dump($event->getEntity()); $drops = $event->getBlockList(); }
My solution was to extend the TNT class and override the onActivate()/ignite() functions where I pass in the Player and setOwningEntity($player); Right or Wrong??? PHP: <?phpnamespace muqsit\mcmmo\block;use pocketmine\Player;use pocketmine\entity\Entity;use pocketmine\block\Block;use pocketmine\block\BlockFactory;use pocketmine\item\Item;use pocketmine\item\FlintSteel;use pocketmine\item\enchantment\Enchantment;use pocketmine\utils\Random;use pocketmine\math\Vector3;class TNT extends \pocketmine\block\TNT { public function onActivate(Item $item, Player $player=null, int $fuse=80) : bool { if($item instanceof FlintSteel or $item->hasEnchantment(Enchantment::FIRE_ASPECT)) { if($item instanceof Durable) { $item->applyDamage(1); } $this->ignite($fuse, $player); return true; } return false; } public function ignite(int $fuse=80, Player $player=null) { $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); $mot = (new Random())->nextSignedFloat() * M_PI * 2; $nbt = Entity::createBaseNBT($this->add(0.5, 0, 0.5), new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02)); $nbt->setShort("Fuse", $fuse); $tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), $nbt); $tnt->setOwningEntity($player); if($tnt !== null) { $tnt->spawnToAll(); } }}