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
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.
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
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
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
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($obsidian, Block::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
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
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($obsidian, Block::get(Block::AIR); //Obsidian is an instance of Vector3 } } } ...........} It's how integrate your code
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}
var_dump() the blocklist to make sure that the block is in it: PHP: foreach($e->getBlockList() as $b) var_dump(get_class($b));
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($obsidian, Block::get(Block::AIR); //Obsidian is an instance of Vector3 } } } This will be correct?
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($obsidian, Block::get(Block::AIR)); //Obsidian is an instance of Vector3 } } } ...........} Try this. And provide us with error next time if there are any.
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.