diff --git a/PocketMine-MP.php b/PocketMine-MP.php index 5b3ef41a8b..e405a1d554 100644 --- a/PocketMine-MP.php +++ b/PocketMine-MP.php @@ -25,7 +25,7 @@ the Free Software Foundation, either version 3 of the License, or */ -require_once("src/common/dependencies.php"); +require_once(dirname(__FILE__)."/src/common/dependencies.php"); require_once("classes/PocketMinecraftServer.class.php"); require_once("API/ServerAPI.php"); diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 14e9d5c1eb..262b926f84 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -35,8 +35,17 @@ class ChatAPI{ } - public function send($a, $b){//a == name of owner. b == message - $this->server->chat($a, $b); - return true; + public function broadcast($message){ + $this->send(false, $message); + } + + public function send($owner, $text, $whitelist = false, $blacklist = false){ + $message = ""; + if($owner !== false){ + $message = "<".$owner."> "; + } + $message .= $text; + console("[CHAT] ".$message); + $this->server->handle("server.chat", new Container($message, $whitelist, $blacklist)); } } \ No newline at end of file diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 649ab3a52f..95db7c427e 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -147,7 +147,7 @@ class ConsoleAPI{ console("[INFO] Usage: /say "); break; } - $this->server->chat(false, $s); + $this->server->api->chat->broadcast($s); break; case "whitelist": $p = strtolower(array_shift($params)); diff --git a/src/classes/Player.class.php b/src/classes/Player.class.php index ff5760e1d3..79dad095b7 100644 --- a/src/classes/Player.class.php +++ b/src/classes/Player.class.php @@ -98,7 +98,7 @@ class Player{ foreach($this->evid as $ev){ $this->server->deleteEvent($ev); } - $this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat"); + $this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat"); $this->dataPacket(MC_LOGIN_STATUS, array( "status" => 1, )); @@ -106,7 +106,7 @@ class Player{ $this->connected = false; if($msg === true){ - $this->server->api->dhandle("server.chat", $this->username." left the game"); + $this->server->api->chat->broadcast($this->username." left the game"); } console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); $this->server->api->player->remove($this->CID); @@ -166,8 +166,17 @@ class Player{ )); break; case "server.chat": + if(($data instanceof Container) === true){ + if(!$data->check($this->username)){ + return; + }else{ + $message = $data->get(); + } + }else{ + $message = (string) $data; + } $this->dataPacket(MC_CHAT, array( - "message" => str_replace("@username", $this->username, $data), + "message" => str_replace("@username", $this->username, $message), )); break; } diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 63adbf58c5..2bdb277cfc 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -94,15 +94,13 @@ class PocketMinecraftServer{ } public function loadEvents(){ - $this->event("server.chat", array($this, "eventHandler")); $this->event("player.new", array($this, "eventHandler")); - $this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->api->dhandle("server.time.change", $this->time);'); $this->action(5000000, 'if($this->difficulty < 2){$this->api->dhandle("server.regeneration", 1);}'); $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); if($this->api !== false){ - $this->action(1000000 * 80, '$cnt = count($this->clients); if($cnt > 1){$this->chat(false, "Online (".$cnt."): ".implode(", ",$this->api->player->online()));}'); + $this->action(1000000 * 80, '$cnt = count($this->clients); if($cnt > 1){$this->api->chat->broadcast("Online (".$cnt."): ".implode(", ",$this->api->player->online()));}'); } $this->action(1000000 * 120, '$this->debugInfo(true);'); } @@ -163,7 +161,7 @@ class PocketMinecraftServer{ public function close($reason = "stop"){ if($this->stop !== true){ - $this->chat(false, "Stopping server..."); + $this->api->chat->send(false, "Stopping server..."); $this->ticker->stop = true; $this->save(true); $this->stop = true; @@ -172,13 +170,8 @@ class PocketMinecraftServer{ } } - public function chat($owner, $text, $target = true){ - $message = ""; - if($owner !== false){ - $message = "<".$owner."> "; - } - $message .= $text; - $this->handle("server.chat", $message); + public function chat($owner, $text, $target = false){ + $this->api->chat->send($owner, $text, $target); } public function setType($type = "normal"){ @@ -234,9 +227,6 @@ class PocketMinecraftServer{ case "player.new": console("[DEBUG] Player \"".$data["username"]."\" EID ".$data["eid"]." spawned at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); break; - case "server.chat": - console("[CHAT] $data"); - break; } } @@ -303,7 +293,7 @@ class PocketMinecraftServer{ } } console("[DEBUG] Loaded ".count($this->entities)." Entities", true, true, 2); - $this->action(1000000 * 60 * 15, '$this->chat(false, "Forcing save...");$this->save();$this->chat(false, "Done");'); + $this->action(1000000 * 60 * 15, '$this->api->chat->broadcast("Forcing save...");$this->save();'); } } diff --git a/src/misc/utils/Container.php b/src/misc/utils/Container.php new file mode 100644 index 0000000000..d3ae5fbbab --- /dev/null +++ b/src/misc/utils/Container.php @@ -0,0 +1,73 @@ +payload = $payload; + if(is_array($whitelist)){ + $this->whitelist = $whitelist; + } + if(is_array($blacklist)){ + $this->blacklist = $blacklist; + } + } + + public function get(){ + return $this->payload; + } + + public function check($target){ + $w = true; + $b = false; + if($this->whitelist !== false){ + $w = false; + if(in_array($target, $this->whitelist, true)){ + $w = true; + } + }else{ + $w = true; + } + if($this->blacklist !== false){ + $b = true; + if(in_array($target, $this->blacklist, true)){ + $b = false; + } + }else{ + $b = false; + } + if($w === false or $b === true){ + return false; + } + return true; + } + + + public function __toString(){ + return $this->payload; + } +} \ No newline at end of file