setDefaultCommands(); } private function setDefaultCommands() : void{ $this->registerAll("pocketmine", [ new BanCommand("ban"), new BanIpCommand("ban-ip"), new BanListCommand("banlist"), new ClearCommand("clear"), new DefaultGamemodeCommand("defaultgamemode"), new DeopCommand("deop"), new DifficultyCommand("difficulty"), new DumpMemoryCommand("dumpmemory"), new EffectCommand("effect"), new EnchantCommand("enchant"), new GamemodeCommand("gamemode"), new GarbageCollectorCommand("gc"), new GiveCommand("give"), new HelpCommand("help"), new KickCommand("kick"), new KillCommand("kill"), new ListCommand("list"), new MeCommand("me"), new OpCommand("op"), new PardonCommand("pardon"), new PardonIpCommand("pardon-ip"), new ParticleCommand("particle"), new PluginsCommand("plugins"), new SaveCommand("save-all"), new SaveOffCommand("save-off"), new SaveOnCommand("save-on"), new SayCommand("say"), new SeedCommand("seed"), new SetWorldSpawnCommand("setworldspawn"), new SpawnpointCommand("spawnpoint"), new StatusCommand("status"), new StopCommand("stop"), new TeleportCommand("tp"), new TellCommand("tell"), new TimeCommand("time"), new TimingsCommand("timings"), new TitleCommand("title"), new TransferServerCommand("transferserver"), new VersionCommand("version"), new WhitelistCommand("whitelist") ]); } public function registerAll(string $fallbackPrefix, array $commands) : void{ foreach($commands as $command){ $this->register($fallbackPrefix, $command); } } public function register(string $fallbackPrefix, Command $command, ?string $label = null) : bool{ if($label === null){ $label = $command->getName(); } $label = trim($label); $fallbackPrefix = strtolower(trim($fallbackPrefix)); $registered = $this->registerAlias($command, false, $fallbackPrefix, $label); $aliases = $command->getAliases(); foreach($aliases as $index => $alias){ if(!$this->registerAlias($command, true, $fallbackPrefix, $alias)){ unset($aliases[$index]); } } $command->setAliases($aliases); if(!$registered){ $command->setLabel($fallbackPrefix . ":" . $label); } $command->register($this); return $registered; } public function unregister(Command $command) : bool{ foreach($this->knownCommands as $lbl => $cmd){ if($cmd === $command){ unset($this->knownCommands[$lbl]); } } $command->unregister($this); return true; } private function registerAlias(Command $command, bool $isAlias, string $fallbackPrefix, string $label) : bool{ $this->knownCommands[$fallbackPrefix . ":" . $label] = $command; if(($command instanceof VanillaCommand || $isAlias) && isset($this->knownCommands[$label])){ return false; } if(isset($this->knownCommands[$label]) && $this->knownCommands[$label]->getLabel() === $label){ return false; } if(!$isAlias){ $command->setLabel($label); } $this->knownCommands[$label] = $command; return true; } public function dispatch(CommandSender $sender, string $commandLine) : bool{ $args = CommandStringHelper::parseQuoteAware($commandLine); $sentCommandLabel = array_shift($args); if($sentCommandLabel !== null && ($target = $this->getCommand($sentCommandLabel)) !== null){ $target->timings->startTiming(); try{ $target->execute($sender, $sentCommandLabel, $args); }catch(InvalidCommandSyntaxException $e){ $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage()))); }finally{ $target->timings->stopTiming(); } return true; } $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel ?? "", "/help")->prefix(TextFormat::RED)); return false; } public function clearCommands() : void{ foreach($this->knownCommands as $command){ $command->unregister($this); } $this->knownCommands = []; $this->setDefaultCommands(); } public function getCommand(string $name) : ?Command{ return $this->knownCommands[$name] ?? null; } /** * @return Command[] */ public function getCommands() : array{ return $this->knownCommands; } public function registerServerAliases() : void{ $values = $this->server->getCommandAliases(); foreach($values as $alias => $commandStrings){ if(strpos($alias, ":") !== false){ $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_illegal($alias))); continue; } $targets = []; $bad = []; $recursive = []; foreach($commandStrings as $commandString){ $args = CommandStringHelper::parseQuoteAware($commandString); $commandName = array_shift($args) ?? ""; $command = $this->getCommand($commandName); if($command === null){ $bad[] = $commandString; }elseif(strcasecmp($commandName, $alias) === 0){ $recursive[] = $commandString; }else{ $targets[] = $commandString; } } if(count($recursive) > 0){ $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_recursive($alias, implode(", ", $recursive)))); continue; } if(count($bad) > 0){ $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_notFound($alias, implode(", ", $bad)))); continue; } //These registered commands have absolute priority if(count($targets) > 0){ $this->knownCommands[strtolower($alias)] = new FormattedCommandAlias(strtolower($alias), $targets); }else{ unset($this->knownCommands[strtolower($alias)]); } } } }