Use assoc instead of object for command data, fix data modifications affecting all commands

how did I do manage to do somthing this stupid -_- smh what an idiot
Lucky permission is a root node, or the whole commands system would've been compromised. Epic fail.
This commit is contained in:
Dylan K. Taylor 2017-03-14 11:39:59 +00:00
parent 3138e02acb
commit d26713ab59
2 changed files with 32 additions and 28 deletions

View File

@ -534,16 +534,14 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
public function sendCommandData(){
$data = new \stdClass();
$count = 0;
$data = [];
foreach($this->server->getCommandMap()->getCommands() as $command){
if(($cmdData = $command->generateCustomCommandData($this)) !== null){
++$count;
$data->{$command->getName()}->versions[0] = $cmdData;
if(count($cmdData = $command->generateCustomCommandData($this)) > 0){
$data[$command->getName()]["versions"][0] = $cmdData;
}
}
if($count > 0){
if(count($data) > 0){
//TODO: structure checking
$pk = new AvailableCommandsPacket();
$pk->commands = json_encode($data);

View File

@ -32,12 +32,12 @@ use pocketmine\Server;
use pocketmine\utils\TextFormat;
abstract class Command{
/** @var \stdClass */
/** @var array */
private static $defaultDataTemplate = null;
/** @var string */
private $name;
/** @var \stdClass */
/** @var array */
protected $commandData = null;
/** @var string */
@ -89,9 +89,9 @@ abstract class Command{
/**
* Returns an \stdClass containing command data
*
* @return \stdClass
* @return array
*/
public function getDefaultCommandData() : \stdClass{
public function getDefaultCommandData() : array{
return $this->commandData;
}
@ -101,25 +101,28 @@ abstract class Command{
*
* @param Player $player
*
* @return \stdClass|null
* @return array
*/
public function generateCustomCommandData(Player $player){
//TODO: fix command permission filtering on join
/*if(!$this->testPermissionSilent($player)){
return null;
}*/
$customData = clone $this->commandData;
$customData->aliases = $this->getAliases();
/*foreach($customData->overloads as &$overload){
if(isset($overload->pocketminePermission) and !$player->hasPermission($overload->pocketminePermission)){
unset($overload);
$customData = $this->commandData;
$customData["aliases"] = $this->getAliases();
/*foreach($customData["overloads"] as $overloadName => $overload){
if(isset($overload["pocketminePermission"]) and !$player->hasPermission($overload["pocketminePermission"])){
unset($customData["overloads"][$overloadName]);
}
}*/
return $customData;
}
public function getOverloads(): \stdClass{
return $this->commandData->overloads;
/**
* @return array
*/
public function getOverloads(): array{
return $this->commandData["overloads"];
}
/**
@ -142,7 +145,7 @@ abstract class Command{
* @return string
*/
public function getPermission(){
return $this->commandData->pocketminePermission ?? null;
return $this->commandData["pocketminePermission"] ?? null;
}
@ -151,9 +154,9 @@ abstract class Command{
*/
public function setPermission($permission){
if($permission !== null){
$this->commandData->pocketminePermission = $permission;
$this->commandData["pocketminePermission"] = $permission;
}else{
unset($this->commandData->pocketminePermission);
unset($this->commandData["pocketminePermission"]);
}
}
@ -239,7 +242,7 @@ abstract class Command{
public function unregister(CommandMap $commandMap){
if($this->allowChangesFrom($commandMap)){
$this->commandMap = null;
$this->activeAliases = $this->commandData->aliases;
$this->activeAliases = $this->commandData["aliases"];
$this->label = $this->nextLabel;
return true;
@ -282,7 +285,7 @@ abstract class Command{
* @return string
*/
public function getDescription(){
return $this->commandData->description;
return $this->commandData["description"];
}
/**
@ -296,7 +299,7 @@ abstract class Command{
* @param string[] $aliases
*/
public function setAliases(array $aliases){
$this->commandData->aliases = $aliases;
$this->commandData["aliases"] = $aliases;
if(!$this->isRegistered()){
$this->activeAliases = (array) $aliases;
}
@ -306,7 +309,7 @@ abstract class Command{
* @param string $description
*/
public function setDescription($description){
$this->commandData->description = $description;
$this->commandData["description"] = $description;
}
/**
@ -323,11 +326,14 @@ abstract class Command{
$this->usageMessage = $usage;
}
public static final function generateDefaultData() : \stdClass{
/**
* @return array
*/
public static final function generateDefaultData() : array{
if(self::$defaultDataTemplate === null){
self::$defaultDataTemplate = json_decode(file_get_contents(Server::getInstance()->getFilePath() . "src/pocketmine/resources/command_default.json"));
self::$defaultDataTemplate = json_decode(file_get_contents(Server::getInstance()->getFilePath() . "src/pocketmine/resources/command_default.json"), true);
}
return clone self::$defaultDataTemplate;
return self::$defaultDataTemplate;
}
/**