From 3ba56c3e8163f7184a21df4df465c01412d5acca Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 19 Mar 2013 19:54:13 +0100 Subject: [PATCH] Independient Player gamemode, saved between sessions, set it via /gamemode --- src/API/ConsoleAPI.php | 10 ---------- src/API/PlayerAPI.php | 29 ++++++++++++++++++++++++++--- src/Deprecation.php | 2 ++ src/Player.php | 33 +++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 64bbdedd8..f53017046 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -92,15 +92,6 @@ class ConsoleAPI{ $output .= "Stopping the server\n"; $this->server->close(); break; - case "gamemode": - $s = trim(array_shift($params)); - if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1 and ((int) $s) !== 2)){ - $output .= "Usage: /gamemode <0 | 1 | 2>\n"; - break; - } - $this->server->api->setProperty("gamemode", (int) $s); - $output .= "Gamemode changed to ".$this->server->getGamemode()."\n"; - break; case "difficulty": $s = trim(array_shift($params)); if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){ @@ -126,7 +117,6 @@ class ConsoleAPI{ case "?": $output .= "/help: Show available commands\n"; $output .= "/status: Show server TPS and memory usage\n"; - $output .= "/gamemode: Changes default gamemode\n"; $output .= "/difficulty: Changes difficulty\n"; $output .= "/invisible: Manages server visibility\n"; $output .= "/say: Broadcasts mesages\n"; diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f5e455c38..b6f9af6d1 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -37,6 +37,7 @@ class PlayerAPI{ $this->server->api->console->register("list", "Shows connected player list", array($this, "commandHandler")); $this->server->api->console->register("kill", "Kills a player", array($this, "commandHandler")); $this->server->api->console->register("harm", "Harms a player", array($this, "commandHandler")); + $this->server->api->console->register("gamemode", "Changes the player gamemode", array($this, "commandHandler")); $this->server->api->console->register("tppos", "Teleports a player to a position", array($this, "commandHandler")); $this->server->api->console->register("tp", "Teleports a player to another player", array($this, "commandHandler")); $this->server->api->console->alias("suicide", "kill"); @@ -113,6 +114,26 @@ class PlayerAPI{ public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ + case "gamemode": + $gm = -1; + $player = false; + if(!isset($params[1]) and isset($params[0]) and ($issuer instanceof Player)){ + $player = $issuer; + $gm = (int) $params[1]; + }elseif(isset($params[1]) and isset($params[0])){ + $player = $this->server->api->player->get($params[0]); + $gm = (int) $params[1]; + } + if(!($player instanceof Player) or $gm < 0 or $gm > 2){ + $output .= "Usage: /gamemode [player] <0 | 1 | 2>\n"; + break; + } + + + if($player->setGamemode($gm)){ + $output .= "Gamemode of ".$player->username." changed to ".$player->getGamemode()."\n"; + } + break; case "tp": if(!isset($params[1]) and isset($params[0]) and ($issuer instanceof Player)){ $name = $issuer->username; @@ -248,6 +269,7 @@ class PlayerAPI{ $player = $this->server->clients[$CID]; console("[INFO] Player \"\x1b[33m".$player->username."\x1b[0m\" connected from \x1b[36m".$player->ip.":".$player->port."\x1b[0m"); $player->data = $this->getOffline($player->username); + $player->gamemode = $player->data->get("gamemode"); $this->server->query("INSERT OR REPLACE INTO players (clientID, ip, port, name) VALUES (".$player->clientID.", '".$player->ip."', ".$player->port.", '".$player->username."');"); } } @@ -282,6 +304,7 @@ class PlayerAPI{ ), "inventory" => array_fill(0, 36, array(AIR, 0, 0)), "armor" => array_fill(0, 4, array(AIR, 0, 0)), + "gamemode" => $this->server->gamemode, "health" => 20, "lastIP" => "", "lastID" => 0, @@ -291,15 +314,15 @@ class PlayerAPI{ console("[NOTICE] Player data not found for \"".$iname."\", creating new profile"); $data->save(); } - if($this->server->gamemode === 1){ + if($this->server->gamemode === CREATIVE){ $data->set("health", 20); } - $this->server->handle("api.player.offline.get", $data); + $this->server->handle("player.offline.get", $data); return $data; } public function saveOffline(Config $data){ - $this->server->handle("api.player.offline.save", $data); + $this->server->handle("player.offline.save", $data); $data->save(); } } \ No newline at end of file diff --git a/src/Deprecation.php b/src/Deprecation.php index 015a24126..36d2eb5c8 100644 --- a/src/Deprecation.php +++ b/src/Deprecation.php @@ -31,6 +31,8 @@ class Deprecation{ "world.block.change" => "block.change", "block.drop" => "item.drop", "api.op.check" => "op.check", + "api.player.offline.get" => "player.offline.get", + "api.player.offline.save" => "player.offline.save", ); diff --git a/src/Player.php b/src/Player.php index 45f43ee95..b8f5e5e84 100644 --- a/src/Player.php +++ b/src/Player.php @@ -188,6 +188,7 @@ class Player{ )); $this->data->set("inventory", $this->inventory); $this->data->set("armor", $this->armor); + $this->data->set("gamemode", $this->gamemode); } } @@ -490,6 +491,34 @@ class Player{ "pitch" => 0, )); } + + public function getGamemode(){ + switch($this->gamemode){ + case SURVIVAL: + return "survival"; + case CREATIVE: + return "creative"; + case ADVENTURE: + return "adventure"; + } + } + + public function setGamemode($gm){ + if($gm < 0 or $gm > 2 or $this->gamemode === $gm){ + return false; + } + + if(($this->gamemode === SURVIVAL and $gm === ADVENTURE) or ($this->gamemode === ADVENTURE and $gm === SURVIVAL)){ + $this->gamemode = $gm; + $this->sendSettings(); + $this->eventHandler("Your gamemode has been changed to ".$this->getGamemode()."..", "server.chat"); + }else{ + $this->gamemode = $gm; + $this->eventHandler("Your gamemode has been changed to ".$this->getGamemode().", you've to do a forced reconnect.", "server.chat"); + $this->server->schedule(30, array($this, "close")); //Forces a kick + } + return true; + } public function handle($pid, $data){ if($this->connected === true){ @@ -610,7 +639,7 @@ class Player{ $this->close("bad username", false); break; } - $o = $this->server->api->player->getOffline($this->username); + if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){ $this->close("\"\x1b[33m".$this->username."\x1b[0m\" not being on white-list", false); break; @@ -622,12 +651,12 @@ class Player{ $u->close("logged in from another location"); } + $this->server->api->player->add($this->CID); if($this->server->api->handle("player.join", $this) === false){ $this->close(); return; } - $this->server->api->player->add($this->CID); $this->auth = true; if(!$this->data->exists("inventory") or $this->gamemode === CREATIVE){ $this->data->set("inventory", $this->inventory);