help please PHP: <?phpnamespace GetDown;use pocketmine\scheduler\PluginTask;use GetDown\Main;use pocketmine\Player;use pocketmine\utils\Config;//use pocketmine\class Timer extends PluginTask { private $plugin; public function __construct(Main $plugin) { $this->plugin = $plugin; parent::__construct($plugin); } public function onRun($currentTick) { $cfg = new Config($this->plugin->getDataFolder()."config.yml", Config::YAML); $po = $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers(); if(count($po) > 1) { $tts = 30; $tts--; $cfg->set("TimeToStart", $tts); $cfg->save(); $timetostart = $cfg->get("TimeToStart"); $po->sendMessage("Start in $timetostart"); if($timetostart==0) { $po->plugin->sendMessage("sTarTedD"); } } }} In main file PHP: public function onEnable(){ $this->getLogger()->info("Plugin enabled"); @mkdir($this->getDataFolder()); $this->saveResource("config.yml", true); $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getScheduler()->scheduleRepeatingTask(new SignRefresher($this), 50); $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20); $this->saveDefaultConfig(); $cfg = new Config($this->getDataFolder() . "config.yml", Config::YAML);}
Your logic is really bad practice. Why are you saving the value of a variable into the config, just so you can get it's value again? That's unwise and inefficient. This might help you understand better: PHP: <?phpnamespace GetDown;use pocketmine\scheduler\PluginTask;use GetDown\Main;use pocketmine\Player;use pocketmine\utils\Config;//use pocketmine\class Timer extends PluginTask { private $plugin; private $ticks; public function __construct(Main $plugin) { $this->plugin = $plugin; parent::__construct($plugin); } public function onRun($currentTick) { $cfg = new Config($this->plugin->getDataFolder()."config.yml", Config::YAML); $po = $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers(); if(count($po) > 1) { --$this->ticks; $po->sendMessage("Start in " . $this->ticks); if($this->ticks == 0) { $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId()); $po->plugin->sendMessage("sTarTedD"); } } }}
Try to declare as less a variable as possible in the onRun($tick) function. Because it is a repeating task, onRun() is basically a for-loop (just without the "for"). You can declare the variables in the constructor. PHP: private $something;public function __construct(MainClass $plugin){ parent::__construct($plugin); $this->plugin = $plugin; $this->something = 100;}public function onRun($tick){ $this->plugin->getLogger()->info('$something is '.$this->something); //output: $something is 100} Back to you.. PHP: public function __construct(Main $plugin) { $this->plugin = $plugin; $this->tts = 30; //countdown $this->players = $plugin->getServer()->getLevelByName("GDHub")->getPlayers(); parent::__construct($plugin); } public function onRun($currentTick) { if(count($this->players) > 1) { --$this->tts; //pre incrementing foreach ($this->players as $po) { $po->sendMessage("Start in $this->tts"); } } }
Bad idea declaring the player count in the construct method, what if all the players leave? Then you'll have a useless task running which will take up way more resources than assigning a value to a variable.
if there's anywhere that will modify the value of "TimeToStart" on config you should instate try assign a reference instated by using & $a = 5; $b = &$a; $c = &$a; example code so if a or b or c is changed they all will be changed together
I tested muqsit s and xbeastmode s code and it dont start timer i dont know what i doing bad my actually code: PHP: <?phpnamespace GetDown;use pocketmine\Player;use pocketmine\scheduler\PluginTask;use pocketmine\utils\Config;use GetDown\Main;class Timer extends PluginTask{ private $plugin; public $gameprefix = "§l§0[ §r§6Get§eDown §l§0]§r "; public function __construct(Main $plugin) { $this->plugin = $plugin; $this->tts = 30; parent::__construct($plugin); } public function onRun($currentTick) { $players = $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers(); if (count($players) > 1) { { --$this->tts; $po->sendPopup($this->plugin->gameprefix . C::GREEN . "Starting in" . $this->tts); if ($this->tts == 0) { $this->plugin->getServer()->broadcastMessage($this->gameprefix . "Test"); } } } }} Console do not give any errors
You must put that on onEnable() function on the Main class PHP: $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20); Don't forget to add GetDown\Timer on use
Try this PHP: /*** @param $currentTick*/public function onRun($currentTick) { $players = $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers(); if(count($players) > 1) { $this->tts--; $po->sendPopup($this->plugin->gameprefix . C::GREEN . "Starting in" . $this->tts); if($this->tts == 0) { $this->plugin->getServer()->broadcastMessage($this->gameprefix . "Test"); } }}
Mmm It do not work and no errors PHP: <?phpnamespace GetDown;use pocketmine\Player;use pocketmine\scheduler\PluginTask;use pocketmine\utils\Config;use pocketmine\utils\TextFormat as C;use GetDown\Main;class Timer extends PluginTask{ private $plugin; public $tts; public function __construct(Main $plugin) { $this->plugin = $plugin; $this->tts = 50; parent::__construct($plugin); } /** * @param $currentTick */ public function onRun($currentTick) { $players = $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers(); if (count($players) > 1) { $this->tts--; $players->sendPopup($this->plugin->gameprefix . C::GREEN . "Starting in" . $this->tts); if ($this->tts == 0) { $this->plugin->getServer()->broadcastMessage($this->gameprefix . "Test"); } } }}
$players->sendPopup is guaranteed to not work, you are calling a method on an array, wtf. This indicates that, in the case of no errors being displayed, that that line was never run. Please place some echo()s in apropos places to debug your code.