From fb019a88c1e503129cee5e5319f55dd82edb2679 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 12 Dec 2012 00:07:55 +0100 Subject: [PATCH] Server restart without closing, added more info --- classes/API/ConsoleAPI.php | 8 +++++++- classes/API/PlayerAPI.php | 2 +- classes/API/ServerAPI.php | 3 ++- classes/PocketMinecraftServer.class.php | 3 ++- classes/UDPSocket.class.php | 8 ++++++-- server.php | 11 ++++++++--- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/classes/API/ConsoleAPI.php b/classes/API/ConsoleAPI.php index f73e38e1f..026fd855c 100644 --- a/classes/API/ConsoleAPI.php +++ b/classes/API/ConsoleAPI.php @@ -45,7 +45,12 @@ class ConsoleAPI{ $this->server->api->setProperty("last-update", time()); break; case "stop": - console("[INFO] Stopping server..."); + console("[INFO] Stopping the server..."); + $this->server->close(); + break; + case "restart": + console("[INFO] Restarting the server..."); + $this->server->api->restart = true; $this->server->close(); break; case "banip": @@ -174,6 +179,7 @@ class ConsoleAPI{ console("[INFO] /whitelist: Manages whitelisting"); console("[INFO] /banip: Manages IP ban"); console("[INFO] /stop: Stops the server"); + console("[INFO] /restart: Restarts the server"); foreach($this->help as $c => $h){ console("[INFO] /$c: ".$h[0]); } diff --git a/classes/API/PlayerAPI.php b/classes/API/PlayerAPI.php index 3ba282985..d247ec129 100644 --- a/classes/API/PlayerAPI.php +++ b/classes/API/PlayerAPI.php @@ -49,7 +49,7 @@ class PlayerAPI{ case "list": console("[INFO] Player list:"); foreach($this->server->clients as $c){ - console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID); + console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->username->entity->position["x"], 2).", ".round($c->username->entity->position["y"], 2).", ".round($c->username->entity->position["z"], 2).")"); } break; } diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index d2a696949..26f16339c 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -26,7 +26,7 @@ the Free Software Foundation, either version 3 of the License, or */ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in runtime! - var $server; + var $server, $restart = false; private $config, $apiList = array(); function __construct(){ console("[INFO] Starting ServerAPI server handler..."); @@ -260,6 +260,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run public function start(){ $this->server->start(); + return $this->restart; } diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 56a2b0603..e4d58865e 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -130,11 +130,12 @@ class PocketMinecraftServer extends stdClass{ return $info; } - public function close($reason = "stop"){ + public function close($reason = "stop"){ $this->chat(false, "Stopping server..."); $this->save(); $this->stop = true; $this->trigger("onClose"); + $this->interface->close(); } public function chat($owner, $text, $target = true){ diff --git a/classes/UDPSocket.class.php b/classes/UDPSocket.class.php index 9d3ffce75..202a33c89 100644 --- a/classes/UDPSocket.class.php +++ b/classes/UDPSocket.class.php @@ -48,8 +48,12 @@ class UDPSocket{ $this->buffer = array(); $this->unblock(); }else{ - socket_bind($this->sock, "0.0.0.0", $port); - $this->unblock(); + if(socket_bind($this->sock, "0.0.0.0", $port) === true){ + $this->unblock(); + }else{ + console("[ERROR] Couldn't bind to 0.0.0.0:".$port, true, true, 0); + die(); + } } } } diff --git a/server.php b/server.php index bee65d45b..1833f6530 100644 --- a/server.php +++ b/server.php @@ -29,7 +29,12 @@ require_once("common/dependencies.php"); require_once("classes/PocketMinecraftServer.class.php"); require_once("classes/API/ServerAPI.php"); -$server = new ServerAPI(); -//You can add simple things here +while(true){ + $server = new ServerAPI(); + //You can add simple things here -$server->start(); + if($server->start() !== true){ + break; + } + console("[NOTICE] The server is restarting...", true, true, 0); +}