It runs :O (Added more Permissions, executors and such things)

This commit is contained in:
Shoghi Cervantes
2014-03-26 04:14:53 +01:00
parent b1b8c89227
commit 7ea9e4c862
76 changed files with 3296 additions and 1061 deletions

View File

@@ -0,0 +1,296 @@
<?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/
*
*
*/
/**
* Command handling related classes
*/
namespace PocketMine\Command;
use PocketMine;
use PocketMine\Utils\TextFormat;
abstract class Command{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $nextLabel;
/**
* @var string
*/
private $label;
/**
* @var string[]
*/
private $aliases = array();
/**
* @var string[]
*/
private $activeAliases = array();
/**
* @var CommandMap
*/
private $commandMap = null;
/**
* @var string
*/
protected $description = "";
/**
* @var string
*/
protected $usageMessage;
/**
* @var string
*/
private $permission = null;
/**
* @var string
*/
private $permissionMessage = null;
/**
* @param string $name
* @param string $description
* @param string $usageMessage
* @param string[] $aliases
*/
protected function __construct($name, $description = "", $usageMessage = null, array $aliases = array()){
$this->name = $name;
$this->nextLabel = $name;
$this->label = $name;
$this->description = $description;
$this->usageMessage = $usageMessage === null ? "/" . $name : $usageMessage;
$this->aliases = $aliases;
$this->activeAliases = (array) $aliases;
}
/**
* @param CommandSender $sender
* @param string $commandLabel
* @param string[] $args
*
* @return mixed
*/
public abstract function execute(CommandSender $sender, $commandLabel, array $args);
/**
* @return string
*/
public function getName(){
return $this->name;
}
/**
* @return string
*/
public function getPermission(){
return $this->permission;
}
/**
* @param string|null $permission
*/
public function setPermission($permission){
$this->permission = $permission;
}
/**
* @param CommandSender $target
*
* @return bool
*/
public function testPermission(CommandSender $target){
if($this->testPermissionSilent($target)){
return true;
}
if($this->permissionMessage === null){
$target->sendMessage(TextFormat::RED . "You don't have permissions to use this command.");
}elseif($this->permissionMessage !== ""){
$target->sendMessage(str_replace("<permission>", $this->permission, $this->permissionMessage));
}
return false;
}
/**
* @param CommandSender $target
*
* @return bool
*/
public function testPermissionSilent(CommandSender $target){
if($this->permission === null or $this->permission === ""){
return true;
}
foreach(explode(";", $this->permission) as $permission){
if($target->hasPermission($permission)){
return true;
}
}
return false;
}
/**
* @return string
*/
public function getLabel(){
return $this->label;
}
public function setLabel($name){
$this->nextLabel = $name;
if(!$this->isRegistered()){
$this->label = $name;
return true;
}
return false;
}
/**
* Registers the command into a Command map
*
* @param CommandMap $commandMap
*
* @return bool
*/
public function register(CommandMap $commandMap){
if($this->allowChangesFrom($commandMap)){
$this->commandMap = $commandMap;
return true;
}
return false;
}
/**
* @param CommandMap $commandMap
*
* @return bool
*/
public function unregister(CommandMap $commandMap){
if($this->allowChangesFrom($commandMap)){
$this->commandMap = null;
$this->activeAliases = $this->aliases;
$this->label = $this->nextLabel;
return true;
}
return false;
}
/**
* @param CommandMap $commandMap
*
* @return bool
*/
private function allowChangesFrom(CommandMap $commandMap){
return $this->commandMap === null or $this->commandMap === $commandMap;
}
/**
* @return bool
*/
public function isRegistered(){
return $this->commandMap !== null;
}
/**
* @return string[]
*/
public function getAliases(){
return $this->activeAliases;
}
/**
* @return string
*/
public function getPermissionMessage(){
return $this->permissionMessage;
}
/**
* @return string
*/
public function getDescription(){
return $this->description;
}
/**
* @return string
*/
public function getUsage(){
return $this->usageMessage;
}
/**
* @param string[] $aliases
*/
public function setAliases(array $aliases){
$this->aliases = $aliases;
if(!$this->isRegistered()){
$this->activeAliases = (array) $aliases;
}
}
/**
* @param string $description
*/
public function setDescription($description){
$this->description = $description;
}
/**
* @param string $permissionMessage
*/
public function setPermissionMessage($permissionMessage){
$this->permissionMessage = $permissionMessage;
}
/**
* @param string $usage
*/
public function setUsage($usage){
$this->usageMessage = $usage;
}
/**
* TODO: static::broadcastCommandMessage()
*
* @param CommandSender $source
* @param string $message
* @param bool $sendToSource
*/
public static function broadcastCommandMessage(CommandSender $source, $message, $sendToSource = true){
}
}

View File

@@ -0,0 +1,38 @@
<?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;
use PocketMine;
interface CommandExecutor{
/**
* @param CommandSender $sender
* @param Command $command
* @param string $label
* @param string[] $args
*
* @return boolean
*/
public function onCommand(CommandSender $sender, Command $command, $label, array $args);
}

View File

@@ -0,0 +1,62 @@
<?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;
use PocketMine;
interface CommandMap{
/**
* @param string $fallbackPrefix
* @param Command[] $commands
*/
public function registerAll($fallbackPrefix, array $commands);
/**
* @param string $fallbackPrefix
* @param Command $command
* @param string $label
*/
public function register($fallbackPrefix, Command $command, $label = null);
/**
* @param CommandSender $sender
* @param string $cmdLine
*
* @return boolean
*/
public function dispatch(CommandSender $sender, $cmdLine);
/**
* @return void
*/
public function clearCommands();
/**
* @param string $name
*
* @return Command
*/
public function getCommand($name);
}

View File

@@ -0,0 +1,45 @@
<?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;
use PocketMine;
use PocketMine\Permission\Permissible;
interface CommandSender extends Permissible{
/**
* @param string $message
*/
public function sendMessage($message);
/**
* @return PocketMine\Server
*/
public function getServer();
/**
* @return string
*/
public function getName();
}

View File

@@ -0,0 +1,131 @@
<?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;
use PocketMine;
use PocketMine\Permission\PermissibleBase;
use PocketMine\Permission\PermissionAttachment;
use PocketMine\Plugin\Plugin;
class ConsoleCommandSender implements CommandSender{
private $perm;
public function __construct(){
$this->perm = new PermissibleBase($this);
}
/**
* @param PocketMine\Permission\Permission|string $name
*
* @return bool
*/
public function isPermissionSet($name){
return $this->perm->isPermissionSet($name);
}
/**
* @param PocketMine\Permission\Permission|string $name
*
* @return bool
*/
public function hasPermission($name){
return $this->perm->hasPermission($name);
}
/**
* @param Plugin $plugin
* @param string $name
* @param bool $value
*
* @return PocketMine\Permission\PermissionAttachment
*/
public function addAttachment(Plugin $plugin, $name = null, $value = null){
return $this->perm->addAttachment($plugin, $name, $value);
}
/**
* @param PermissionAttachment $attachment
*
* @return void
*/
public function removeAttachment(PermissionAttachment $attachment){
$this->perm->removeAttachment($attachment);
}
public function recalculatePermissions(){
$this->perm->recalculatePermissions();
}
/**
* @return PocketMine\Permission\PermissionAttachmentInfo[]
*/
public function getEffectivePermissions(){
return $this->perm->getEffectivePermissions();
}
/**
* @return bool
*/
public function isPlayer(){
return false;
}
/**
* @return PocketMine\Server
*/
public function getServer(){
return PocketMine\Server::getInstance();
}
/**
* @param string $message
*/
public function sendMessage($message){
foreach(explode("\n", $message) as $line){
$line = trim($line);
console($line);
}
}
/**
* @return string
*/
public function getName(){
return "CONSOLE";
}
/**
* @return bool
*/
public function isOp(){
return true;
}
/**
* @param bool $value
*/
public function setOp($value){
}
}

View File

@@ -0,0 +1,71 @@
<?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;
use PocketMine;
use PocketMine\Plugin\Plugin;
class PluginCommand extends Command{
/**
* @var Plugin
*/
private $owningPlugin;
/**
* @var CommandExecutor
*/
private $executor;
protected function __construct($name, Plugin $owner){
parent::__construct($name);
$this->owningPlugin = $owner;
$this->executor = $owner;
$this->usageMessage = "";
}
public function execute(CommandSender $sender, $commandLabel, array $args){
if(!$this->owningPlugin->isEnabled()){
return false;
}
if(!$this->testPermission($sender)){
return false;
}
$success = $this->executor->onCommand($sender, $this, $commandLabel, $args);
if(!$success and $this->usageMessage !== ""){
$sender->sendMessage($this->usageMessage);
}
return $success;
}
/**
* @return Plugin
*/
public function getPlugin(){
return $this->owningPlugin;
}
}

View File

@@ -0,0 +1,28 @@
<?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;
use PocketMine;
interface RemoteConsoleCommandSender extends CommandSender{
}

View File

@@ -0,0 +1,153 @@
<?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;
use PocketMine;
use PocketMine\Server;
use PocketMine\Command\Defaults\VersionCommand;
use PocketMine\Command\Defaults\VanillaCommand;
class SimpleCommandMap implements CommandMap{
/**
* @var Command[]
*/
protected $knownCommands = array();
/**
* @var Server
*/
private $server;
public function __construct(Server $server){
$this->server = $server;
$this->setDefaultCommands();
}
private function setDefaultCommands(){
//TODO
$this->register("pocketmine", new VersionCommand("version"));
}
public function registerAll($fallbackPrefix, array $commands){
foreach($commands as $command){
$this->register($fallbackPrefix, $command);
}
}
public function register($fallbackPrefix, Command $command, $label = null){
if($label === null){
$label = $command->getName();
}
$label = strtolower(trim($label));
$fallbackPrefix = strtolower(trim($fallbackPrefix));
$registered = $this->registerAlias($command, false, $fallbackPrefix, $label);
$aliases = $command->getAliases();
foreach($aliases as $index => $alias){
if(!$this->registerAlias($command, true, $fallbackPrefix, $alias)){
unset($aliases[$index]);
}
}
$command->setAliases($aliases);
if(!$registered){
$command->setLabel($fallbackPrefix . ":" . $label);
}
$command->register($this);
return $registered;
}
private function registerAlias(Command $command, $isAlias, $fallbackPrefix, $label){
$this->knownCommands[$fallbackPrefix . ":" . $label] = $command;
if(($command instanceof VanillaCommand or $isAlias) and isset($this->knownCommands[$label])){
return false;
}
if(isset($this->knownCommands[$label]) and $this->knownCommands[$label]->getLabel() === $label){
return false;
}
if(!$isAlias){
$command->setLabel($label);
}
$this->knownCommands[$label] = $command;
return true;
}
public function dispatch(CommandSender $sender, $commandLine){
$args = explode(" ", $commandLine);
if(count($args) === 0){
return false;
}
$sentCommandLabel = strtolower(array_shift($args));
$target = $this->getCommand($sentCommandLabel);
if($target === null){
return false;
}
$target->execute($sender, $sentCommandLabel, $args);
return true;
}
public function clearCommands(){
foreach($this->knownCommands as $command){
$command->unregister($this);
}
$this->knownCommands = array();
$this->setDefaultCommands();
}
public function getCommand($name){
if(isset($this->knownCommands[$name])){
return $this->knownCommands[$name];
}
return null;
}
/**
* @return Command[]
*/
public function getCommands(){
return $this->knownCommands;
}
/**
* @return void
*/
public function registerServerAliases(){
//TODO
}
}

View File

@@ -0,0 +1,68 @@
<?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;
use PocketMine\Command\Command;
use PocketMine\Command\CommandSender;
abstract class VanillaCommand extends Command{
//TODO: increment chunk indexes
const MAX_COORD = 524288;
const MIN_COORD = -524288;
protected function __construct($name, $description = "", $usageMessage = null, array $aliases = array()){
parent::__construct($name, $description, $usageMessage, $aliases);
}
protected function getInteger(CommandSender $sender, $value, $min = self::MIN_COORD, $max = self::MAX_COORD){
$i = (int) $value;
if($i < $min){
$i = $min;
}elseif($i > $max){
$i = $max;
}
return $i;
}
protected function getRelativeDouble($original, CommandSender $sender, $input){
if($input{0} === "~"){
$value = $this->getDouble($sender, substr($input, 1));
return $original + $value;
}
return $this->getDouble($input);
}
protected function getDouble(CommandSender $sender, $value, $min = self::MIN_COORD, $max = self::MAX_COORD){
$i = (double) $value;
if($i < $min){
$i = $min;
}elseif($i > $max){
$i = $max;
}
return $i;
}
}

View File

@@ -0,0 +1,97 @@
<?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;
use PocketMine\Command\CommandSender;
use PocketMine\Utils\TextFormat;
class VersionCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Gets the version of this server including any plugins in use",
"/version [plugin name]",
["ver", "about"]
);
$this->setPermission("pocketmine.command.version");
}
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$output = "This server is running PocketMine-MP version " . PocketMine\Server::getInstance()->getPocketMineVersion() . "" . PocketMine\Server::getInstance()->getCodename() . "」 (Implementing API version " . PocketMine\Server::getInstance()->getApiVersion() . " for Minecraft: PE " . PocketMine\Server::getInstance()->getVersion() . " protocol version " . PocketMine\Network\Protocol\Info::CURRENT_PROTOCOL . ")";
if(\PocketMine\GIT_COMMIT !== str_repeat("00", 20)){
$output .= " [git " . \PocketMine\GIT_COMMIT . "]";
}
$sender->sendMessage($output);
}else{
$pluginName = implode(" ", $args);
$exactPlugin = PocketMine\Server::getInstance()->getPluginManager()->getPlugin($pluginName);
if($exactPlugin instanceof PocketMine\Plugin\Plugin){
$this->describeToSender($exactPlugin, $sender);
return true;
}
$found = false;
$pluginName = strtolower($pluginName);
foreach(PocketMine\Server::getInstance()->getPluginManager()->getPlugins() as $plugin){
if(stripos($plugin->getName(), $pluginName) !== false){
$this->describeToSender($plugin, $sender);
$found = true;
}
}
if(!$found){
$sender->sendMessage("This server is not running any plugin by that name.\nUse /plugins to get a list of plugins.");
}
}
return true;
}
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());
if($desc->getDescription() != null){
$sender->sendMessage($desc->getDescription());
}
if($desc->getWebsite() != null){
$sender->sendMessage("Website: " . $desc->getWebsite());
}
if(count($authors = $desc->getAuthors()) > 0){
if(count($authors) === 1){
$sender->sendMessage("Author: " . implode(", ", $authors));
}else{
$sender->sendMessage("Authors: " . implode(", ", $authors));
}
}
}
}