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

Solved EntityExplodeEvent issue

Discussion in 'Development' started by 8675309, Oct 22, 2019.

  1. 8675309

    8675309 Spider

    Messages:
    11
    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();
        }
     
  2. 8675309

    8675309 Spider

    Messages:
    11
    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:
    <?php

    namespace 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 $itemPlayer $player=nullint $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=80Player $player=null) {
                    
    $this->getLevel()->setBlock($thisBlockFactory::get(Block::AIR), true);
                    
    $mot = (new Random())->nextSignedFloat() * M_PI 2;
                    
    $nbt Entity::createBaseNBT($this->add(0.500.5), new Vector3(-sin($mot) * 0.020.2, -cos($mot) * 0.02));
                    
    $nbt->setShort("Fuse"$fuse);
                    
    $tnt Entity::createEntity("PrimedTNT"$this->getLevel(), $nbt);
                    
    $tnt->setOwningEntity($player);
     
                    if(
    $tnt !== null) {
                            
    $tnt->spawnToAll();
                    }
        }
    }
     
  3. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    If it works ¯\_(ツ)_/¯
     
  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.