Updated ConsoleAPI::alias(), BlockAPI::fromString()

This commit is contained in:
Shoghi Cervantes Pueyo 2013-02-10 11:03:16 +01:00
parent fbc1873e06
commit ce925cd382
5 changed files with 38 additions and 32 deletions

View File

@ -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":

View File

@ -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 <username> <item> [amount] [damage]");
$output .= "Usage: /give <username> <item> [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;

View File

@ -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)){

View File

@ -107,7 +107,7 @@ class PlayerAPI{
}
}
public function commandHandler($cmd, $params, $issuer){
public function commandHandler($cmd, $params, $issuer, $alias){
$output = "";
switch($cmd){
case "tp":

View File

@ -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":