Destroying Obsidian with TNT

Discussion in 'Development' started by BlobKing21, Dec 23, 2019.

  1. BlobKing21

    BlobKing21 Creeper

    Messages:
    1
    I know, I know...another obsidian thread, but this time it's mainly to do with making the obsidian blow up after a couple of hits with TNT instead of just one explosion.
    PHP:
    <?php

    declare(strict_types=1);

    namespace 
    BlobKing21\ObbyDestroyer;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\block\Obsidian;
    use 
    pocketmine\block\BlockFactory;

    class 
    Main extends PluginBase implements Listener
    {
        public function 
    onEnable()
        {
            
    BlockFactory::registerBlock(new MyObsidian(), true);
        }
    }

    class 
    MyObsidian extends \pocketmine\block\Obsidian
    {
        public function 
    getBlastResistance() : float
        
    {
              return 
    7.5;//same as stone
        
    }
    }
    This code works, but as I previously said, I want to make some way of blowing up the obsidian after, say 4 times. If you guys could point me in the right direction of how to do this, that will be greatly appreciated, Thanks!

    Edit: I know that I could just set the hardness to 77, and it be the hardest block that TNT can break, but that isn't the way I want it to break, just so you know!
     
    Last edited: Dec 23, 2019
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,469
    GitHub:
    jasonwynn10
    on block break, increment a counter. if counter > 4 actually break the block
     
    HimbeersaftLP likes this.
  3. Loosty

    Loosty Spider

    Messages:
    12
    GitHub:
    Loosty
    Exemple plis
     
  4. HeyDeniis_

    HeyDeniis_ Witch

    Messages:
    62
    this may not be the best way, but it works
    PHP:
    //MAIN
    public function onExplodeEntity(EntityExplodeEvent $e){
    $list = [];
    foreach(
    $e->getBlockList() as $b){
    if(
    $b instanceof Obsidian){
        
    $b->onExplode();
         }else{
        
    $list[] = $b
         
    }
      }
    $e->setBlockList($list);
    }
    ///Obsidian class
    class Obsidian extends \pocketmine\block\Obsidian{

    const 
    MAX_EXPLODE_COUNT 3//durability

    public function onExplode(){
    if((
    $this->meta 1) >= self::MAX_EXPLODE_COUNT){
    $this->getLevel()->setBlock($this, new Block(0));
    }else{
    $this->meta += 1;
    $this->getLevel()->setBlock($this$this);
       }
      }
    }
     
  5. NickteeChunky

    NickteeChunky Spider Jockey

    Messages:
    42
    GitHub:
    nickteechunky
    Didn't work for me...


    PHP:
    //Main File

    <?php

    namespace NickteeChunky;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\entity\EntityExplodeEvent;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\block\BlockFactory;

    class 
    Main extends PluginBase implements Listener{
       
        public function 
    onEnable()
        {
            
    BlockFactory::registerBlock(new MyObsidian(), true);
        }

        public function 
    onExplodeEntity(EntityExplodeEvent $e){
            
    $list = [];
            foreach(
    $e->getBlockList() as $b){
                if(
    $b instanceof MyObsidian){
                    
    $b->onExplode();
                }else{
                    
    $list[] = $b;
                }
            }
            
    $e->setBlockList($list);
        }
    }

    //Obsidian File

    <?php

    namespace NickteeChunky;

    use 
    pocketmine\block\Obsidian;

    class 
    MyObsidian extends \pocketmine\block\Obsidian{

    const 
    MAX_EXPLODE_COUNT 3//durability

        
    public function onExplode(){
            if((
    $this->meta 1) >= self::MAX_EXPLODE_COUNT){
                
    $this->getLevel()->setBlock($this, new Block(0));
            }else{
                
    $this->meta += 1;
                
    $this->getLevel()->setBlock($this$this);
            }
        }
    }
     
  6. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    430
    GitHub:
    GamakCZ
    Missing getBlastResidence function
     
  7. wolfdale

    wolfdale Zombie

    Messages:
    394
    You didnt register events, plus missing
    PHP:
    public function getBlastResistance() : float
        
    {
              return 
    7.5;//same as stone
        
    }
    in MyObsidian class
     
  8. NickteeChunky

    NickteeChunky Spider Jockey

    Messages:
    42
    GitHub:
    nickteechunky
    Ah okay, got it, but what if I wanted to know a block's current durability after x amount of explosions. Say for example, the block blows up when it's exploded 4 times, I've exploded it twice and I want to know how many more explosions it needs to blow up, how would I get this number via interaction? Thanks in advance. @wolfdale @GamakCZ
     
  9. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    430
    GitHub:
    GamakCZ
    PHP:
    MAX_EXPLODE_COUNT-$block->meta
     
  10. NickteeChunky

    NickteeChunky Spider Jockey

    Messages:
    42
    GitHub:
    nickteechunky
    How would I get each block's meta?
     
  11. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    430
    GitHub:
    GamakCZ
    $block->getDamage()
     
  12. NickteeChunky

    NickteeChunky Spider Jockey

    Messages:
    42
    GitHub:
    nickteechunky
    yeah that was a bit of a dumb question, I understand now. Thank you so much.
     

Share This Page

  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.