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

Remove block

Discussion in 'Development' started by jordi1206, Apr 23, 2017.

  1. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    Hello all,

    I'm making a plugin, and need to know how to remove an block, I've got the XYZ.
    How can I do this?

    Thanks for helping!
     
  2. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    You can replace old block to air block
    PHP:
    $level->setBlock(new Vector3($x$y$z), Block::get(0), truetrue);
     
  3. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Due to MCPE logic, there is no such thing as "removing" a block, only replacing it with air. You can do that with this:
    PHP:
     $level->setBlock(new Vector3($x$y$z), Block::get(Block::AIR)); 
    You'll have to define a Level and the Vector3 parameters.
     
  4. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    Hi!

    Thanks both for your help, really useful, it worked!

    I've got another question. My plugin contains a scheduled task that repeats every x seconds.
    Using function onRun(), I would like to place an goldblock (#41) randomly on a position that is defined.

    I tried doing that but I can't seem to get the $level right :/

    PHP:
        public function onRun($tick) {
            
    $level $this->getServer()->getLevelByName('world');
            
    $block Block::get(41);
            
    $pos = new Vector3(6459844);
            
    $level->setBlock($pos$block);
       }
    [21:47:37] [Server thread/CRITICAL]: Could not execute task teamcraft\Task: Call to undefined method teamcraft\Task::getServer()
    [21:47:37] [Server thread/CRITICAL]: Error: "Call to undefined method teamcraft\Task::getServer()" (EXCEPTION) in "/plugins/TeamCraft/src/teamcraft/Task" at line 28
     
  5. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    PHP:
    //use pocketmine\Server;
    $level Server::getInstance()->getLevelByName("world");
     
  6. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    Tried it, didn't work.
    Here's my full code:

    [21:59:28] [Server thread/CRITICAL]: Could not execute task teamcraft\Task: Call to a member function setBlock() on null
    [21:59:28] [Server thread/CRITICAL]: Error: "Call to a member function setBlock() on null" (EXCEPTION) in "/plugins/TeamCraft/src/teamcraft/Task" at line 35

    PHP:
    namespace teamcraft;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Server;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\Level;
       
    class 
    Task extends PluginTask {
       
        public 
    $plugin;
       
        public function 
    __construct($plugin){
           
            
    $this->plugin $plugin;
            
    parent::__construct($plugin);
       
        }
       
        public function 
    getPlugin() {
           
            return 
    $this->plugin;
           
        }
       
        public function 
    onRun($tick) {
       
            
    $level Server::getInstance()->getLevelByName('world');
           
            
    $block Block::get(17);
            
    $pos = new Vector3(6459844);
            
    $level->setBlock($pos$block);
       
            
    $this->plugin->sendBroadcast('§l[TC]§r An §6goldbox§r has been dropped!');
       
        }
       
    }
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You can also use $this->plugin->getServer()->getLevelByName("world"); if you have defined $plugin in the task constructor.
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What world are you wanting to place the block in?
     
  9. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    The name of the world is called "world", just the default :)
     
  10. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    You need your main class xD
    Try
    PHP:
    public function __construct(/*Your main class"*/ $plugin){}
     
  11. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    This is how the task is created:

    PHP:
    class Main extends PluginBase implements CommandExecutorListener {
         public function 
    onEnable() {
              
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Task($this), 20 5);
              --- 
    more stuff ---
              
    $this->getServer()->getPluginManager()->registerEvents($this$this);
         }
    }
     
  12. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    public function __construct(Main $plugin) {}

    Tried it, not a success.
     
  13. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    With 'use teamcraft\Main'?
     
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    The server isn't finding the world named "world", because Server::getLevelByName("") is returning null. Is it loaded?
     
  15. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    That's not necessary because PHP is weakly-typed. You don't need data types or class names in front of objects.
     
  16. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    Yeah, it is loaded.
    Since the task is executed after the console said "Done", it should be loaded already right?
    Just to test, I'm doing it using my main class now. Outside the task, didn't work either.
     
  17. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You can try making the plugin load postworld and see if that helps. Do that by adding load: POSTWORLD to your plugin.yml if you don't already.

    You could also schedule the task as a delayed repeating task so that it is run after the plugin is enabled. You can do that with something like this:
    PHP:
     $this->getServer()->getScheduler()->scheduleDelayedRepeatingTask(new Task($this), 20 6020 60); //This would run the task every 5 minutes starting 5 minutes after the plugin is enabled.
     
  18. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    It's actually PluginTask::getOwner() unless you've defined your own property.
     
    HimbeersaftLP and corytortoise like this.
  19. jordi1206

    jordi1206 Silverfish

    Messages:
    15
    Well, I tried everything and still nothing works.

    The pocketmine\level\Level Object is empty. Any other idea's of placing blocks scheduled?
     
  20. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    PHP:
    namespace teamcraft;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\WeakPosition;
     
    class 
    MyTask extends PluginTask {
     
        public function 
    onRun($tick) {
            
    // $level = $this->getOwner()->getServer()->getLevelByName('world'); // Get a specific level
            
    $level $this->getOwner()->getServer()->getDefaultLevel(); // Get the default level
      
            
    if($level instanceof Level) {
                    
    $block Block::get(Block::WOOD0, new WeakPosition(6459844$level));
                    
    $level->setBlock($block$block);
                    
    $this->plugin->sendBroadcast('§l[TC]§r An §6goldbox§r has been dropped!');
            } else {
                    
    $this->getOwner()->getLogger()->notice("Expected Level, got " gettype($level) . " instead!"); // Debug code
            
    }
     
        }
     
    }
     
    GilBenDavid and corytortoise like 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.