Player is instanceof CommandSender

This commit is contained in:
Shoghi Cervantes 2014-03-31 02:53:37 +02:00
parent 5bb909adf1
commit 2b8df410d1
4 changed files with 475 additions and 5 deletions

View File

@ -21,6 +21,7 @@
namespace PocketMine;
use PocketMine\Command\CommandSender;
use PocketMine\Entity\RealHuman;
use PocketMine\Event;
use PocketMine\Item\Item;
@ -61,6 +62,9 @@ use PocketMine\Network\Protocol\UnknownPacket;
use PocketMine\Network\Protocol\UpdateBlockPacket;
use PocketMine\Network\RakNet\Info;
use PocketMine\Network\RakNet\Packet;
use PocketMine\Permission\PermissibleBase;
use PocketMine\Permission\PermissionAttachment;
use PocketMine\Plugin\Plugin;
use PocketMine\PMF\LevelFormat;
use PocketMine\Recipes\Crafting;
use PocketMine\Scheduler\CallbackTask;
@ -80,7 +84,7 @@ use PocketMine\Utils\Utils;
* Class Player
* @package PocketMine
*/
class Player extends RealHuman/* implements CommandSender*/{
class Player extends RealHuman implements CommandSender{
const BROADCAST_CHANNEL_ADMINISTRATIVE = "pocketmine.broadcast.admin";
const BROADCAST_CHANNEL_USERS = "pocketmine.broadcast.user";
@ -160,6 +164,87 @@ class Player extends RealHuman/* implements CommandSender*/{
*/
private $tasks = array();
private $perm = null;
/**
* @return Server
*/
public function getServer(){
return $this->server;
}
/**
* @return bool
*/
public function isOp(){
return $this->server->isOp($this->getName());
}
/**
* @param bool $value
*/
public function setOp($value){
if($value === $this->isOp()){
return;
}
if($value === true){
$this->server->addOp($this->getName());
}else{
$this->server->removeOp($this->getName());
}
$this->perm->recalculatePermissions();
}
/**
* @param Permission\Permission|string $name
*
* @return bool
*/
public function isPermissionSet($name){
return $this->perm->isPermissionSet($name);
}
/**
* @param 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 Permission\PermissionAttachment
*/
public function addAttachment(Plugin $plugin, $name = null, $value = null){
return $this->perm->addAttachment($plugin, $name, $value);
}
/**
* @param PermissionAttachment $attachment
*/
public function removeAttachment(PermissionAttachment $attachment){
$this->perm->removeAttachment($attachment);
}
public function recalculatePermissions(){
$this->perm->recalculatePermissions();
}
/**
* @return Permission\PermissionAttachmentInfo[]
*/
public function getEffectivePermissions(){
return $this->perm->getEffectivePermissions();
}
/**
* @param integer $clientID
* @param string $ip
@ -167,6 +252,7 @@ class Player extends RealHuman/* implements CommandSender*/{
* @param integer $MTU
*/
public function __construct($clientID, $ip, $port, $MTU){
$this->perm = new PermissibleBase($this);
$this->bigCnt = 0;
$this->MTU = $MTU;
$this->server = Server::getInstance();
@ -977,13 +1063,11 @@ class Player extends RealHuman/* implements CommandSender*/{
return;
}
//TODO: whitelist things
if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->iusername)){
if($this->server->isWhitelisted(strtolower($this->getName()))){
$this->close($this->username . " has left the game", "Server is white-listed");
return;
//TODO: ban things
}elseif($this->server->api->ban->isBanned($this->iusername) or $this->server->api->ban->isIPBanned($this->ip)){
}elseif($this->server->getNameBans()->isBanned(strtolower($this->getName())) or $this->server->getIPBans()->isBanned($this->getIP())){
$this->close($this->username . " has left the game", "You are banned");
return;
@ -1970,6 +2054,10 @@ class Player extends RealHuman/* implements CommandSender*/{
$this->connected = false;
$this->level->freeAllChunks($this);
$this->loggedIn = false;
foreach($this->tasks as $task){
$task->cancel();
}
$this->tasks = array();
$this->recoveryQueue = array();
$this->receiveQueue = array();
$this->resendQueue = array();

View File

@ -44,6 +44,7 @@ use PocketMine\Network\Query\QueryHandler;
use PocketMine\Network\Query\QueryPacket;
use PocketMine\Network\ThreadedHandler;
use PocketMine\Network\UPnP\UPnP;
use PocketMine\Permission\BanList;
use PocketMine\Permission\DefaultPermissions;
use PocketMine\Plugin\Plugin;
use PocketMine\Plugin\PluginLoadOrder;
@ -61,6 +62,18 @@ class Server{
/** @var Server */
private static $instance = null;
/** @var BanList */
private $banByName = null;
/** @var BanList */
private $banByIP = null;
/** @var Config */
private $operators = null;
/** @var Config */
private $whitelist = null;
/** @var bool */
private $isRunning = true;
@ -477,6 +490,92 @@ class Server{
}
}
/**
* @return BanList
*/
public function getNameBans(){
return $this->banByName;
}
/**
* @return BanList
*/
public function getIPBans(){
return $this->banByIP;
}
/**
* @param string $name
*/
public function addOp($name){
$this->operators->set(strtolower($name), true);
if(($player = Player::get($name, false, false)) instanceof Player){
$player->recalculatePermissions();
}
}
/**
* @param string $name
*/
public function removeOp($name){
$this->operators->remove(strtolower($name));
if(($player = Player::get($name, false, false)) instanceof Player){
$player->recalculatePermissions();
}
}
/**
* @param string $name
*/
public function addWhitelist($name){
$this->whitelist->set(strtolower($name), true);
}
/**
* @param string $name
*/
public function removeWhitelist($name){
$this->whitelist->remove(strtolower($name));
}
/**
* @param string $name
*
* @return bool
*/
public function isWhitelisted($name){
return !$this->hasWhitelist() or $this->operators->exists($name, true) or $this->whitelist->exists($name, true);
}
/**
* @param string $name
*
* @return bool
*/
public function isOp($name){
return $this->operators->exists($name, true);
}
/**
* @return Config
*/
public function getWhitelisted(){
return $this->whitelist;
}
/**
* @return Config
*/
public function getOPs(){
return $this->operators;
}
public function reloadWhitelist(){
$this->whitelist->reload();
}
/**
* @return Server
*/
@ -501,6 +600,14 @@ class Server{
@mkdir($this->dataPath . "players/", 0777);
@mkdir($this->pluginPath, 0777);
$this->operators = new Config($this->dataPath . "ops.txt", Config::ENUM);
$this->whitelist = new Config($this->dataPath . "whitelist.txt", Config::ENUM);
if(file_exists($this->dataPath . "banned.txt") and !file_exists($this->dataPath . "banned-players.txt")){
@rename($this->dataPath . "banned.txt", $this->dataPath . "banned-players.txt");
}
$this->banByName = new BanList($this->dataPath . "banned-players.txt");
$this->banByIP = new BanList($this->dataPath . "banned-ips.txt");
$this->tickScheduler = new TickScheduler(20);
$this->scheduler = new ServerScheduler();
$this->console = new CommandReader();

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\Permission;
class BanEntry{
public static $format = "Y-m-d H:i:s O";
private $name;
/** @var \DateTime*/
private $creationDate = null;
private $source = "(Unknown)";
/** @var \DateTime */
private $expirationDate = null;
private $reason = "Banned by an operator.";
public function __construct($name){
$this->name = strtolower($name);
$this->creationDate = new \DateTime();
}
public function getName(){
return $this->name;
}
public function getCreated(){
return $this->creationDate;
}
public function setCreated(\DateTime $date){
$this->creationDate = $date;
}
public function getSource(){
return $this->source;
}
public function setSource($source){
$this->source = $source;
}
public function getExpires(){
return $this->expirationDate;
}
public function setExpires(\DateTime $date){
$this->expirationDate = $date;
}
public function hasExpired(){
$now = new \DateTime();
return $this->expirationDate === null ? false : $this->expirationDate < $now;
}
public function getReason(){
return $this->reason;
}
public function setReason($reason){
$this->reason = $reason;
}
public function getString(){
$str = "";
$str .= $this->getName();
$str .= "|";
$str .= $this->getCreated()->format(self::$format);
$str .= "|";
$str .= $this->getSource();
$str .= "|";
$str .= $this->getExpires() === null ? "Forever" : $this->getExpires()->format(self::$format);
$str .= "|";
$str .= $this->getReason();
$str .= "|";
return $str;
}
/**
* @param string $str
*
* @return BanEntry
*/
public static function fromString($str){
if(strlen($str) < 2){
return null;
}else{
$str = explode("|", trim($str));
$entry = new BanEntry(trim(array_shift($str)));
if(count($str) > 0){
$entry->setCreated(\DateTime::createFromFormat(self::$format, array_shift($str)));
if(count($str) > 0){
$entry->setSource(trim(array_shift($str)));
if(count($str) > 0){
$expire = trim(array_shift($str));
if(strtolower($expire) !== "forever" and strlen($expire) > 0){
$entry->setExpires(\DateTime::createFromFormat(self::$format, $expire));
}
if(count($str) > 0){
$entry->setReason(trim(array_shift($str)));
return $entry;
}else{
return $entry;
}
}
}else{
return $entry;
}
}else{
return $entry;
}
}
}
}

View File

@ -0,0 +1,144 @@
<?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\Permission;
use PocketMine\Server;
class BanList{
/** @var string[] */
private $list = array();
/** @var string */
private $file;
/** @var bool */
private $enabled = true;
/**
* @param string $file
*/
public function __construct($file){
$this->file = $file;
}
/**
* @return bool
*/
public function isEnabled(){
return $this->enabled === true;
}
/**
* @param bool $flag
*/
public function setEnabled($flag){
$this->enabled = (bool) $flag;
}
/**
* @return string[]
*/
public function getEntries(){
$this->removeExpired();
return $this->list;
}
/**
* @param string $name
*
* @return bool
*/
public function isBanned($name){
$name = strtolower($name);
if(!$this->isEnabled()){
return false;
}else{
$this->removeExpired();
return isset($this->list[$name]);
}
}
/**
* @param BanEntry $entry
*/
public function add(BanEntry $entry){
$this->list[$entry->getName()] = $entry;
$this->save();
}
/**
* @param string $name
*/
public function remove($name){
$name = strtolower($name);
if(isset($this->list[$name])){
unset($this->list[$name]);
$this->save();
}
}
public function removeExpired(){
foreach($this->list as $name => $entry){
if($entry->hasExpired()){
unset($this->list[$name]);
}
}
}
public function load(){
$this->list = array();
$fp = @fopen($this->file, "r");
if(is_resource($fp)){
while(($line = fgets($fp)) !== false){
if($line{0} !== "#"){
$entry = BanEntry::fromString($line);
if($entry instanceof BanEntry){
$this->list[$entry->getName()] = $entry;
}
}
}
fclose($fp);
}else{
console("[ERROR] Could not load ban list");
}
}
public function save($flag = true){
$this->removeExpired();
$fp = @fopen($this->file, "w");
if(is_resource($fp)){
if($flag === true){
fwrite($fp, "# Updated ".strftime("%x %H:%M", time())." by ".Server::getInstance()->getName() . " ".Server::getInstance()->getPocketMineVersion()."\n");
fwrite($fp, "# victim name | ban date | banned by | banned until | reason\n\n");
}
foreach($this->list as $entry){
fwrite($fp, $entry->getString() . "\n");
}
fclose($fp);
}else{
console("[ERROR] Could not save ban list");
}
}
}