Selector aliases & @world & /spawnpoint

This commit is contained in:
Shoghi Cervantes Pueyo 2013-05-16 18:02:24 +02:00
parent ad4959f8c0
commit 66322d7719
3 changed files with 50 additions and 15 deletions

View File

@ -205,7 +205,7 @@ class ConsoleAPI{
console("[DEBUG] \x1b[33m*".$issuer."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); console("[DEBUG] \x1b[33m*".$issuer."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2);
} }
if(preg_match_all('#@([@A-Za-z_]{1,})#', $params, $matches, PREG_OFFSET_CAPTURE) > 0){ if(preg_match_all('#@([@a-z]{1,})#', $params, $matches, PREG_OFFSET_CAPTURE) > 0){
$offsetshift = 0; $offsetshift = 0;
foreach($matches[1] as $selector){ foreach($matches[1] as $selector){
if($selector[0]{0} === "@"){ //Escape! if($selector[0]{0} === "@"){ //Escape!
@ -214,18 +214,27 @@ class ConsoleAPI{
continue; continue;
} }
switch(strtolower($selector[0])){ switch(strtolower($selector[0])){
case "u":
case "player": case "player":
case "username": case "username":
$p = ($issuer instanceof Player) ? $issuer->username:$issuer; $p = ($issuer instanceof Player) ? $issuer->username:$issuer;
$params = substr_replace($params, $p, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); $params = substr_replace($params, $p, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1);
$offsetshift -= strlen($selector[0]) - strlen($p) + 1; $offsetshift -= strlen($selector[0]) - strlen($p) + 1;
break; break;
case "w":
case "world":
$p = ($issuer instanceof Player) ? $issuer->level->getName():$this->server->api->level->getDefault()->getName();
$params = substr_replace($params, $p, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1);
$offsetshift -= strlen($selector[0]) - strlen($p) + 1;
break;
case "a":
case "all": case "all":
$output = ""; $output = "";
foreach($this->server->api->player->getAll() as $p){ foreach($this->server->api->player->getAll() as $p){
$output .= $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); $output .= $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias);
} }
return $output; return $output;
case "r":
case "random": case "random":
$l = array(); $l = array();
foreach($this->server->api->player->getAll() as $p){ foreach($this->server->api->player->getAll() as $p){

View File

@ -37,7 +37,8 @@ class PlayerAPI{
$this->server->api->console->register("list", "", array($this, "commandHandler")); $this->server->api->console->register("list", "", array($this, "commandHandler"));
$this->server->api->console->register("kill", "<player>", array($this, "commandHandler")); $this->server->api->console->register("kill", "<player>", array($this, "commandHandler"));
$this->server->api->console->register("gamemode", "<mode> [player]", array($this, "commandHandler")); $this->server->api->console->register("gamemode", "<mode> [player]", array($this, "commandHandler"));
$this->server->api->console->register("tp", "[target player] <destination player> OR [target player] <x> <y> <z>", array($this, "commandHandler")); $this->server->api->console->register("tp", "[target player] <destination player> OR /tp [target player] <x> <y> <z>", array($this, "commandHandler"));
$this->server->api->console->register("spawnpoint", "[player] [x] [y] [z]", array($this, "commandHandler"));
$this->server->api->console->register("spawn", "[world]", array($this, "commandHandler")); $this->server->api->console->register("spawn", "[world]", array($this, "commandHandler"));
$this->server->api->console->register("lag", "", array($this, "commandHandler")); $this->server->api->console->register("lag", "", array($this, "commandHandler"));
$this->server->api->console->alias("suicide", "kill"); $this->server->api->console->alias("suicide", "kill");
@ -115,12 +116,39 @@ class PlayerAPI{
public function commandHandler($cmd, $params, $issuer, $alias){ public function commandHandler($cmd, $params, $issuer, $alias){
$output = ""; $output = "";
switch($cmd){ switch($cmd){
case "spawn": case "spawnpoint":
if(count($params) > 1){ if(!($issuer instanceof Player)){
$player = $this->server->api->player->get(array_pop($params)); $output .= "Please run this command in-game.\n";
}else{ break;
$player = $issuer;
} }
if(count($params) === 1 or count($params) === 4){
$target = $this->server->api->player->get(array_shift($params));
}else{
$target = $issuer;
}
if(!($target instanceof Player)){
$output .= "That player cannot be found.\n";
break;
}
if(count($params) === 3){
$spawn = new Position(floatval(array_shift($params)), floatval(array_shift($params)), floatval(array_shift($params)), $issuer->level);
}else{
$spawn = new Position($issuer->x, $issuer->y, $issuer->z, $issuer->level);
}
$target->setSpawn($spawn);
$output .= "Spawnpoint set correctly!\n";
break;
case "spawn":
if(!($issuer instanceof Player)){
$output .= "Please run this command in-game.\n";
break;
}
if(isset($params[0])){ if(isset($params[0])){
$lv = $this->server->api->level->get(trim(implode(" ",$params))); $lv = $this->server->api->level->get(trim(implode(" ",$params)));
if($lv === false){ if($lv === false){
@ -132,11 +160,7 @@ class PlayerAPI{
$spawn = $this->server->api->level->getDefault()->getSpawn(); $spawn = $this->server->api->level->getDefault()->getSpawn();
} }
if(!($player instanceof Player)){ $issuer->teleport($spawn);
$output .= "Couldn't respawn.\n";
break;
}
$player->teleport($spawn);
break; break;
case "lag": case "lag":
if(!($issuer instanceof Player)){ if(!($issuer instanceof Player)){

View File

@ -94,9 +94,11 @@ class Player{
console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2);
} }
public function setSpawn(Vector3 $pos, $level){ public function setSpawn(Vector3 $pos){
if(($level = $this->server->api->level->get($level)) === false){ if(!($pos instanceof Level)){
$level = $this->server->api->level->getDefault(); $level = $this->level;
}else{
$level = $pos->level;
} }
$this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level);
$this->dataPacket(MC_SET_SPAWN_POSITION, array( $this->dataPacket(MC_SET_SPAWN_POSITION, array(