Can someone please explain me how to add Objects(Player, Level...) to class. So I can use them in my Task(onRun() Function). Just want to send Message to Player via Task. First time making a task. #newbie Main Class: ->Just the part for Task<- PHP: $this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer()), 120); BroadcastTask.php PHP: class BroadcastTask extends Task implements Listener{ /** @var Server */ private $server; public function __construct(Server $server){ $this->server = $server; } public function onRun(int $currentTick) : void{ $this->server->broadcastMessage("[ExamplePlugin] Derzeitiger Tick " . $currentTick); }
$this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer(), $player), 120); public function __construct(Server $server, \pocketmine\Player $player){ $this->server = $server; $this->player = $player; }
Now? Server thread/CRITICAL ErrorException: "Undefined variable: player" (EXCEPTION) in "FZ.phar/src/FZ/Main" at line 44 23.10 19:58:57 [Server] Server thread/DEBUG #0 FZ.phar/src/FZ/Main(44): pocketmine\utils\Utils::errorExceptionHandler(integer 8, string Undefined variable: player, string phar:///plugins/FZ.phar/src/FZ/Main.php, integer 44, array Array())
I told you how to pass the player in the constructor. You have to define the player. What do you want to do, so I will be able to help you?
Define player ✔️ First of all I want to learn how to make a task. I think I got it now a bit. My goal is to make a sign refreshing itself and show me the players in a specific world.
you could do this PHP: foreach($this->server->getOnlinePlayers() as $player){$player->sendMessage("You are " . $player->getName());}
Use the code you had then. Do something like that: $count = count($this->server>getLevelByName("worldname")->getPlayers()); then send a message with $count. like with $currentTick in your example.
I wouldn't pass any objects to your task unless you need to directly modify or use those objects, and even then, you could just use player name or some ID rather than the Player object. Assuming you want to do it on a task, I'd do something like this: PHP: //In your PluginBase class: $this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer(), count($this->getServer()->getLevelByName("worldname")->getPlayers())), 120);//In your BroadcastTask class:class BroadcastTask extends Task implements Listener{ /** @var Server */ private $server; /** @int PlayerCount */ private $count = 0; public function __construct(Server $server, int $count){ $this->server = $server; $this->count = $count; //You can now use the player count however you want in your task. } public function onRun(int $currentTick) : void{ $this->server->broadcastMessage("[ExamplePlugin] Derzeitiger Tick " . $currentTick); }} You probably see that you can add any variable to a task just by putting it in the object class' constructor and by specifying it when you create an object, like you did with the server object. I didn't include a player object because it was unnecessary. Assuming the task isn't intended for only one specific player, you can use the Server object you already have to get all players on the server, get the server IP, specific Levels, etc. However, you didn't include the PluginBase object, which most people do instead of the Server. PluginBase can access everything you would ever need, so it's typically more useful, but not necessarily in every case.