Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

ScheduleRepeatingTask help

Comments in 'Plugin Development' started by args, Jun 14, 2014.

  1. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    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?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  3. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    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?
  4. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    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);
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.

  7. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
  8. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    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
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  10. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    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
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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?
  12. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    Edit class serverTimer!
    PHP:
    <?php
    namespace 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);
    Last edited: Jun 14, 2014
    args likes this.
  13. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    $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
  14. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    Read again my post. I'm edited code
  15. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    Trying it now :)
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You at least have to understand classes.
  17. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    Code:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new serverTimer($this), 20);
    
    This works!!! :D
    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 :)?
  18. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    Russian
    args likes this.
  19. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    спасибо за помощь мой плагин :D
  20. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    Ты Русский чтоль?

Share This Page

Advertisement