diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index b9c66d1c..1483f7eb 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -45,21 +45,26 @@ class BanAPI{ $this->server->api->console->register("whitelist", "Manages White-listing", array($this, "commandHandler")); } - public function commandHandler($cmd, $params){ + public function commandHandler($cmd, $params, $issuer){ + $output = ""; switch($cmd){ case "kick": if(!isset($params[0])){ - console("[INFO] Usage: /kick [reason]"); + $output .= "Usage: /kick [reason]\n"; }else{ $name = array_shift($params); $player = $this->server->api->player->get($name); if($player === false){ - console("[ERROR] Player \"".$name."\" does not exist"); + $output .= "Player \"".$name."\" does not exist\n"; }else{ $reason = implode(" ", $params); $reason = $reason == "" ? "No reason":$reason; $player->close("You have been kicked: ".$reason); - console("[INFO] Player \"".$player->username."\" has been kicked: $reason"); + if($issuer instanceof Player){ + $this->server->api->chat->broadcast($player->username." has been kicked by ".$issuer->username.": $reason\n"); + }else{ + $this->server->api->chat->broadcast($player->username." has been kicked: $reason\n"); + } } } break; @@ -70,34 +75,34 @@ class BanAPI{ $user = trim(implode(" ", $params)); $this->whitelist->remove($user); $this->whitelist->save(); - console("[INFO] Player \"$user\" removed from white-list"); + $output .= "Player \"$user\" removed from white-list\n"; break; case "add": $user = trim(implode(" ", $params)); $this->whitelist->set($user); $this->whitelist->save(); - console("[INFO] Player \"$user\" added to white-list"); + $output .= "Player \"$user\" added to white-list\n"; break; case "reload": $this->whitelist = $this->load(FILE_PATH."white-list.txt", CONFIG_LIST); break; case "list": - console("[INFO] White-list: ".implode(", ", $this->whitelist->getAll(true))); + $output .= "White-list: ".implode(", ", $this->whitelist->getAll(true))."\n"; break; case "on": case "true": case "1": - console("[INFO] White-list turned on"); + $output .= "White-list turned on\n"; $this->server->api->setProperty("white-list", true); break; case "off": case "false": case "0": - console("[INFO] White-list turned off"); + $output .= "White-list turned off\n"; $this->server->api->setProperty("white-list", false); break; default: - console("[INFO] Usage: /whitelist [username]"); + $output .= "Usage: /whitelist [username]\n"; break; } break; @@ -109,23 +114,23 @@ class BanAPI{ $ip = trim(implode($params)); $this->bannedIPs->remove($ip); $this->bannedIPs->save(); - console("[INFO] IP \"$ip\" removed from ban list"); + $output .= "IP \"$ip\" removed from ban list\n"; break; case "add": case "ban": $ip = trim(implode($params)); $this->bannedIPs->set($ip); $this->bannedIPs->save(); - console("[INFO] IP \"$ip\" added to ban list"); + $output .= "IP \"$ip\" added to ban list\n"; break; case "reload": $this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST); break; case "list": - console("[INFO] IP ban list: ".implode(", ", $this->bannedIPs->getAll(true))); + $output .= "IP ban list: ".implode(", ", $this->bannedIPs->getAll(true))."\n"; break; default: - console("[INFO] Usage: /banip [IP]"); + $output .= "Usage: /banip [IP]\n"; break; } break; @@ -137,7 +142,7 @@ class BanAPI{ $user = trim(implode($params)); $this->banned->remove($user); $this->banned->save(); - console("[INFO] Player \"$user\" removed from ban list"); + $output .= "Player \"$user\" removed from ban list\n"; break; case "add": case "ban": @@ -149,20 +154,26 @@ class BanAPI{ $player->close("You have been banned"); } $this->server->api->chat->broadcast("$user has been banned"); - console("[INFO] Player \"$user\" added to ban list"); + if($issuer instanceof Player){ + $this->server->api->chat->broadcast($user." has been banned by ".$issuer->username."\n"); + }else{ + $this->server->api->chat->broadcast($user." has been banned\n"); + } + $output .= "Player \"$user\" added to ban list\n"; break; case "reload": $this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST); break; case "list": - console("[INFO] Ban list: ".implode(", ", $this->banned->getAll(true))); + $output .= "Ban list: ".implode(", ", $this->banned->getAll(true))."\n"; break; default: - console("[INFO] Usage: /ban [player]"); + $output .= "Usage: /ban [player]\n"; break; } break; } + return $output; } public function ban($username){ diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 6f2f9a44..245e7ac2 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -39,14 +39,18 @@ class ChatAPI{ $this->send(false, $message); } - public function sendTo($owner, $text, $username){ - $this->send($owner, $text, array($username)); + public function sendTo($owner, $text, $player){ + $this->send($owner, $text, array($player)); } public function send($owner, $text, $whitelist = false, $blacklist = false){ $message = ""; if($owner !== false){ - $message = "<".$owner."> "; + if($owner instanceof Player){ + $message = "<".$owner->username."> "; + }else{ + $message = "<".$owner."> "; + } } $message .= $text; console("[CHAT] ".$message); diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 357c9176..ab674aba 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -47,96 +47,97 @@ class ConsoleAPI{ $this->loop->join(); } - public function defaultCommands($cmd, $params){ - switch($cmd){ - case "crash": //Crashes the server to generate an report - $this->callNotDefinedMethodCrash(); - $this->server->api->server; //Access a private property - callNotExistingFunction(); - break; - case "invisible": - $p = strtolower(array_shift($params)); - switch($p){ - case "on": - case "true": - case "1": - console("[INFO] Server is invisible"); - $this->server->api->setProperty("invisible", true); - break; - case "off": - case "false": - case "0": - console("[INFO] Server is visible"); - $this->server->api->setProperty("invisible", false); - break; - default: - console("[INFO] Usage: /invisible "); - break; - } - break; - case "status": - case "lag": - $this->server->debugInfo(true); - $info = $this->server->debugInfo(); - console("[INFO] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")"); - break; - case "update-done": - $this->server->api->setProperty("last-update", time()); - break; - case "stop": - $this->loop->stop = true; - console("[INFO] Stopping the server..."); - $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)){ - console("[INFO] Usage: /gamemode <0 | 1 | 2>"); + public function defaultCommands($cmd, $params, $issuer){ + $output = ""; + switch($cmd){ + case "crash": //Crashes the server to generate an report + $this->callNotDefinedMethodCrash(); + $this->server->api->server; //Access a private property + callNotExistingFunction(); break; - } - $this->server->api->setProperty("gamemode", (int) $s); - console("[INFO] Gamemode changed to ".$this->server->getGamemode()); - break; - case "difficulty": - $s = trim(array_shift($params)); - if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){ - console("[INFO] Usage: /difficulty <0 | 1>"); + case "invisible": + $p = strtolower(array_shift($params)); + switch($p){ + case "on": + case "true": + case "1": + $output .= "Server is invisible\n"; + $this->server->api->setProperty("invisible", true); + break; + case "off": + case "false": + case "0": + $output .= "Server is visible\n"; + $this->server->api->setProperty("invisible", false); + break; + default: + $output .= "Usage: /invisible \n"; + break; + } break; - } - $this->server->api->setProperty("difficulty", (int) $s); - console("[INFO] Difficulty changed to ".$this->server->difficulty); - loadConfig(true); - break; - case "say": - $s = implode(" ", $params); - if(trim($s) == ""){ - console("[INFO] Usage: /say "); + case "status": + case "lag": + $this->server->debugInfo(true); + $info = $this->server->debugInfo(); + $output .= "TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")\n"; break; - } - $this->server->api->chat->broadcast($s); - break; - case "save-all": - $this->server->save(); - break; - case "help": - case "?": - console("[INFO] /help: Show available commands"); - console("[INFO] /status: Show server TPS and memory usage"); - console("[INFO] /gamemode: Changes default gamemode"); - console("[INFO] /difficulty: Changes difficulty"); - console("[INFO] /invisible: Manages server visibility"); - console("[INFO] /say: Broadcasts mesages"); - console("[INFO] /save-all: Saves pending changes"); - console("[INFO] /stop: Stops the server"); - //console("[INFO] /restart: Restarts the server"); - foreach($this->help as $c => $h){ - console("[INFO] /$c: ".$h); - } - break; - default: - console("[ERROR] Command doesn't exist! Use /help"); - break; - } + case "update-done": + $this->server->api->setProperty("last-update", time()); + break; + case "stop": + $this->loop->stop = true; + $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)){ + $output .= "Usage: /difficulty <0 | 1>\n"; + break; + } + $this->server->api->setProperty("difficulty", (int) $s); + $output .= "Difficulty changed to ".$this->server->difficulty."\n"; + loadConfig(true); + break; + case "say": + $s = implode(" ", $params); + if(trim($s) == ""){ + $output .= "Usage: /say \n"; + break; + } + $this->server->api->chat->broadcast($s); + break; + case "save-all": + $this->server->save(); + break; + case "help": + 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 mesagesv"; + $output .= "/save-all: Saves pending changes\n"; + $output .= "/stop: Stops the server\n"; + foreach($this->help as $c => $h){ + $output .= "/$c: ".$h."\n"; + } + break; + default: + $output .= "Command doesn't exist! Use /help\n"; + break; + } + return $output; } public function alias($alias, $cmd){ @@ -156,15 +157,33 @@ class ConsoleAPI{ $this->help[$cmd] = $help; } - public function run($line = ""){ + public function run($line = "", $issuer = false){ if($line != ""){ $params = explode(" ", $line); $cmd = strtolower(array_shift($params)); - console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); - if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){ - call_user_func($this->cmds[$cmd], $cmd, $params); - }elseif($this->server->api->dhandle("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){ - $this->defaultCommands($cmd, $params); + if($issuer instanceof Player){ + console("[INFO] \"".$issuer->username."\" issued server command: /$cmd ".implode(" ", $params)); + }else{ + console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); + } + if($this->server->api->dhandle("console.command.".$cmd, array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer)) === false + or $this->server->api->dhandle("console.command", array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer)) === false){ + $output = "You don't have permissions\n"; + }else{ + if(isset($this->cmds[$cmd]) and is_callable($this->cmds[$cmd])){ + $output = @call_user_func($this->cmds[$cmd], $cmd, $params, $issuer); + }elseif($this->server->api->dhandle("console.command.unknown", array("cmd" => $cmd, "params" => $params, "issuer" => $issuer)) !== false){ + $output = $this->defaultCommands($cmd, $params, $issuer); + } + } + if($output != "" and ($issuer instanceof Player)){ + $issuer->sendChat($output); + }elseif($output != ""){ + $mes = explode("\n", $output); + foreach($mes as $m){ + console($m); + } + } } } diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index d8fdf546..7a9839d8 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -107,19 +107,20 @@ class PlayerAPI{ } } - public function commandHandler($cmd, $params){ + public function commandHandler($cmd, $params, $issuer){ + $output = ""; switch($cmd){ case "tp": $name = array_shift($params); $target = array_shift($params); if($name == null or $target == null){ - console("[INFO] Usage: /tp "); + $output .= "Usage: /tp \n"; break; } if($this->teleport($name, $target)){ - console("[INFO] \"$name\" teleported to \"$target\""); + $output .= "\"$name\" teleported to \"$target\"\n"; }else{ - console("[ERROR] Couldn't teleport"); + $output .= "Couldn't teleport\n"; } break; case "tppos": @@ -128,13 +129,13 @@ class PlayerAPI{ $x = array_pop($params); $name = implode(" ", $params); if($name == null or $x === null or $y === null or $z === null){ - console("[INFO] Usage: /tp "); + $output .= "Usage: /tp \n"; break; } if($this->tppos($name, $x, $y, $z)){ - console("[INFO] \"$name\" teleported to ($x, $y, $z)"); + $output .= "\"$name\" teleported to ($x, $y, $z)\n"; }else{ - console("[ERROR] Couldn't teleport"); + $output .= "Couldn't teleport\n"; } break; case "kill": @@ -142,7 +143,7 @@ class PlayerAPI{ if($player !== false){ $this->server->api->entity->harm($player->eid, 20, "console", true); }else{ - console("[INFO] Usage: /kill "); + $output .= "Usage: /kill \n"; } break; case "harm": @@ -151,16 +152,17 @@ class PlayerAPI{ if($player !== false){ $this->server->api->entity->harm($player->eid, $dmg, "console", true); }else{ - console("[INFO] Usage: /harm "); + $output .= "Usage: /harm \n"; } break; case "list": - console("[INFO] Player list:"); + $output .= "Player list:\n"; foreach($this->server->clients as $c){ - console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->entity->x, 2).", ".round($c->entity->y, 2).", ".round($c->entity->z, 2).")"); + $output .= $c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->entity->x, 2).", ".round($c->entity->y, 2).", ".round($c->entity->z, 2).")\n"; } break; } + return $output; } public function teleport($name, $target){ diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index f9fc7d09..1f4b167b 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -41,13 +41,14 @@ class TimeAPI{ $this->server->api->console->register("time", "Manages server time", array($this, "commandHandler")); } - public function commandHandler($cmd, $params){ + public function commandHandler($cmd, $params, $issuer){ + $output = ""; switch($cmd){ case "time": $p = strtolower(array_shift($params)); switch($p){ case "check": - console("[INFO] Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")"); + $output .= "Time: ".$this->getDate().", ".$this->getPhase()." (".$this->get(true).")\n"; break; case "add": $this->add(array_shift($params)); @@ -68,11 +69,12 @@ class TimeAPI{ $this->night(); break; default: - console("[INFO] Usage: /time [time]"); + $output .= "Usage: /time [time]\n"; break; } break; } + return $output; } public function night(){ diff --git a/src/classes/Player.php b/src/classes/Player.php index 5874af2a..673b1c5d 100644 --- a/src/classes/Player.php +++ b/src/classes/Player.php @@ -374,12 +374,19 @@ class Player{ }else{ $message = (string) $data; } - $this->dataPacket(MC_CHAT, array( - "message" => str_replace("@username", $this->username, $message), - )); + $this->sendChat($message); break; } } + + public function sendChat($message){ + $mes = explode("\n", $message); + foreach($mes as $m){ + $this->dataPacket(MC_CHAT, array( + "message" => str_replace("@username", $this->username, $m), + )); + } + } public function handle($pid, $data){ if($this->connected === true){ @@ -724,6 +731,16 @@ class Player{ } } break; + case MC_CHAT: + $message = $data["message"]; + if($message{0} === "/"){ //Command + $this->server->api->console->run(substr($message, 1), $this); + }else{ + if($this->server->api->dhandle("player.chat", array("player" => $this, "message" => $message)) !== false){ + $this->server->api->send($this, $message); + } + } + break; default: console("[DEBUG] Unhandled 0x".dechex($data["id"])." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2); break;