Added Help command

This commit is contained in:
Shoghi Cervantes 2014-03-26 20:11:53 +01:00
parent 76438e78d2
commit 9fe68003eb
11 changed files with 150 additions and 78 deletions

View File

@ -84,7 +84,7 @@ class CommandReader extends \Thread{
while(true){
if(($line = $this->readLine()) !== ""){
$this->buffer->synchronized(function(\Threaded $buffer, $line){
$buffer[] = $line;
$buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line);
}, $this->buffer, $line);
$lastLine = microtime(true);
}elseif((microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU

View File

@ -72,6 +72,7 @@ use PocketMine\Tile\Tile;
use PocketMine\Utils\Config;
use PocketMine\Utils\TextFormat;
use PocketMine\Utils\Utils;
use PocketMine\Command\CommandSender;
/**
* Main class that handles networking, recovery, and packet sending to the server part

View File

@ -1,59 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine;
use PocketMine\Network\Protocol\Info;
use PocketMine\Plugin\PluginManager;
class PluginAPI extends \stdClass{
private $server;
public function __construct(){
$this->server = Server::getInstance();
$this->server->api->console->register("plugins", "", array($this, "commandHandler"));
$this->server->api->console->register("version", "", array($this, "commandHandler"));
$this->server->api->ban->cmdWhitelist("version");
}
public function commandHandler($cmd, $params, $issuer, $alias){
$output = "";
switch($cmd){
case "plugins":
$output = "Plugins: ";
foreach($this->server->getPluginManager()->getPlugins() as $plugin){
$d = $plugin->getDescription();
$output .= $d->getName() . ": " . $d->getVersion() . ", ";
}
$output = $output === "Plugins: " ? "No plugins installed.\n" : substr($output, 0, -2) . "\n";
break;
}
return $output;
}
public function __destruct(){
}
public function init(){
}
}

View File

@ -212,6 +212,9 @@ namespace PocketMine {
function console($message, $EOL = true, $log = true, $level = 1){
if(!defined("PocketMine\\DEBUG") or \PocketMine\DEBUG >= $level){
$message .= $EOL === true ? PHP_EOL : "";
if($message{0} !== "["){
$message = "[INFO] $message";
}
$time = (\PocketMine\ANSI === true ? Utils\TextFormat::AQUA . date("H:i:s") . Utils\TextFormat::RESET : date("H:i:s")) . " ";
$replaced = Utils\TextFormat::clean(preg_replace('/\x1b\[[0-9;]*m/', "", $time . $message));
if($log === true and (!defined("LOG") or LOG === true)){
@ -219,7 +222,7 @@ namespace PocketMine {
}
if(\PocketMine\ANSI === true){
$add = "";
if(preg_match("/\\[([a-zA-Z0-9]*)\\]/", $message, $matches) > 0){
if(preg_match("/^\\[([a-zA-Z0-9]*)\\]/", $message, $matches) > 0){
switch($matches[1]){
case "ERROR":
case "SEVERE":

View File

@ -26,6 +26,7 @@
namespace PocketMine;
use PocketMine\Block\Block;
use PocketMine\Command\CommandSender;
use PocketMine\Command\ConsoleCommandSender;
use PocketMine\Command\SimpleCommandMap;
use PocketMine\Entity\Entity;
@ -281,6 +282,13 @@ class Server{
return $this->interface;
}
/**
* @return SimpleCommandMap
*/
public function getCommandMap(){
return $this->commandMap;
}
/**
* @param string $variable
* @param string $defaultValue
@ -497,10 +505,32 @@ class Server{
public function checkConsole(){
if(($line = $this->console->getLine()) !== null){
$this->commandMap->dispatch($this->consoleSender, $line);
$this->dispatchCommand($this->consoleSender, $line);
}
}
/**
* Executes a command from a CommandSender
*
* @param CommandSender $sender
* @param string $commandLine
*
* @return bool
*/
public function dispatchCommand(CommandSender $sender, $commandLine){
if($this->commandMap->dispatch($sender, $commandLine)){
return true;
}
if($sender instanceof Player){
$sender->sendMessage("Unknown command. Type \"/help\" for help.");
}else{
$sender->sendMessage("Unknown command. Type \"help\" for help.");
}
return false;
}
/**
* Starts the PocketMine-MP server and starts processing ticks and packets
*/

View File

@ -101,7 +101,7 @@ class ConsoleCommandSender implements CommandSender{
* @param string $message
*/
public function sendMessage($message){
foreach(explode("\n", $message) as $line){
foreach(explode("\n", trim($message)) as $line){
$line = trim($line);
console($line);
}

View File

@ -24,6 +24,8 @@ namespace PocketMine\Command;
use PocketMine\Command\Defaults\PluginsCommand;
use PocketMine\Command\Defaults\VanillaCommand;
use PocketMine\Command\Defaults\VersionCommand;
use PocketMine\Command\Defaults\SeedCommand;
use PocketMine\Command\Defaults\HelpCommand;
use PocketMine\Server;
use PocketMine;
@ -48,6 +50,8 @@ class SimpleCommandMap implements CommandMap{
//TODO
$this->register("pocketmine", new VersionCommand("version"));
$this->register("pocketmine", new PluginsCommand("plugins"));
$this->register("pocketmine", new SeedCommand("seed"));
$this->register("pocketmine", new HelpCommand("help"));
}

View File

@ -0,0 +1,106 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Command\Defaults;
use PocketMine\Command\CommandSender;
use PocketMine\Utils\TextFormat;
use PocketMine\Player;
use PocketMine\Level\Level;
use PocketMine;
use PocketMine\Command\ConsoleCommandSender;
use PocketMine\Server;
use PocketMine\Command\Command;
class HelpCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Shows the help menu",
"/help [pageNumber]\n/help <topic> [pageNumber]",
["?"]
);
$this->setPermission("pocketmine.command.help");
}
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$command = "";
$pageNumber = 1;
}elseif(is_numeric($args[count($args) - 1])){
$pageNumber = (int) array_pop($args);
if($pageNumber <= 0){
$pageNumber = 1;
}
$command = implode(" ", $args);
}else{
$command = implode(" ", $args);
$pageNumber = 1;
}
if($sender instanceof ConsoleCommandSender){
$pageHeight = 10;
}else{
$pageHeight = 5;
}
if($command === ""){
$commands = array();
foreach(Server::getInstance()->getCommandMap()->getCommands() as $command){
if($command->testPermissionSilent($sender)){
$commands[$command->getName()] = $command;
}
}
ksort($commands, SORT_NATURAL | SORT_FLAG_CASE);
$commands = array_chunk($commands, $pageHeight);
$pageNumber = (int) min(count($commands), $pageNumber);
if($pageNumber < 1){
$pageNumber = 1;
}
$message = TextFormat::RED . "-" . TextFormat::RESET . " Showing help page ".$pageNumber." of ".count($commands)." (/help <pageNumber>) " . TextFormat::RED . "-" . TextFormat::RESET . "\n";
if(isset($commands[$pageNumber - 1])){
foreach($commands[$pageNumber - 1] as $command){
$message .= TextFormat::DARK_GREEN . "/" . $command->getName().": ". TextFormat::WHITE. $command->getDescription()."\n";
}
}
$sender->sendMessage($message);
return true;
}else{
if(($command = Server::getInstance()->getCommandMap()->getCommand(strtolower($command))) instanceof Command){
if($command->testPermissionSilent($sender)){
$message = TextFormat::YELLOW . "--------- ". TextFormat::WHITE . " Help: /" .$command->getName() . TextFormat::YELLOW . " ---------\n";
$message .= TextFormat::DARK_GREEN . "Description: " . TextFormat::WHITE . $command->getDescription() . "\n";
$message .= TextFormat::DARK_GREEN . "Usage: " . TextFormat::WHITE . $command->getUsage() . "\n";
$sender->sendMessage($message);
return true;
}
}
$sender->sendMessage(TextFormat::RED . "No help for ".strtolower($command));
return true;
}
}
}

View File

@ -53,7 +53,7 @@ class PluginsCommand extends VanillaCommand{
if(strlen($list) > 0){
$list .= TextFormat::WHITE . ", ";
}
$list .= $plugin->isEnabled() ? TextFormat::GREEN : TextFormat::RED;
$list .= $plugin->isEnabled() ? TextFormat::DARK_GREEN : TextFormat::RED;
$list .= $plugin->getDescription()->getName();
}

View File

@ -52,17 +52,4 @@ class SeedCommand extends VanillaCommand{
return true;
}
private function getPluginList(){
$list = "";
foreach(($plugins = PocketMine\Server::getInstance()->getPluginManager()->getPlugins()) as $plugin){
if(strlen($list) > 0){
$list .= TextFormat::WHITE . ", ";
}
$list .= $plugin->isEnabled() ? TextFormat::GREEN : TextFormat::RED;
$list .= $plugin->getDescription()->getName();
}
return "(" . count($plugins) . "): $list";
}
}

View File

@ -76,7 +76,7 @@ class VersionCommand extends VanillaCommand{
private function describeToSender(PocketMine\Plugin\Plugin $plugin, CommandSender $sender){
$desc = $plugin->getDescription();
$sender->sendMessage(TextFormat::GREEN . $desc->getName() . TextFormat::WHITE . " version " . TextFormat::GREEN . $desc->getVersion());
$sender->sendMessage(TextFormat::DARK_GREEN . $desc->getName() . TextFormat::WHITE . " version " . TextFormat::DARK_GREEN . $desc->getVersion());
if($desc->getDescription() != null){
$sender->sendMessage($desc->getDescription());