From 3daba24fd636ec35e7e52177ef128881dc132575 Mon Sep 17 00:00:00 2001 From: CortexPE Date: Wed, 5 Jun 2019 08:30:57 -0700 Subject: [PATCH 1/2] basic selectors [untested] --- src/CortexPE/Commando/BaseCommand.php | 2 +- src/CortexPE/Commando/args/BaseArgument.php | 6 +- .../Commando/args/IntegerArgument.php | 2 +- .../Commando/args/StringEnumArgument.php | 2 +- src/CortexPE/Commando/args/TargetArgument.php | 64 +++++++++++++++++++ src/CortexPE/Commando/args/TextArgument.php | 1 - .../Commando/args/Vector3Argument.php | 2 +- .../args/selector/AllEntitiesSelector.php | 23 +++++++ .../args/selector/AllPlayersSelector.php | 17 +++++ .../Commando/args/selector/BaseSelector.php | 13 ++++ .../Commando/args/selector/PlayerSelector.php | 27 ++++++++ .../args/selector/RandomPlayerSelector.php | 21 ++++++ .../Commando/args/selector/SelectorParser.php | 47 ++++++++++++++ .../Commando/traits/ArgumentableTrait.php | 6 +- 14 files changed, 222 insertions(+), 11 deletions(-) create mode 100644 src/CortexPE/Commando/args/TargetArgument.php create mode 100644 src/CortexPE/Commando/args/selector/AllEntitiesSelector.php create mode 100644 src/CortexPE/Commando/args/selector/AllPlayersSelector.php create mode 100644 src/CortexPE/Commando/args/selector/BaseSelector.php create mode 100644 src/CortexPE/Commando/args/selector/PlayerSelector.php create mode 100644 src/CortexPE/Commando/args/selector/RandomPlayerSelector.php create mode 100644 src/CortexPE/Commando/args/selector/SelectorParser.php diff --git a/src/CortexPE/Commando/BaseCommand.php b/src/CortexPE/Commando/BaseCommand.php index ad32a35..331735c 100644 --- a/src/CortexPE/Commando/BaseCommand.php +++ b/src/CortexPE/Commando/BaseCommand.php @@ -192,4 +192,4 @@ public function registerSubCommand(BaseSubCommand $subCommand): void { public function getSubCommands(): array { return $this->subCommands; } -} +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/BaseArgument.php b/src/CortexPE/Commando/args/BaseArgument.php index b5015e6..eb068d2 100644 --- a/src/CortexPE/Commando/args/BaseArgument.php +++ b/src/CortexPE/Commando/args/BaseArgument.php @@ -56,8 +56,8 @@ public function __construct(string $name, bool $optional = false) { abstract public function getNetworkType(): int; /** - * @param string $testString - * @param CommandSender $sender + * @param string $testString + * @param CommandSender $sender * * @return bool */ @@ -97,7 +97,7 @@ public function getSpanLength(): int { abstract public function getTypeName(): string; - public function getNetworkParameterData():CommandParameter { + public function getNetworkParameterData(): CommandParameter { return $this->parameterData; } } diff --git a/src/CortexPE/Commando/args/IntegerArgument.php b/src/CortexPE/Commando/args/IntegerArgument.php index 1d5085a..2a60d0c 100644 --- a/src/CortexPE/Commando/args/IntegerArgument.php +++ b/src/CortexPE/Commando/args/IntegerArgument.php @@ -44,7 +44,7 @@ public function getTypeName(): string { } public function canParse(string $testString, CommandSender $sender): bool { - return (bool)preg_match("/^-?(?:\d+)$/", $testString); + return (bool)preg_match("/^-?\d+$/", $testString); } public function parse(string $argument, CommandSender $sender) { diff --git a/src/CortexPE/Commando/args/StringEnumArgument.php b/src/CortexPE/Commando/args/StringEnumArgument.php index a7cc11d..b88dfd8 100644 --- a/src/CortexPE/Commando/args/StringEnumArgument.php +++ b/src/CortexPE/Commando/args/StringEnumArgument.php @@ -73,4 +73,4 @@ public function getEnumName(): string { public function getEnumValues(): array { return array_keys(static::VALUES); } -} +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/TargetArgument.php b/src/CortexPE/Commando/args/TargetArgument.php new file mode 100644 index 0000000..4740f0d --- /dev/null +++ b/src/CortexPE/Commando/args/TargetArgument.php @@ -0,0 +1,64 @@ +. + * + * Written by @CortexPE + * + */ +declare(strict_types=1); + +namespace CortexPE\Commando\args; + + +use CortexPE\Commando\args\selector\SelectorParser; +use pocketmine\command\CommandSender; +use pocketmine\network\mcpe\protocol\AvailableCommandsPacket; +use pocketmine\Server; + +class TargetArgument extends BaseArgument { + /** @var Server */ + private $server; + /** @var SelectorParser */ + private $parser; + + public function __construct(string $name, bool $optional) { + parent::__construct($name, $optional); + $this->server = Server::getInstance(); + } + + public function getNetworkType(): int { + return AvailableCommandsPacket::ARG_TYPE_TARGET; + } + + public function getTypeName(): string { + return "target"; + } + + public function canParse(string $testString, CommandSender $sender): bool { + return $this->parser->isValid($sender, $testString); + } + + public function parse(string $argument, CommandSender $sender) { + return $this->parser->parse($sender, $argument); + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/TextArgument.php b/src/CortexPE/Commando/args/TextArgument.php index 582d35f..e768bc1 100644 --- a/src/CortexPE/Commando/args/TextArgument.php +++ b/src/CortexPE/Commando/args/TextArgument.php @@ -30,7 +30,6 @@ namespace CortexPE\Commando\args; -use pocketmine\command\CommandSender; use pocketmine\network\mcpe\protocol\AvailableCommandsPacket; class TextArgument extends RawStringArgument { diff --git a/src/CortexPE/Commando/args/Vector3Argument.php b/src/CortexPE/Commando/args/Vector3Argument.php index 8c7205a..573c7e1 100644 --- a/src/CortexPE/Commando/args/Vector3Argument.php +++ b/src/CortexPE/Commando/args/Vector3Argument.php @@ -97,4 +97,4 @@ public function parse(string $argument, CommandSender $sender) { public function getSpanLength(): int { return 3; } -} +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php b/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php new file mode 100644 index 0000000..fbe6916 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php @@ -0,0 +1,23 @@ +getServer()->getLevels() as $level) { + $entities = array_merge($entities, $level->getEntities()); + } + + return $entities; + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/AllPlayersSelector.php b/src/CortexPE/Commando/args/selector/AllPlayersSelector.php new file mode 100644 index 0000000..d5b91f4 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/AllPlayersSelector.php @@ -0,0 +1,17 @@ +getServer()->getOnlinePlayers(); + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/BaseSelector.php b/src/CortexPE/Commando/args/selector/BaseSelector.php new file mode 100644 index 0000000..6c66439 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/BaseSelector.php @@ -0,0 +1,13 @@ +getLevel()->getNearestEntity( + $sender, PHP_INT_MAX, Player::class, true + ) + ]; + } + + return []; + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php b/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php new file mode 100644 index 0000000..7a89083 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php @@ -0,0 +1,21 @@ +getServer()->getOnlinePlayers(); + + return [ + $players[array_rand($players)] + ]; + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/SelectorParser.php b/src/CortexPE/Commando/args/selector/SelectorParser.php new file mode 100644 index 0000000..407f018 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/SelectorParser.php @@ -0,0 +1,47 @@ +getChar(){0}); + if(!isset($this->selectors[$c])){ + $this->selectors[$c] = $selector; + } + $this->selRegex = "/(?:@([" . implode("", array_keys($this->selectors)) . "])(?:\[(.+)\])?)/"; + } + + public function parse(CommandSender $sender, string $arg):array { + preg_match_all($this->selRegex, $arg, $matches); + $args = []; + if(!empty($matches[2])){ + foreach(explode(",", $matches[2][0]) as $arg){ + $arg = explode("=", trim($arg)); + if(count($arg) === 2){ + $args[$arg[0]] = $arg[1]; + }else{ + throw new InvalidCommandSyntaxException("Invalid selector syntax"); + } + } + } + return $this->selectors[$matches[1][0]]->getTargets($sender, $args); + } + + public function isValid(CommandSender $sender, string $arg) :bool{ + return (bool)preg_match($this->selRegex, $arg); + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/traits/ArgumentableTrait.php b/src/CortexPE/Commando/traits/ArgumentableTrait.php index 58270e8..3c1dc27 100644 --- a/src/CortexPE/Commando/traits/ArgumentableTrait.php +++ b/src/CortexPE/Commando/traits/ArgumentableTrait.php @@ -33,11 +33,11 @@ use CortexPE\Commando\args\BaseArgument; use CortexPE\Commando\args\TextArgument; use CortexPE\Commando\BaseCommand; -use function array_slice; use CortexPE\Commando\exception\ArgumentOrderException; +use pocketmine\command\CommandSender; +use function array_slice; use function count; use function implode; -use pocketmine\command\CommandSender; use function usort; trait ArgumentableTrait { @@ -181,4 +181,4 @@ public function hasArguments(): bool { public function getArgumentList(): array { return $this->argumentList; } -} +} \ No newline at end of file From 9243e0f029b22ca7cdbd167ad6b83b9739831e1f Mon Sep 17 00:00:00 2001 From: CortexPE Date: Wed, 5 Jun 2019 08:36:40 -0700 Subject: [PATCH 2/2] o --- src/CortexPE/Commando/args/TargetArgument.php | 16 +++++-- .../args/selector/AllEntitiesSelector.php | 26 +++++++++++ .../args/selector/AllPlayersSelector.php | 26 +++++++++++ .../Commando/args/selector/BaseSelector.php | 26 +++++++++++ .../args/selector/ExecutorSelector.php | 45 +++++++++++++++++++ .../Commando/args/selector/PlayerSelector.php | 26 +++++++++++ .../args/selector/RandomPlayerSelector.php | 26 +++++++++++ .../Commando/args/selector/SelectorParser.php | 26 +++++++++++ 8 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/CortexPE/Commando/args/selector/ExecutorSelector.php diff --git a/src/CortexPE/Commando/args/TargetArgument.php b/src/CortexPE/Commando/args/TargetArgument.php index 4740f0d..d22b551 100644 --- a/src/CortexPE/Commando/args/TargetArgument.php +++ b/src/CortexPE/Commando/args/TargetArgument.php @@ -30,20 +30,28 @@ namespace CortexPE\Commando\args; +use CortexPE\Commando\args\selector\AllEntitiesSelector; +use CortexPE\Commando\args\selector\AllPlayersSelector; +use CortexPE\Commando\args\selector\ExecutorSelector; +use CortexPE\Commando\args\selector\PlayerSelector; +use CortexPE\Commando\args\selector\RandomPlayerSelector; use CortexPE\Commando\args\selector\SelectorParser; use pocketmine\command\CommandSender; use pocketmine\network\mcpe\protocol\AvailableCommandsPacket; -use pocketmine\Server; class TargetArgument extends BaseArgument { - /** @var Server */ - private $server; /** @var SelectorParser */ private $parser; public function __construct(string $name, bool $optional) { parent::__construct($name, $optional); - $this->server = Server::getInstance(); + + $this->parser = new SelectorParser(); + $this->parser->registerSelector(new AllEntitiesSelector()); + $this->parser->registerSelector(new AllPlayersSelector()); + $this->parser->registerSelector(new ExecutorSelector()); + $this->parser->registerSelector(new PlayerSelector()); + $this->parser->registerSelector(new RandomPlayerSelector()); } public function getNetworkType(): int { diff --git a/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php b/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php index fbe6916..0edcd71 100644 --- a/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php +++ b/src/CortexPE/Commando/args/selector/AllEntitiesSelector.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector; diff --git a/src/CortexPE/Commando/args/selector/AllPlayersSelector.php b/src/CortexPE/Commando/args/selector/AllPlayersSelector.php index d5b91f4..f05cb04 100644 --- a/src/CortexPE/Commando/args/selector/AllPlayersSelector.php +++ b/src/CortexPE/Commando/args/selector/AllPlayersSelector.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector; diff --git a/src/CortexPE/Commando/args/selector/BaseSelector.php b/src/CortexPE/Commando/args/selector/BaseSelector.php index 6c66439..28a431b 100644 --- a/src/CortexPE/Commando/args/selector/BaseSelector.php +++ b/src/CortexPE/Commando/args/selector/BaseSelector.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector; diff --git a/src/CortexPE/Commando/args/selector/ExecutorSelector.php b/src/CortexPE/Commando/args/selector/ExecutorSelector.php new file mode 100644 index 0000000..0753ef8 --- /dev/null +++ b/src/CortexPE/Commando/args/selector/ExecutorSelector.php @@ -0,0 +1,45 @@ +. + * + * Written by @CortexPE + * + */ +declare(strict_types=1); + +namespace CortexPE\Commando\args\selector; + + +use pocketmine\command\CommandSender; +use pocketmine\level\Position; +use pocketmine\Player; + +class ExecutorSelector extends BaseSelector { + public function getChar(): string { + return "s"; + } + + public function getTargets(CommandSender $sender, array $args): array { + return [$sender]; + } +} \ No newline at end of file diff --git a/src/CortexPE/Commando/args/selector/PlayerSelector.php b/src/CortexPE/Commando/args/selector/PlayerSelector.php index bab4d07..4c63e50 100644 --- a/src/CortexPE/Commando/args/selector/PlayerSelector.php +++ b/src/CortexPE/Commando/args/selector/PlayerSelector.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector; diff --git a/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php b/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php index 7a89083..dbb78b8 100644 --- a/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php +++ b/src/CortexPE/Commando/args/selector/RandomPlayerSelector.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector; diff --git a/src/CortexPE/Commando/args/selector/SelectorParser.php b/src/CortexPE/Commando/args/selector/SelectorParser.php index 407f018..dc2246e 100644 --- a/src/CortexPE/Commando/args/selector/SelectorParser.php +++ b/src/CortexPE/Commando/args/selector/SelectorParser.php @@ -1,5 +1,31 @@ . + * + * Written by @CortexPE + * + */ +declare(strict_types=1); namespace CortexPE\Commando\args\selector;