When i use Sleep() in my Plugin, the whole server "sleeps". How can i fix that? Here is my Code. PHP: <?phpnamespace derdumme\info;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\utils\TextFormat;use pocketmine\event\player\PlayerJoinEvent;class info extends PluginBase implements Listener { public function onEnable() { $this->getLogger()->info(TextFormat::GREEN."wurde aktiviert."); $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onDisable() { $this->getLogger()->info(TextFormat::RED."wurde deaktiviert."); } public function onJoin(PlayerJoinEvent $event){ while(true) { $message = random_int(1, 5); if($message == 1){ $event->getPlayer()->sendPopup(TextFormat::GREEN."Teamspeak-Server: ts.skyzonemc.net"); } elseif ($message == 2) { $event->getPlayer()->sendPopup(TextFormat::GOLD."Website: SkyZoneMC.net"); } elseif ($message == 3) { $event->getPlayer()->sendTip("This is a Tip"); } sleep(60); } } }
Why would you want to use a while loop and sleep() here? Can't you just remove the while loop and sleep?
This is not the way how to do it in PocketMine, use a RepeatingTask instead. Your thinking was logical, but "while" in a plugin freezes the server. Original post:
This is correct way of thinking but you didn't took thread blocking in consideration. Although code is terrible it has some sense in it. Just google "Scheduling Repeating task in PocketMine Tutorial"
You should use repeating task instead of what you used. Put this in onEnable() function in your main class PHP: //parameters PluginTask $class | $delay$this->getServer()->getScheduler()->schedulerepeatingtask(new sendTipTask(),60); create a new class this task will be repeated with a delay of 60 ticks in between (aprox 3 secs i think) PHP: class sendTipTask extends PluginTask { private $plugin public function __construct(info $plugin){ parent::__construct($plugin); $this->plugin = $plugin; } public function onRun($currentTick){ foreach($this->plugin->getServer()->getOnlinePlayers() as $player){ $messages = [ "message 1", "message 2", "etc" ]; $player->sendMessage($messages[array_rand($messages)]); } } } Hopefully helped. Also now you dont need the PlayerJoinEvent anymore.
Im getting this Error: Fatal error: Declaration of derdumme\info\sendTipTask:nRun() must be compatible with pocketmine\scheduler\Task:nRun($currentTick) in C:\Users\...\Desktop\Server 0.16\plugins\Info - DerDumme\src\derdumme\info\info.php on line 23
@BalAnce forgot to add parameter $currentTick for Task :: onRun() method. The error says that your method signature differs from parent. If you don't know what that means, don't worry simply look at this code and correct yours PHP: public function onRun($currentTick){
A bad practice is something that would still work and caused minimal impacts, but not encouraged. Use of sleep() entirely stops the server, so it is not only discouraged, but also wrong and unacceptable at all. Reminder: you forgot to pass the plugin parameter. You actually don't need such assignment. If you aren't concerned about the autocompletion from IDEs, you can use $this->owner to access the main class directly.
Now im getting this error: [11:44:20] [Server thread/CRITICAL]: TypeError: "Argument 1 passed to derdumme\info\sendTipTask::__construct() must be an instance of derdumme\info\info, none given, called in C:\Users\ik83\Desktop\Server 0.16\plugins\Info - DerDumme\src\derdumme\info\info.php on line 15" (EXCEPTION) in "/Info - DerDumme/src/derdumme/info/info" at line 26