1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

sleep() Alternative

Discussion in 'Development' started by SkyZone, Nov 27, 2016.

  1. SkyZone

    SkyZone Slime

    Messages:
    95
    When i use Sleep() in my Plugin, the whole server "sleeps". How can i fix that? Here is my Code.
    PHP:
    <?php

    namespace 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(15);
      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);
      }
      }
      
      

    }
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    why sleep 60 first doe?
    you are not going to execute any code after 60 second?
     
    applqpak and SkyZone like this.
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Why would you want to use a while loop and sleep() here? Can't you just remove the while loop and sleep?
     
    applqpak and SkyZone like this.
  4. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    Why sleep() at all?

    Anyways for the entire server sleeping, you could use a delayed task.
     
    applqpak and SkyZone like this.
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Ohh the while loop
    there a neat thing called task
     
    applqpak and SkyZone like this.
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    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:
     
    Last edited: Nov 27, 2016
    SkyZone and SalmonDE like this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    This is not a bad practice. This is entirely wrong.
     
    Muqsit, applqpak, SkyZone and 2 others like this.
  8. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    It is bad practice for a PocketMine plugin.
     
    SkyZone likes this.
  9. JackboyPlay

    JackboyPlay Spider

    Messages:
    11
    GitHub:
    JackboyPlay
    Is the sense of sleep (60) for breaking the while loop? If yes break the while loop with break;
     
    SkyZone likes this.
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Horrible.
     
    applqpak, SkyZone and Sandertv like this.
  11. SkyZone

    SkyZone Slime

    Messages:
    95
    And what is a good practice? Im a beginner.
     
  12. SkyZone

    SkyZone Slime

    Messages:
    95
    The Code should be executed every 60 seconds.
     
  13. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Just schedule a repeating task and send a popup for each player every 60 seconds.
     
    applqpak, SkyZone and HimbeersaftLP like this.
  14. Primus

    Primus Zombie Pigman

    Messages:
    749
    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"
     
    applqpak, SkyZone and HimbeersaftLP like this.
  15. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Edited my post
     
    SkyZone likes this.
  16. BalAnce

    BalAnce Silverfish

    Messages:
    22
    GitHub:
    YaBoiBalAnce
    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.
     
    Last edited: Nov 28, 2016
  17. SkyZone

    SkyZone Slime

    Messages:
    95
    Im getting this Error:
    Fatal error: Declaration of derdumme\info\sendTipTask::eek:nRun() must be compatible with pocketmine\scheduler\Task::eek:nRun($currentTick) in C:\Users\...\Desktop\Server 0.16\plugins\Info - DerDumme\src\derdumme\info\info.php on line 23
     
  18. Primus

    Primus Zombie Pigman

    Messages:
    749
    @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){
     
    applqpak, BalAnce and HimbeersaftLP like this.
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    applqpak, BalAnce and HimbeersaftLP like this.
  20. SkyZone

    SkyZone Slime

    Messages:
    95
    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
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.