Hello everyone, I began to developing my plugin for my server. I'm developing my plugin for the new API and it came out a problem that I can not fix! (I use windows) The console says: Call to a member function getOnlinePlayers () on a non-object ... here is my code: class main PHP: <?phpnamespace Test;use pocketmine\event\Listener;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\utils\TextFormat;class main extends PluginBase implements Listener{ public $timer; public function onLoad() {} public function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->timer = new Timer($this); $this->getServer()->getScheduler()->scheduleRepeatingTask($this->timer, 4800); } public function eventi(){ //Call to a member function getOnlinePlayers() on a non-object $online = count($this->getServer()->getOnlinePlayers()); Server::getInstance()->broadcastMessage("There are ".$online." players ..."); } public function onDisable(){}} class time PHP: <?phpnamespace Test;use pocketmine\scheduler\PluginTask;class Timer extends PluginTask{ public $main; public function onRun($currentTick){ $this->main = new main($this); $this->main->eventi(); }} In the old API this code works PHP: $this->api->chat->broadcast("Now there are: ".count($this->api->player->getAll())." players online..."); how can fix this error?
Try changing the Timer class to: PHP: <?phpnamespace Test;use pocketmine\scheduler\PluginTask;class Timer extends PluginTask{ public function onRun($currentTick){ $this->getOwner()->eventi(); }}
PHP: Server::getInstance()->broadcastMessage("Now there are : ". $online. " players online"); Try this :/
Thank you Falk, your solution works. I have another problem, I can not update this short code of the old API to the new API PHP: case 'entity.health.change':return false;break; I tried searching in http://docs.pocketmine.net/ something about the player's health, but I did not find anything, could you help me please?
Yeah, that is generally bad and would lead to an awful memory overflow if you actually initialized the onEnable