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

Solved set and replace block

Discussion in 'Plugin Help' started by Marie meow, Dec 13, 2020.

  1. Marie meow

    Marie meow Spider

    Messages:
    13
    hi! Is there any way someone can send me a code for when the player put a stone start a task, and in that task dps of 5 seconds the block turns cobblestone and after +5 seconds it disappears? Please I'm new to pocketmine and php, so i study taking examples... ;)
     
  2. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    Welcome to php! ;)

    PHP:
    // Main.php
    <?php

    namespace test;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\Player;

    class 
    test extends PluginBase implements Listener {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    blockplace(BlockPlaceEvent $event) {
            if((
    $event->getBlock()->getId() == 1) && ($event->getBlock()->getDamage() == 0)) {
                
    $this->getScheduler()->scheduleDelayedTask(new MyTask($this$event10), 100);
            }
        }
    }

    // MyTask.php
    <?php
    namespace test;
    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\math\Vector3;

    class 
    TaskMotd extends Task {

        private 
    $plugin;
        private 
    $event;
        private 
    $id;
        private 
    $meta;

        public function 
    __construct(Main $plugin$event$id$meta){
            
    $this->plugin $plugin;
            
    $this->event $event;
            
    $this->id $id;
            
    $this->meta $meta;
        }

        public function 
    onRun($tick){
            
    $event $this->event;
            
    $block $event->getPlayer();
            
    $block $event->getBlock();
            
    $level $block->getLevel();
            
    $pos = [$block->getX(), $block->getY(), $block->getZ()];
            
    $id $this->id;
            
    $meta $this->meta;
            
    $plugin $this->plugin;
            
    $level->setBlock(new Vector3($pos[0], $pos[1], $pos[2]), new Block($id$meta));
            
    $player->sendMessage("Block changed !?");
            if(
    $id == 1) {
                
    $plugin->getScheduler()->scheduleDelayedTask(new self::($this$event00), 100);
            }
        }
      
        public function 
    getPlugin(){
           return 
    $this->plugin;
        }
    }
     
  3. SeferA

    SeferA Spider

    Messages:
    6
    can i reach you on discord?
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    PHP:
    <?php

    declare(strict_types=1);

    namespace 
    me\project;

    use 
    pocketmine\block\Block;
    use 
    pocketmine\block\BlockIds;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\scheduler\ClosureTask;

    /**
     * Class test
     * @package me\project
     */
    class test extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onBlockPlace(BlockPlaceEvent $event) {
            
    $block $event->getBlock();
            if(
    $block->getId() == BlockIds::STONE and $block->getDamage() == 0) { // Without damage check, it would work with for example andesite too
                
    $this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (int $currentTick) use ($block): void {
                    
    $block->getLevel()->setBlock($blockBlock::get(BlockIds::COBBLESTONE));
                }), 
    20); // It should be in ticks, 20 ticks = 1 second
                
    $this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (int $currentTick) use ($block): void {
                    
    $block->getLevel()->setBlock($blockBlock::get(BlockIds::AIR));
                }), 
    10 20);
            }
        }
    }
    Another way to solve your task
     
  5. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    Yep
    Oğuzhan#6561
     
  6. Marie meow

    Marie meow Spider

    Messages:
    13
    thx u all!!
     
  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.