added PermissibleDelegateTrait to cut down boilerplate in Player and ConsoleCommandSender

This commit is contained in:
Dylan K. Taylor 2019-06-17 16:10:13 +01:00
parent 20f092a685
commit 3c677bd3ec
3 changed files with 88 additions and 109 deletions

View File

@ -101,8 +101,7 @@ use pocketmine\network\mcpe\protocol\types\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\EntityMetadataProperties;
use pocketmine\network\mcpe\protocol\types\PlayerMetadataFlags;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissionAttachment;
use pocketmine\permission\PermissionAttachmentInfo;
use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\permission\PermissionManager;
use pocketmine\plugin\Plugin;
use pocketmine\timings\Timings;
@ -144,6 +143,9 @@ use const PHP_INT_MAX;
* Main class that handles networking, recovery, and packet sending to the server part
*/
class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, IPlayer{
use PermissibleDelegateTrait {
recalculatePermissions as private delegateRecalculatePermissions;
}
/**
* Checks a supplied username and checks it is valid.
@ -253,9 +255,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
/** @var bool */
protected $flying = false;
/** @var PermissibleBase */
private $perm = null;
/** @var int|null */
protected $lineHeight = null;
/** @var string */
@ -690,47 +689,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$this->networkSession->syncAdventureSettings($this);
}
/**
* @param permission\Permission|string $name
*
* @return bool
*/
public function isPermissionSet($name) : bool{
return $this->perm->isPermissionSet($name);
}
/**
* @param permission\Permission|string $name
*
* @return bool
*
* @throws \InvalidStateException if the player is closed
*/
public function hasPermission($name) : bool{
if($this->closed){
throw new \InvalidStateException("Trying to get permissions of closed player");
}
return $this->perm->hasPermission($name);
}
/**
* @param Plugin $plugin
* @param string $name
* @param bool $value
*
* @return PermissionAttachment
*/
public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{
return $this->perm->addAttachment($plugin, $name, $value);
}
/**
* @param PermissionAttachment $attachment
*/
public function removeAttachment(PermissionAttachment $attachment) : void{
$this->perm->removeAttachment($attachment);
}
public function recalculatePermissions() : void{
$permManager = PermissionManager::getInstance();
$permManager->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this);
@ -740,7 +698,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
return;
}
$this->perm->recalculatePermissions();
$this->delegateRecalculatePermissions();
if($this->spawned){
if($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)){
@ -754,13 +712,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
}
}
/**
* @return PermissionAttachmentInfo[]
*/
public function getEffectivePermissions() : array{
return $this->perm->getEffectivePermissions();
}
/**
* @return bool
*/

View File

@ -25,18 +25,14 @@ namespace pocketmine\command;
use pocketmine\lang\TextContainer;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\Permission;
use pocketmine\permission\PermissionAttachment;
use pocketmine\permission\PermissionAttachmentInfo;
use pocketmine\plugin\Plugin;
use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\Server;
use function explode;
use function trim;
use const PHP_INT_MAX;
class ConsoleCommandSender implements CommandSender{
private $perm;
use PermissibleDelegateTrait;
/** @var int|null */
protected $lineHeight = null;
@ -45,55 +41,6 @@ class ConsoleCommandSender implements CommandSender{
$this->perm = new PermissibleBase($this);
}
/**
* @param Permission|string $name
*
* @return bool
*/
public function isPermissionSet($name) : bool{
return $this->perm->isPermissionSet($name);
}
/**
* @param Permission|string $name
*
* @return bool
*/
public function hasPermission($name) : bool{
return $this->perm->hasPermission($name);
}
/**
* @param Plugin $plugin
* @param string $name
* @param bool $value
*
* @return PermissionAttachment
*/
public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{
return $this->perm->addAttachment($plugin, $name, $value);
}
/**
* @param PermissionAttachment $attachment
*
* @return void
*/
public function removeAttachment(PermissionAttachment $attachment) : void{
$this->perm->removeAttachment($attachment);
}
public function recalculatePermissions() : void{
$this->perm->recalculatePermissions();
}
/**
* @return PermissionAttachmentInfo[]
*/
public function getEffectivePermissions() : array{
return $this->perm->getEffectivePermissions();
}
/**
* @return Server
*/

View File

@ -0,0 +1,81 @@
<?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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\permission;
use pocketmine\plugin\Plugin;
trait PermissibleDelegateTrait{
/** @var PermissibleBase */
private $perm = null;
/**
* @param Permission|string $name
*
* @return bool
*/
public function isPermissionSet($name) : bool{
return $this->perm->isPermissionSet($name);
}
/**
* @param Permission|string $name
*
* @return bool
*/
public function hasPermission($name) : bool{
return $this->perm->hasPermission($name);
}
/**
* @param Plugin $plugin
* @param string $name
* @param bool $value
*
* @return PermissionAttachment
*/
public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{
return $this->perm->addAttachment($plugin, $name, $value);
}
/**
* @param PermissionAttachment $attachment
*/
public function removeAttachment(PermissionAttachment $attachment) : void{
$this->perm->removeAttachment($attachment);
}
public function recalculatePermissions() : void{
$this->perm->recalculatePermissions();
}
/**
* @return PermissionAttachmentInfo[]
*/
public function getEffectivePermissions() : array{
return $this->perm->getEffectivePermissions();
}
}