Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace serverdeathrestarter;
  4.  
  5. use pocketmine\Thread;
  6.  
  7. class CheckerThread extends Thread{
  8.     public $lastAlive;
  9.     /** @var number */
  10.     private $timeout;
  11.     public function __construct($timeout){
  12.         $this->timeout = $timeout;
  13.     }
  14.     public function run(){
  15.         $this->lastAlive = microtime(true);
  16.         while(true){
  17.             $time = microtime(true);
  18.             if($time - $this->lastAlive > $this->timeout){
  19.                 die("Main thread timeout ($this->timeout second(s))");
  20.             }
  21.             sleep(1);
  22.         }
  23.     }
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30. =====================================================
  31.  
  32.  
  33. <?php
  34.  
  35. namespace serverdeathrestarter;
  36.  
  37. use pocketmine\command\Command;
  38. use pocketmine\command\CommandSender;
  39. use pocketmine\plugin\PluginBase;
  40.  
  41. class SDRPlugin extends PluginBase{
  42.     public function onEnable(){
  43.         $this->saveDefaultConfig();
  44.         $thread = new CheckerThread($this->getConfig()->get("timeout"));
  45.         $thread->start();
  46.     }
  47.     public function onCommand(CommandSender $sender, Command $cmd, $alias, array $args){
  48.         if($cmd->getName() === "serverdie"){
  49.             die;
  50.         }
  51.         if($cmd->getName() === "suspend"){
  52.             $this->suspendThread();
  53.         }
  54.     }
  55.     /**
  56.      * Function for testing
  57.      */
  58.     public static function suspendThread(){
  59.         while(true);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement