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!
You can replace old block to air block PHP: $level->setBlock(new Vector3($x, $y, $z), Block::get(0), true, true);
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.
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(645, 9, 844); $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
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(645, 9, 844); $level->setBlock($pos, $block); $this->plugin->sendBroadcast('§l[TC]§r An §6goldbox§r has been dropped!'); } }
You can also use $this->plugin->getServer()->getLevelByName("world"); if you have defined $plugin in the task constructor.
This is how the task is created: PHP: class Main extends PluginBase implements CommandExecutor, Listener { public function onEnable() { $this->getServer()->getScheduler()->scheduleRepeatingTask(new Task($this), 20 * 5); --- more stuff --- $this->getServer()->getPluginManager()->registerEvents($this, $this); }}
The server isn't finding the world named "world", because Server::getLevelByName("") is returning null. Is it loaded?
That's not necessary because PHP is weakly-typed. You don't need data types or class names in front of objects.
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.
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), 5 * 20 * 60, 5 * 20 * 60); //This would run the task every 5 minutes starting 5 minutes after the plugin is enabled.
Well, I tried everything and still nothing works. The pocketmine\level\Level Object is empty. Any other idea's of placing blocks scheduled?
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::WOOD, 0, new WeakPosition(645, 9, 844, $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 } } }