From ce925cd3824a8b8462278de9621b5907c8b6fee8 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sun, 10 Feb 2013 11:03:16 +0100 Subject: [PATCH] Updated ConsoleAPI::alias(), BlockAPI::fromString() --- src/API/BanAPI.php | 2 +- src/API/BlockAPI.php | 44 ++++++++++++++++++++++++------------------ src/API/ConsoleAPI.php | 20 +++++++++---------- src/API/PlayerAPI.php | 2 +- src/API/TimeAPI.php | 2 +- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 36712a6423..255f74ba72 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -81,7 +81,7 @@ class BanAPI{ return false; } - public function commandHandler($cmd, $params, $issuer){ + public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ case "op": diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 695055a467..e8907c9829 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -38,6 +38,25 @@ define("BLOCK_UPDATE_WEAK", 3); class BlockAPI{ private $server; + public static function fromString($str){ + $b = explode(":", str_replace(" ", "_", trim($str))); + if(!isset($b[1])){ + $meta = 0; + }else{ + $meta = ((int) $b[1]) & 0xFFFF; + } + + if(defined(strtoupper($b[0]))){ + $item = BlockAPI::getItem(constant(strtoupper($b[0])), $meta); + if($item->getID() === AIR and strtoupper($b[0]) !== "AIR"){ + $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); + } + }else{ + $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); + } + return $item; + } + public static function get($id, $meta = 0, $v = false){ $id = (int) $id; if(isset(Block::$class[$id])){ @@ -96,29 +115,16 @@ class BlockAPI{ $this->server->api->console->register("give", "Give items to a player", array($this, "commandHandler")); } - public function commandHandler($cmd, $params){ + public function commandHandler($cmd, $params, $issuer, $alias){ + $output = ""; switch($cmd){ case "give": if(!isset($params[0]) or !isset($params[1])){ - console("[INFO] Usage: /give [amount] [damage]"); + $output .= "Usage: /give [amount] [damage]\n"; break; } $username = $params[0]; - $b = explode(":", $params[1]); - if(!isset($b[1])){ - $meta = 0; - }else{ - $meta = ((int) $b[1]) & 0xFFFF; - } - - if(defined(strtoupper($b[0]))){ - $item = BlockAPI::getItem(constant(strtoupper($b[0])), $meta); - if($item->getID() === 0){ - $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); - } - }else{ - $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); - } + $item = BlockAPI::fromString($params[1]); if(!isset($params[2])){ $amount = 64; @@ -130,9 +136,9 @@ class BlockAPI{ } if(($player = $this->server->api->player->get($username)) !== false){ $this->drop($player->entity->x - 0.5, $player->entity->y, $player->entity->z - 0.5, $item->getID(), $item->getMetadata(), $amount); - console("[INFO] Giving ".$amount." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$username); + $output .= "Giving ".$amount." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$username."\n"; }else{ - console("[INFO] Unknown player"); + $output .= "Unknown player\n"; } break; diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index ef9d0c27f4..64bbdedd8b 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -48,7 +48,7 @@ class ConsoleAPI{ $this->loop->join(); } - public function defaultCommands($cmd, $params, $issuer){ + public function defaultCommands($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ case "crash": //Crashes the server to generate an report @@ -157,27 +157,27 @@ class ConsoleAPI{ $this->help[$cmd] = $help; } - public function run($line = "", $issuer = false){ + public function run($line = "", $issuer = false, $alias = false){ if($line != ""){ $params = explode(" ", $line); $cmd = strtolower(array_shift($params)); if(isset($this->alias[$cmd])){ - $this->run($this->alias[$cmd] . " " .implode(" ", $params), $issuer); + $this->run($this->alias[$cmd] . " " .implode(" ", $params), $issuer, $cmd); return; } if($issuer instanceof Player){ - console("[INFO] \"".$issuer->username."\" issued server command: /$cmd ".implode(" ", $params)); + console("[INFO] \"".$issuer->username."\" issued server command: $alias /$cmd ".implode(" ", $params)); }else{ - console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); + console("[INFO] Issued server command: $alias /$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){ + if($this->server->api->dhandle("console.command.".$cmd, array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer, "alias" => $alias)) === false + or $this->server->api->dhandle("console.command", array("cmd" => $cmd, "parameters" => $params, "issuer" => $issuer, "alias" => $alias)) === 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); + $output = @call_user_func($this->cmds[$cmd], $cmd, $params, $issuer, $alias); + }elseif($this->server->api->dhandle("console.command.unknown", array("cmd" => $cmd, "params" => $params, "issuer" => $issuer, "alias" => $alias)) !== false){ + $output = $this->defaultCommands($cmd, $params, $issuer, $alias); } } if($output != "" and ($issuer instanceof Player)){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 58335cee3c..5e64a39fbe 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -107,7 +107,7 @@ class PlayerAPI{ } } - public function commandHandler($cmd, $params, $issuer){ + public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ case "tp": diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index 1f4b167be1..1b5023f4a4 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -41,7 +41,7 @@ class TimeAPI{ $this->server->api->console->register("time", "Manages server time", array($this, "commandHandler")); } - public function commandHandler($cmd, $params, $issuer){ + public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ case "time":