Skip to content

Add the 'target' type of argument for player selections #7

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

Closed
wants to merge 5 commits into from
Closed
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
64 changes: 64 additions & 0 deletions src/CortexPE/Commando/args/TargetArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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 pocketmine\command\CommandSender;
use pocketmine\network\mcpe\protocol\AvailableCommandsPacket;
use pocketmine\Player;
use pocketmine\Server;

class TargetArgument extends BaseArgument {
/** @var Server */
private $server;

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 Player::isValidUserName($testString);
}

public function parse(string $argument, CommandSender $sender) {
// TODO: handle @a @e @p @r @s @c @v
$player = $this->server->getPlayer($argument) ?? $this->server->getOfflinePlayer($argument);
return $player->getName();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also why does it return the name instead of Player or OfflinePlayer? Wouldn't that be more versatile?

And we don't need to call Server->getPlayer() because Server->getOfflinePlayer() already returns a Player if it's available:
https://github.com/pmmp/PocketMine-MP/blob/stable/src/pocketmine/Server.php#L778-L792

}
}