コード例 #1
1
 /**
  * When the player join
  * @param PlayerJoinEvent $event
  */
 public function onPlayerJoin(PlayerJoinEvent $event)
 {
     $player = $event->getPlayer();
     // If the user exist
     if (User::exist($player->getName())) {
         $user = User::get($player->getName());
         // Get the login timeout from the config
         $minutes = Config::get("config.loginProtection.loginTimeout");
         // If is not an integer, set default value and write an error in the logs
         if (!is_int($minutes)) {
             $minutes = 5;
             Config::getLogger()->error("The LoginProtection loginTimeout is not and integer and will be the default time (5 minutes)");
         }
         // Add $minutes to the last_disconnection time
         $maxTime = date('Y-m-d H:i:s', strtotime($user->last_disconnection . ' + ' . $minutes . ' minute'));
         $now = date('Y-m-d H:i:s');
         // If he is logged and the maxTime is < than the actual time, then logout him.
         if ($user->isLogged() && $maxTime < $now) {
             $user->logout();
         }
     }
     // If he is disconnected, tell him to log in
     if (isset($user) && !$user->isLogged()) {
         $player->sendMessage(TextFormat::GOLD . "Welcome, you need to login before playing");
     } else {
         if (isset($user) && $user->isLogged()) {
             $player->sendMessage(TextFormat::GREEN . "Welcome back {$user->username}");
         }
     }
 }