mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Use an object to represent command entries in plugin manifest
This commit is contained in:
parent
31a176286d
commit
d07517fe8b
@ -38,10 +38,6 @@ use function dirname;
|
||||
use function fclose;
|
||||
use function file_exists;
|
||||
use function fopen;
|
||||
use function gettype;
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
use function is_string;
|
||||
use function mkdir;
|
||||
use function rtrim;
|
||||
use function stream_copy_to_stream;
|
||||
@ -171,47 +167,34 @@ abstract class PluginBase implements Plugin, CommandExecutor{
|
||||
$this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->getDescription()->getFullName())));
|
||||
continue;
|
||||
}
|
||||
if(is_array($data)){ //TODO: error out if it isn't
|
||||
$newCmd = new PluginCommand($key, $this, $this);
|
||||
if(isset($data["description"])){
|
||||
$newCmd->setDescription($data["description"]);
|
||||
}
|
||||
|
||||
if(isset($data["usage"])){
|
||||
$newCmd->setUsage($data["usage"]);
|
||||
}
|
||||
$newCmd = new PluginCommand($key, $this, $this);
|
||||
if(($description = $data->getDescription()) !== null){
|
||||
$newCmd->setDescription($description);
|
||||
}
|
||||
|
||||
if(isset($data["aliases"]) and is_array($data["aliases"])){
|
||||
$aliasList = [];
|
||||
foreach($data["aliases"] as $alias){
|
||||
if(strpos($alias, ":") !== false){
|
||||
$this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->getDescription()->getFullName())));
|
||||
continue;
|
||||
}
|
||||
$aliasList[] = $alias;
|
||||
}
|
||||
if(($usageMessage = $data->getUsageMessage()) !== null){
|
||||
$newCmd->setUsage($usageMessage);
|
||||
}
|
||||
|
||||
$newCmd->setAliases($aliasList);
|
||||
}
|
||||
|
||||
if(isset($data["permission"])){
|
||||
if(is_string($data["permission"])){
|
||||
$newCmd->setPermission($data["permission"]);
|
||||
}else{
|
||||
$this->logger->error("Permission must be a string, " . gettype($data["permission"]) . " given for command $key");
|
||||
continue;
|
||||
}
|
||||
}else{
|
||||
$this->logger->error("No permission set for command $key");
|
||||
$aliasList = [];
|
||||
foreach($data->getAliases() as $alias){
|
||||
if(strpos($alias, ":") !== false){
|
||||
$this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->getDescription()->getFullName())));
|
||||
continue;
|
||||
}
|
||||
|
||||
if(isset($data["permission-message"])){
|
||||
$newCmd->setPermissionMessage($data["permission-message"]);
|
||||
}
|
||||
|
||||
$pluginCmds[] = $newCmd;
|
||||
$aliasList[] = $alias;
|
||||
}
|
||||
|
||||
$newCmd->setAliases($aliasList);
|
||||
|
||||
$newCmd->setPermission($data->getPermission());
|
||||
|
||||
if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !== null){
|
||||
$newCmd->setPermissionMessage($permissionDeniedMessage);
|
||||
}
|
||||
|
||||
$pluginCmds[] = $newCmd;
|
||||
}
|
||||
|
||||
if(count($pluginCmds) > 0){
|
||||
|
@ -29,6 +29,7 @@ use pocketmine\permission\PermissionParserException;
|
||||
use function array_map;
|
||||
use function array_values;
|
||||
use function is_array;
|
||||
use function is_string;
|
||||
use function phpversion;
|
||||
use function preg_match;
|
||||
use function str_replace;
|
||||
@ -70,8 +71,8 @@ class PluginDescription{
|
||||
/** @var string */
|
||||
private $version;
|
||||
/**
|
||||
* @var mixed[][]
|
||||
* @phpstan-var array<string, array<string, mixed>>
|
||||
* @var PluginDescriptionCommandEntry[]
|
||||
* @phpstan-var array<string, PluginDescriptionCommandEntry>
|
||||
*/
|
||||
private $commands = [];
|
||||
/** @var string */
|
||||
@ -123,7 +124,24 @@ class PluginDescription{
|
||||
$this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin["os"] ?? []));
|
||||
|
||||
if(isset($plugin["commands"]) and is_array($plugin["commands"])){
|
||||
$this->commands = $plugin["commands"];
|
||||
foreach($plugin["commands"] as $commandName => $commandData){
|
||||
if(!is_string($commandName)){
|
||||
throw new PluginDescriptionParseException("Invalid Plugin commands, key must be the name of the command");
|
||||
}
|
||||
if(!is_array($commandData)){
|
||||
throw new PluginDescriptionParseException("Command $commandName has invalid properties");
|
||||
}
|
||||
if(!isset($commandData["permission"]) || !is_string($commandData["permission"])){
|
||||
throw new PluginDescriptionParseException("Command $commandName does not have a permission set");
|
||||
}
|
||||
$this->commands[$commandName] = new PluginDescriptionCommandEntry(
|
||||
$commandData["description"] ?? null,
|
||||
$commandData["usage"] ?? null,
|
||||
$commandData["aliases"] ?? [],
|
||||
$commandData["permission"],
|
||||
$commandData["permission-message"] ?? null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($plugin["depend"])){
|
||||
@ -221,8 +239,8 @@ class PluginDescription{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
* @phpstan-return array<string, array<string, mixed>>
|
||||
* @return PluginDescriptionCommandEntry[]
|
||||
* @phpstan-return array<string, PluginDescriptionCommandEntry>
|
||||
*/
|
||||
public function getCommands() : array{
|
||||
return $this->commands;
|
||||
|
53
src/plugin/PluginDescriptionCommandEntry.php
Normal file
53
src/plugin/PluginDescriptionCommandEntry.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?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\plugin;
|
||||
|
||||
final class PluginDescriptionCommandEntry{
|
||||
|
||||
/**
|
||||
* @param string[] $aliases
|
||||
* @phpstan-param list<string> $aliases
|
||||
*/
|
||||
public function __construct(
|
||||
private ?string $description,
|
||||
private ?string $usageMessage,
|
||||
private array $aliases,
|
||||
private string $permission,
|
||||
private ?string $permissionDeniedMessage,
|
||||
){}
|
||||
|
||||
public function getDescription() : ?string{ return $this->description; }
|
||||
|
||||
public function getUsageMessage() : ?string{ return $this->usageMessage; }
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @phpstan-return list<string>
|
||||
*/
|
||||
public function getAliases() : array{ return $this->aliases; }
|
||||
|
||||
public function getPermission() : string{ return $this->permission; }
|
||||
|
||||
public function getPermissionDeniedMessage() : ?string{ return $this->permissionDeniedMessage; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user