Skip to content

Selectors #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CortexPE/Commando/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ public function registerSubCommand(BaseSubCommand $subCommand): void {
public function getSubCommands(): array {
return $this->subCommands;
}
}
}
6 changes: 3 additions & 3 deletions src/CortexPE/Commando/args/BaseArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -97,7 +97,7 @@ public function getSpanLength(): int {

abstract public function getTypeName(): string;

public function getNetworkParameterData():CommandParameter {
public function getNetworkParameterData(): CommandParameter {
return $this->parameterData;
}
}
2 changes: 1 addition & 1 deletion src/CortexPE/Commando/args/IntegerArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/CortexPE/Commando/args/StringEnumArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ public function getEnumName(): string {
public function getEnumValues(): array {
return array_keys(static::VALUES);
}
}
}
72 changes: 72 additions & 0 deletions src/CortexPE/Commando/args/TargetArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
declare(strict_types=1);

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;

class TargetArgument extends BaseArgument {
/** @var SelectorParser */
private $parser;

public function __construct(string $name, bool $optional) {
parent::__construct($name, $optional);

$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 {
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);
}
}
1 change: 0 additions & 1 deletion src/CortexPE/Commando/args/TextArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
namespace CortexPE\Commando\args;


use pocketmine\command\CommandSender;
use pocketmine\network\mcpe\protocol\AvailableCommandsPacket;

class TextArgument extends RawStringArgument {
Expand Down
2 changes: 1 addition & 1 deletion src/CortexPE/Commando/args/Vector3Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ public function parse(string $argument, CommandSender $sender) {
public function getSpanLength(): int {
return 3;
}
}
}
49 changes: 49 additions & 0 deletions src/CortexPE/Commando/args/selector/AllEntitiesSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
declare(strict_types=1);

namespace CortexPE\Commando\args\selector;


use pocketmine\command\CommandSender;
use function array_merge;

class AllEntitiesSelector extends BaseSelector {
public function getChar(): string {
return "e";
}

public function getTargets(CommandSender $sender, array $args): array {
$entities = [];
foreach($sender->getServer()->getLevels() as $level) {
$entities = array_merge($entities, $level->getEntities());
}

return $entities;
}
}
43 changes: 43 additions & 0 deletions src/CortexPE/Commando/args/selector/AllPlayersSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
declare(strict_types=1);

namespace CortexPE\Commando\args\selector;


use pocketmine\command\CommandSender;

class AllPlayersSelector extends BaseSelector {
public function getChar(): string {
return "a";
}

public function getTargets(CommandSender $sender, array $args): array {
return $sender->getServer()->getOnlinePlayers();
}
}
39 changes: 39 additions & 0 deletions src/CortexPE/Commando/args/selector/BaseSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
declare(strict_types=1);

namespace CortexPE\Commando\args\selector;


use pocketmine\command\CommandSender;

abstract class BaseSelector {
abstract public function getChar(): string;

abstract public function getTargets(CommandSender $sender, array $args): array;
}
45 changes: 45 additions & 0 deletions src/CortexPE/Commando/args/selector/ExecutorSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
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];
}
}
53 changes: 53 additions & 0 deletions src/CortexPE/Commando/args/selector/PlayerSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/***
* ___ _
* / __\___ _ __ ___ _ __ ___ __ _ _ __ __| | ___
* / / / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |/ _ \
* / /__| (_) | | | | | | | | | | | (_| | | | | (_| | (_) |
* \____/\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|\___/
*
* Commando - A Command Framework virion for PocketMine-MP
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Written by @CortexPE <https://CortexPE.xyz>
*
*/
declare(strict_types=1);

namespace CortexPE\Commando\args\selector;


use pocketmine\command\CommandSender;
use pocketmine\level\Position;
use pocketmine\Player;

class PlayerSelector extends BaseSelector {
public function getChar(): string {
return "p";
}

public function getTargets(CommandSender $sender, array $args): array {
if($sender instanceof Position) {
return [
$sender->getLevel()->getNearestEntity(
$sender, PHP_INT_MAX, Player::class, true
)
];
}

return [];
}
}
Loading