/** * 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}"); } } }
/** * To write the JSON Errors * (if any) into the logs * @return bool */ public static function getJsonErrors() { switch (json_last_error()) { case JSON_ERROR_DEPTH: $error = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $error = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $error = 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $error = 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; } if (isset($error)) { Config::getLogger()->error(TextFormat::DARK_RED . $error); } return isset($error) ? true : false; }
/** * Save the users into the file * @return bool */ public static function save() { file_put_contents(Config::getDataFolder() . "users.json", json_encode(self::$users, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); if (!Utils::getJsonErrors()) { Config::getLogger()->info("Users saved !"); } else { Config::getLogger()->error("An error occured while saving users !"); } return Utils::getJsonErrors(); }
/** * Save the users on run * @param $currentTick */ public function onRun($currentTick) { Config::getLogger()->info("Saving of all users ..."); User::save(); }