Could someone please help me use a ScheduleRepeatingTask? Code: public function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getScheduler()->scheduleRepeatingTask(serverTimer(), 20); } public function serverTimer(){ foreach($this->getServer()->getOnlinePlayers() as $onlinePlayer){ $onlinePlayer->sendMessage("-------"); } } } I'm clearly doing something wrong, am i supposed to be making a whole other class instead or something? thanks for reading I get an error each time and i've tried a couple of things to solve it without success. Is a task just a method?
A task is another object. If you do it in this way, it will pass the void result from serverTimer() to the schedule, and the function will only be called when you initialize.
So i would need a new class, or am i interpreting this wrong? And would that class need to do something like, "implements runnable" like a java timer would?
Add this: PHP: use pocketmine\Server;use pocketmine\Player;use pocketmine\scheduler\PluginTask;use pocketmine\plugin\PluginBase; And Try this PHP: Server::getInstance()->broadcastMessage("Hello!"); And this PHP: $task = new CallbackTask(array($this, "serverTimer"));$time = 20;$this->getServer()->getScheduler()->scheduleRepeatingTask($task, $time);
Exactly. However, in Java you can create anonymous classes like: Code: Server.getInstance().getScheduler().scheduleRepeatingTask(new PluginTask(this){ @Override public void onRun(long ticks){ Main.this.serverTimer(); } }); But in PHP, you must create a new class. There are anonymous functions (not annoymous. I just realized I had spelt wrong for two years!) in PHP but not anonymous classes.
http://jenkins.pocketmine.net/job/P...e_1_1scheduler_1_1_callback_task.html#details It clearly specified that you may NOT use this class in plugins.
Oh, realy... This good? PHP: $time = 20; $this->getServer()->getScheduler()->scheduleRepeatingTask(new Broadcaster($this), $time);
I'm trying a couple things at the moment, none working for me unfortunately I created a serverTimer class with an update timer method, though phpstorm gives me the error "Undefined class" So the class is there, I'm just not sure how to have the main class reach it thanks both for your help btw
Try uploading it to Github and let us see the full source. A good IDE is everything if you know nothing about the language, like Eclipse for me to Java before.
I only have 2 classes, a main and one that I want to run every second, So its not really worth putting on github yet. Once i can see an example of how that's done I'll be set to code the things that im after Ill paste them below anway: Code: <?php namespace testplugin; class testplugin extends \pocketmine\plugin\PluginBase implements \pocketmine\command\CommandExecutor, \pocketmine\event\Listener{ public function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getScheduler()->scheduleRepeatingTask(serverTimer()->, 20); $time = 20; } public function onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $command, $label, array $params){ if($command->getName()=="test"){ $sender->sendMessage("Hello world this is the beginning of the greatest party ever"); return true; } } public function onChatEvent(\pocketmine\event\player\PlayerChatEvent $event){ $message = $event->getMessage(); $p = $event->getPlayer()->getName(); $event->setCancelled(true); foreach($this->getServer()->getOnlinePlayers() as $onlinePlayer){ $onlinePlayer->sendMessage($p.": ".$message); } } public function onDisable(){ } } Code: <?php namespace serverTimer; class serverTimer extends \pocketmine\plugin\PluginBase{ public function updateTime(){ $this->getLogger()->notice("test"); } } + of couse theres the yml but it's not of any use too see at the moment. The plugin runs, it just gives an error when reaching the Repeating task bit and closes
Learn PHP. Sorry for saying this. serverTimer is a class. Schedule with new serverTimer(). Also, conventions. Capital first letter in class names. Also, extend PluginTask not PluginBase. What are you doing?
Edit class serverTimer! PHP: <?phpnamespace testplugin;use pocketmine\Player;use pocketmine\scheduler\PluginTask;use pocketmine\Server;class serverTimer extends PluginTask{public function onRun(){Server::getInstance()->broadcastMessage("[Test] Hello ");}} Then PHP: $this->getServer()->getScheduler()->scheduleRepeatingTask(serverTimer()->, 20);$time = 20; It is not work! Try this: PHP: $time = 20;$this->getServer()->getScheduler()->scheduleRepeatingTask(new serverTimer($this), $time);
$this->getServer()->getScheduler()->scheduleRepeatingTask(new serverTimer(), 20); which I've tried before still gives the error "Undefined server class" And I was ignoring a lot of conventions because I was just testing to get something running, and I'll be deleting it once i understand how its done. As for the plugin task, that's something specific to pocketmine that I needed to learn, not php itself right? TBH i figured I don't need a complete understanding of PHP to create the plugins I'm making so long as I got subclasses and this timer running, I have enough knowledge to do what I'm interested in doing as far as the entire rest of the plugin goes. Thanks for your help and advice though, its appreciated
Code: $this->getServer()->getScheduler()->scheduleRepeatingTask(new serverTimer($this), 20); This works!!! Yes thank you so much!! this has been very helpful @xpyctum! I appreciate it The only thing I needed to change was adding the return value to the method, then it works: Code: public function onRun(){ Code: public function onRun($currentTick){ What is your home language ?