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

I can't understand setblock.

Discussion in 'Development' started by Myweet, Aug 8, 2019.

  1. Myweet

    Myweet Creeper

    Messages:
    2
    Why can't I use setblock without setCancelled ?

    Source code :
    PHP:
    <?php

    namespace MinePlugin;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\event\block\BlockBreakEvent;                                
                
     class 
    MinePlugin extends PluginBase implements Listener {
      
        public function 
    onEnable(){
            
    $this->getLogger()->info("MinePlugin has been loaded.");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
          
        public function 
    onBreak(BlockBreakEvent $e)
        {
            
    $block $e->getBlock();
            
    $e->setCancelled(true);
            
    $block->getLevel()->setBlock($blockBlock::get(Block::STONE));

            }
          
        }
    ?>
    Please Tell me how to use setblock without setCancelled.
     
    Last edited: Aug 8, 2019
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Please use code and php tags in order to make code more readable, you can do this by pressing the "+" button of the post field and then pressing "Code".
    You can also write it like this:

    [php]php code here[/php]
    or
    [code]config files here [/code]
     
  3. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    As for your actual problem: No clue, sorry
     
  4. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    The reason for you issue is because the block gets updated after your plugin handles the event. When the event is called, all registered handlers can handle the event. If the event is not cancelled after all the hadnlers have handled it, then it will set the block to air, so if you set that block to something else, it will immediately be replaced with air if the event is not cancelled
     
    HimbeersaftLP likes this.
  5. Myweet

    Myweet Creeper

    Messages:
    2
    Okay, I understand. Then how can I use setblock without setcanclled? Some people recommend using "task" to me so I searched how to use task, but i couldn't understand.

    This is the code.

    Code:
    public function onEnable(){
            $this->getScheduler()->scheduleDelayedTask(new class extends \pocketmine\scheduler\Task{
                public function onRun(int $currentTick) : void{
                }
            }, 20); //delay
    
    I could understand some of it, though i don't know how to use setblock at the code.
     
    Last edited: Aug 8, 2019
  6. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    yes you can use a delayed task to set your block, but why?
    why don't you want to cancel the event when it's the needed action to do?
    do you have a specific reason?
    http://xyproblem.info
     
    HimbeersaftLP 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.