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(){ public function sendCommandData(){
$data = new \stdClass(); $data = [];
$count = 0;
foreach($this->server->getCommandMap()->getCommands() as $command){ foreach($this->server->getCommandMap()->getCommands() as $command){
if(($cmdData = $command->generateCustomCommandData($this)) !== null){ if(count($cmdData = $command->generateCustomCommandData($this)) > 0){
++$count; $data[$command->getName()]["versions"][0] = $cmdData;
$data->{$command->getName()}->versions[0] = $cmdData;
} }
} }
if($count > 0){ if(count($data) > 0){
//TODO: structure checking //TODO: structure checking
$pk = new AvailableCommandsPacket(); $pk = new AvailableCommandsPacket();
$pk->commands = json_encode($data); $pk->commands = json_encode($data);

View File

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