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

Make unbreakable blocks breakable

Discussion in 'Development' started by kriskotooBG, May 17, 2020.

  1. kriskotooBG

    kriskotooBG Spider Jockey

    Messages:
    46
    GitHub:
    kriskotoobg
    Heyo! Recently i wanted to add some cool custom blocks, by using command blocks.... but after making them... i realized that onBreak does not get called... I tried setting isBreakable to true, (even though its true by default in Block), tried messing with the hardness... but nothing... all i get is the breaking animation, and then just nothing... the block just stays there.....

    Is it even possible? I would like to do this without any external listeners or tasks.... but if its needed i might ue them..

    Thanks in advance!
     
  2. mlsdmitry0

    mlsdmitry0 Spider

    Messages:
    12
    Demo video
    I have a youtube channel, where I answer the threads from pmmp.io
    Follow spoilers, all files are in the same namespace / directory, so no need additional folders. This code just demonstration, and have issues. Good luck!

    I took some information "how to" from this thread.

    PHP:
    $this->getServer()->getPluginManager()->registerEvents(new BedrockBreakEvent(), $this);
    BlockFactory::registerBlock(new Bedrock(), true);
    PHP:
    <?php


    namespace mlsdmitry\ForumThreads;


    use 
    pocketmine\block\Block;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\server\DataPacketReceiveEvent;
    use 
    pocketmine\network\mcpe\protocol\PlayerActionPacket;

    class 
    BedrockBreakEvent implements Listener
    {

        public function 
    onDataReceive(DataPacketReceiveEvent $event) {
            
    $pk $event->getPacket();
            
    $p $event->getPlayer();
            if (
    $pk instanceof PlayerActionPacket) {
                if (
    $pk->action === PlayerActionPacket::ACTION_START_BREAK) {
                    
    $block $p->getLevel()->getBlockAt($pk->x$pk->y$pk->z);
                    if (
    $block->getId() === Block::BEDROCK) {
                        
    ForumThreads::make()->getScheduler()->scheduleRepeatingTask(
                            new 
    BedrockBreakTask($p->getInventory()->getItemInHand(), $block), 1);
                    }
                }
            }

        }
    }

    PHP:
    <?php


    namespace mlsdmitry\ForumThreads;


    use 
    pocketmine\block\BlockToolType;
    use 
    pocketmine\item\Item;

    class 
    Bedrock extends \pocketmine\block\Bedrock
    {
        public function 
    isBreakable(Item $item): bool {
            return 
    true;
        }

        public function 
    getHardness(): float {
            return 
    0.3;
        }

        public function 
    getToolType() : int{
            return 
    BlockToolType::TYPE_PICKAXE;
        }

        public function 
    getBlastResistance(): float {
            return 
    30;
        }

        public function 
    getBreakTime(Item $item): float {
            return 
    parent::getBreakTime($item); // TODO: Change the autogenerated stub
        
    }
    }
    PHP:
    <?php


    namespace mlsdmitry\ForumThreads;


    use 
    pocketmine\block\Block;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\item\Tool;
    use 
    pocketmine\math\Vector3;

    class 
    BedrockBreakTask extends \pocketmine\scheduler\Task
    {
        
    /** @var Block $block */
        
    private $block;
        
    /** @var Tool $tool */
        
    private $tool;
        private 
    $ticks 0;

        public function 
    __construct(Item $toolBlock $block) {
            
    $this->tool $tool;
            
    $this->block $block;
        }

        
    /**
         * @inheritDoc
         */
        
    public function onRun(int $currentTick) {
            if (
    $this->ticks >= $this->block->getBreakTime($this->tool) * 20) {
                
    $level $this->block->getLevel();
                
    $level->setBlock($this->block->asVector3(), new Block(Block::AIR));
                
    $level->dropItem($this->block->asVector3()->add(00.30), new Item(Item::BEDROCK), new Vector3(000));
                
    $this->getHandler()->cancel();
            }

            
    $this->ticks++;
        }
    }
     
    Last edited: May 17, 2020
  3. kriskotooBG

    kriskotooBG Spider Jockey

    Messages:
    46
    GitHub:
    kriskotoobg
    With some modifications, that will work! thanks!
     
    mlsdmitry0 likes this.
  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.