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

Block detection by explosion

Discussion in 'Development' started by KingDeadKnight, Dec 5, 2016.

  1. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    Hello,
    I would like to know how can I detect a certain block by explosion of tnt, and I hesitate between EntityExplodeEvent or ExplosionPrimeEvent or maybe use the Entity PrimedTnt (but not very sure of this x) )

    Can somebody help me to do this?
    Thanks
     
    Bluzzi likes this.
  2. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Use EntityExplodeEvent, there you can retrieve the blocks that are affected by the explosion with..
    PHP:
    $e->getBlockList();
    This returns as an array of the blocks affected.
     
    HimbeersaftLP and Bluzzi like this.
  3. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    Okey thanks

    So if i do this
    PHP:
    $blocks $event->getBlockList();
    if(
    in_array(Block:OBSIDIAN$blocks)){
       .....
    }
    it's ok?
    But i want to know too how to get of the target block to set it with air maybe
     
    Bluzzi likes this.
  4. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    PHP:
    public function onExlpode(EntityExplodeEvent $e) {
            
    $blocks $e->getBlockList();
            if(
    $key array_search(Block::get(Block::OBSIDIAN), $blocks)) {
                
    $obsidian $blocks[$key];
                
    $x $obsidian->getX();
                
    $y $obsidian->getY();
                
    $z $obsidian->getZ();
                
    $obsidian->getLevel()->setBlock(new Vector3($x$y$z), Block::get(Block::AIR));
            }
        }
    Like this
     
    Bluzzi likes this.
  5. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    I just try on a big carpet of obisidian and your code doesn't work.. :/
    And I don't get error in logs

    I'll try by myself but if you have another idea, say it :)
    Thanks
     
    Last edited: Dec 6, 2016
    Bluzzi likes this.
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    PHP:
    public function onExlpode(EntityExplodeEvent $e) {
            
    $blocks $e->getBlockList();
            foreach(
    $blocks as $block){
                   if(
    $block instanceof Obsidian//Remeber to use pocketmine\block\Obsidian;
                       
    $obsidian $block;
                       
    $obsidian->getLevel()->setBlock($obsidianBlock::get(Block::AIR)); //Obsidian is an instance of  Vector3 
                   
    }
           }
    }
    try that :)
    and @LilCrispy2o9 array_search(Block::get(Block::OBSIDIAN)) it will never find Block::get(Block::OBSIDIAN) because the actual obsidian always has data values
     
  7. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    Hi, i just try your code (I don't forget the note with the use) with a tnt explosion and it doesn't not work :/
    I'll keep on looking for that
    Thanks
     
    Last edited: Dec 6, 2016
  8. archie426

    archie426 Baby Zombie

    Messages:
    130
    GitHub:
    archie426
    *Remember
     
  9. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Please show us how you integrated the code. I'll also try to find time to test it by myself...
     
  10. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    PHP:
    namespace Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityExplodeEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\entity\Entity;

    class 
    Main extends PluginBase implements Listener{
       public function 
    onEnable(){
          
    $this->getServer()->getPluginManager()->registerEvents($this,$this); 
           
    $this->getLogger()->info("Plugin Enabled"); 
       ..........
       public function 
    onExplode(EntityExplodeEvent $e) {
            
    $blocks $e->getBlockList();
            foreach(
    $blocks as $block){
                if(
    $block instanceof Obsidian){ //Remeber to use pocketmine\block\Obsidian;
                    
    $obsidian $block;
                    
    $this->getLevel()->setBlock($obsidianBlock::get(Block::AIR); //Obsidian is an instance of  Vector3
                
    }
            }
        }
      ...........
    }
    It's how integrate your code
     
    Last edited: Dec 7, 2016
  11. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    You have to register event on enable.

    PHP:
    public function onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this,$this); //register event
        
    $this->getLogger()->info("Plugin Enabled"); // use this to check if plugin enabled correctly
    }
     
  12. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    It's what I did but I forgot to mention it, others events works but not this event
    Thanks
     
  13. imYannic

    imYannic Baby Zombie

    Messages:
    113
    var_dump() the blocklist to make sure that the block is in it:
    PHP:
    foreach($e->getBlockList() as $bvar_dump(get_class($b));
     
    Primus likes this.
  14. KingDeadKnight

    KingDeadKnight Spider

    Messages:
    8
    GitHub:
    kingdeadknight
    PHP:
    public function onExplode(EntityExplodeEvent $e) {
             
    $b var_dump($e->getBlockList());
                if(
    $b instanceof Obsidian){ //Remeber to use pocketmine\block\Obsidian;
                    
    $obsidian $block;
                    
    $this->getLevel()->setBlock($obsidianBlock::get(Block::AIR); //Obsidian is an instance of  Vector3
                
    }
            }
        }
    This will be correct?
     
  15. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    ???
    that code is not really making sence
     
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    http://php.net/var-dump
    Please look at what you are doing before you write copy any code.
     
    LilCrispy2o9 and Primus like this.
  17. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    Are you trying to replace the block that shouldnt be destroyed when tnt exploded?
    PHP:
    namespace Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityExplodeEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\block\Obsidian;

    class 
    Main extends PluginBase implements Listener{
       public function 
    onEnable(){
          
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
           
    $this->getLogger()->info("Plugin Enabled");
     }
       ..........
       public function 
    onExplode(EntityExplodeEvent $e) {
            
    $blocks $e->getBlockList();
            foreach(
    $blocks as $block){
                if(
    $block instanceof Obsidian) { //Remember to use pocketmine\block\Obsidian;
                    
    $obsidian $block;
                    
    $obsidian->getLevel()->setBlock($obsidianBlock::get(Block::AIR)); //Obsidian is an instance of  Vector3
                
    }
            }
        }
      ...........
    }
    Try this. And provide us with error next time if there are any.
     
  18. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I think the problem is that the block will eventually be replaced by PocketMine after all event handlers have been called, if it is still in the block list.
     
  19. KelvinCyaX

    KelvinCyaX Silverfish

    Messages:
    18
    GitHub:
    CyanKelv
    What about cancelling the event and replace blocks with air?
     
  20. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Why don't you just edit the block list in the event? There is a setBlockList() function.
     
  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.