85 protected array $knownCommands = [];
87 public function __construct(
private Server $server){
88 $this->setDefaultCommands();
91 private function setDefaultCommands() :
void{
137 public function registerAll(
string $fallbackPrefix, array $commands) : void{
138 foreach($commands as $command){
139 $this->
register($fallbackPrefix, $command);
143 public function register(
string $fallbackPrefix,
Command $command, ?
string $label =
null) :
bool{
144 if(count($command->getPermissions()) === 0){
145 throw new \InvalidArgumentException(
"Commands must have a permission set");
149 $label = $command->getLabel();
151 $label = trim($label);
152 $fallbackPrefix = strtolower(trim($fallbackPrefix));
154 $registered = $this->registerAlias($command,
false, $fallbackPrefix, $label);
156 $aliases = $command->getAliases();
157 foreach($aliases as $index => $alias){
158 if(!$this->registerAlias($command,
true, $fallbackPrefix, $alias)){
159 unset($aliases[$index]);
162 $command->setAliases($aliases);
165 $command->setLabel($fallbackPrefix .
":" . $label);
168 $command->register($this);
173 public function unregister(Command $command) : bool{
174 foreach($this->knownCommands as $lbl => $cmd){
175 if($cmd === $command){
176 unset($this->knownCommands[$lbl]);
180 $command->unregister($this);
185 private function registerAlias(Command $command,
bool $isAlias,
string $fallbackPrefix,
string $label) : bool{
186 $this->knownCommands[$fallbackPrefix .
":" . $label] = $command;
187 if(($command instanceof VanillaCommand || $isAlias) && isset($this->knownCommands[$label])){
191 if(isset($this->knownCommands[$label]) && $this->knownCommands[$label]->getLabel() === $label){
196 $command->setLabel($label);
199 $this->knownCommands[$label] = $command;
204 public function dispatch(CommandSender $sender,
string $commandLine) : bool{
205 $args = CommandStringHelper::parseQuoteAware($commandLine);
207 $sentCommandLabel = array_shift($args);
208 if($sentCommandLabel !==
null && ($target = $this->getCommand($sentCommandLabel)) !==
null){
209 $timings = Timings::getCommandDispatchTimings($target->getLabel());
210 $timings->startTiming();
213 if($target->testPermission($sender)){
214 $target->execute($sender, $sentCommandLabel, $args);
216 }
catch(InvalidCommandSyntaxException $e){
217 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
219 $timings->stopTiming();
224 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel ??
"",
"/help")->prefix(TextFormat::RED));
228 public function clearCommands() : void{
229 foreach($this->knownCommands as $command){
230 $command->unregister($this);
232 $this->knownCommands = [];
233 $this->setDefaultCommands();
236 public function getCommand(
string $name) : ?Command{
237 return $this->knownCommands[$name] ?? null;
244 return $this->knownCommands;
247 public function registerServerAliases() : void{
248 $values = $this->
server->getCommandAliases();
250 foreach($values as $alias => $commandStrings){
251 if(str_contains($alias,
":")){
252 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_illegal($alias)));
260 foreach($commandStrings as $commandString){
261 $args = CommandStringHelper::parseQuoteAware($commandString);
262 $commandName = array_shift($args) ??
"";
263 $command = $this->getCommand($commandName);
265 if($command ===
null){
266 $bad[] = $commandString;
267 }elseif(strcasecmp($commandName, $alias) === 0){
268 $recursive[] = $commandString;
270 $targets[] = $commandString;
274 if(count($recursive) > 0){
275 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_recursive($alias, implode(
", ", $recursive))));
280 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_notFound($alias, implode(
", ", $bad))));
285 $lowerAlias = strtolower($alias);
286 if(count($targets) > 0){
287 $this->knownCommands[$lowerAlias] =
new FormattedCommandAlias($lowerAlias, $targets);
289 unset($this->knownCommands[$lowerAlias]);