Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Cancel block drop on ExplosionPrimeEvent?

Comments in 'Plugin Development' started by MyNameIsTriXz, Jan 16, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    How?
    MrAnnonym likes this.
  2. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    I don't think you can do that. You can however set the block list in EntityExplodeEvent.
    Edit: Maybe you can cancel the ExplosionPrimeEvent, make a new Explosion class that extends pocketmine\level\Explosion, which also has a new explodeB() method which does what you want without dropping blocks.
    Example:
    PHP:
    ExplosionWithoutBlockdrops.php

    class ExplosionWithoutBlockdrops extends Explosion{
       public function 
    explodeB(){
          
    $send = [];
          
    $updateBlocks = [];
          
    $source = (new Vector3($this->source->x$this->source->y$this->source->z))->floor();
          
    $yield = ($this->size) * 100;
          if(
    $this->what instanceof Entity){
             
    $this->level->getServer()->getPluginManager()->callEvent($ev = new EntityExplodeEvent($this->what$this->source$this->affectedBlocks$yield));
             if(
    $ev->isCancelled()){
                return 
    false;
             }else{
                
    $yield $ev->getYield();
                
    $this->affectedBlocks $ev->getBlockList();
             }
          }
          
    $explosionSize $this->size 2;
          
    $minX Math::floorFloat($this->source->$explosionSize 1);
          
    $maxX Math::ceilFloat($this->source->$explosionSize 1);
          
    $minY Math::floorFloat($this->source->$explosionSize 1);
          
    $maxY Math::ceilFloat($this->source->$explosionSize 1);
          
    $minZ Math::floorFloat($this->source->$explosionSize 1);
          
    $maxZ Math::ceilFloat($this->source->$explosionSize 1);
          
    $explosionBB = new AxisAlignedBB($minX$minY$minZ$maxX$maxY$maxZ);
          
    $list $this->level->getNearbyEntities($explosionBB$this->what instanceof Entity $this->what null);
          foreach(
    $list as $entity) {
             
    $distance $entity->distance($this->source) / $explosionSize;
             if (
    $distance <= 1) {
                
    $motion $entity->subtract($this->source)->normalize();
                
    $impact = ($distance) * ($exposure 1);
                
    $damage = (int)((($impact $impact $impact) / 2) * $explosionSize 1);
                if (
    $this->what instanceof Entity){
                   
    $ev = new EntityDamageByEntityEvent($this->what$entityEntityDamageEvent::CAUSE_ENTITY_EXPLOSION$damage);
                }elseif(
    $this->what instanceof Block){
                   
    $ev = new EntityDamageByBlockEvent($this->what$entityEntityDamageEvent::CAUSE_BLOCK_EXPLOSION$damage);
                }else{
                   
    $ev = new EntityDamageEvent($entityEntityDamageEvent::CAUSE_BLOCK_EXPLOSION$damage);
                }
                   
    $entity->attack($ev->getFinalDamage() , $ev);
                   
    $entity->setMotion($motion->multiply($impact));
                }
             }
             
    $air Item::get(Item::AIR);
             foreach(
    $this->affectedBlocks as $block) {
                if (
    $block->getId() === Block::TNT) {
                   
    $mot = (new Random())->nextSignedFloat() * M_PI 2;
                   
    $tnt Entity::createEntity("PrimedTNT"$this->level->getChunk($block->>> 4$block->>> 4) , new Compound("", ["Pos" => new Enum("Pos", [new Double(""$block->0.5) , new Double(""$block->y) , new Double(""$block->0.5) ]) , "Motion" => new Enum("Motion", [new Double("", -sin($mot) * 0.02) , new Double(""0.2) , new Double("", -cos($mot) * 0.02) ]) , "Rotation" => new Enum("Rotation", [new Float(""0) , new Float(""0) ]) , "Fuse" => new Byte("Fuse"mt_rand(1030)) ]));
    $tnt->spawnToAll();
             }elseif (
    mt_rand(0100) < $yield) {
                
    /*foreach($block->getDrops($air) as $drop) {
                   $this->level->dropItem($block->add(0.5, 0.5, 0.5) , Item::get( . . . $drop));
                }*/
                //this is where we stop the blocks dropping
             
    }
             
    $this->level->setBlockIdAt($block->x$block->y$block->z0);
             
    $pos = new Vector3($block->x$block->y$block->z);
             for (
    $side 0$side 5$side++) {
                
    $sideBlock $pos->getSide($side);
                if (!isset(
    $this->affectedBlocks[$index Level::blockHash($sideBlock->x$sideBlock->y$sideBlock->z) ]) and !isset($updateBlocks[$index])) {
                   
    $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockUpdateEvent($this->level->getBlock($sideBlock)));
                   if (!
    $ev->isCancelled()) {
                      
    $ev->getBlock()->onUpdate(Level::BLOCK_UPDATE_NORMAL);
                   }
                   
    $updateBlocks[$index] = true;
                }
             }
             
    $send[] = new Vector3($block->$source->x$block->$source->y$block->$source->z);
          }
          
    $pk = new ExplodePacket();
          
    $pk->$this->source->x;
          
    $pk->$this->source->y;
          
    $pk->$this->source->z;
          
    $pk->radius $this->size;
          
    $pk->records $send;
          
    $this->level->addChunkPacket($source->>> 4$source->>> 4$pk);
          return 
    true;
       }
    }

    EventListener.php
    class EventListener implements Listener{
       public function 
    ExplosionPrimeEvent(ExplosionPrimeEvent $event){
          
    $event->setCancelled();
          
    $explosion = new ExplosionWithoutBlockDrops($event->getEntity(), $event->getForce(), $event->getEntity());
          
    $explosion->explodeA(); // not sure what this does
          
    $explosion->explodeB();
       }
    }
    Last edited: Jan 16, 2016
    MyNameIsTriXz and AndrewBit like this.
  3. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    You can also foreach the array you got from $event->getBlockList() and set those blocks to air and cancel EntityExplodeEvent (No damage to players) or $event->setBlockList([]); (Damages nearby players)
    MyNameIsTriXz, PEMapModder and Lambo like this.
  4. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    That is indeed a much better idea.
  5. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Ugh... That isn't example... I can make a plugin with that xD jk
  6. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    PHP:
          public function ExplosionPrimeEvent(ExplosionPrimeEvent $e){
              
    $e->setBlockBreaking(false);
          }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Once again, please be reminded to follow PHP code conventions to starat class method names with lowercase so that others can read more easily.
  8. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Thanks, that's a good idea!
  9. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    This always makes me wonder if PEMapModder have returned...
    Primus likes this.

Share This Page

Advertisement