89 protected array $knownCommands = [];
91 public function __construct(
private Server $server){
92 $this->setDefaultCommands();
95 private function setDefaultCommands() :
void{
141 public function registerAll(
string $fallbackPrefix, array $commands) : void{
142 foreach($commands as $command){
143 $this->
register($fallbackPrefix, $command);
147 public function register(
string $fallbackPrefix,
Command $command, ?
string $label =
null) :
bool{
148 if(count($command->getPermissions()) === 0){
149 throw new \InvalidArgumentException(
"Commands must have a permission set");
153 $label = $command->getLabel();
155 $label = trim($label);
156 $fallbackPrefix = strtolower(trim($fallbackPrefix));
158 $registered = $this->registerAlias($command,
false, $fallbackPrefix, $label);
160 $aliases = $command->getAliases();
161 foreach($aliases as $index => $alias){
162 if(!$this->registerAlias($command,
true, $fallbackPrefix, $alias)){
163 unset($aliases[$index]);
166 $command->setAliases($aliases);
169 $command->setLabel($fallbackPrefix .
":" . $label);
172 $command->register($this);
177 public function unregister(Command $command) : bool{
178 foreach(Utils::promoteKeys($this->knownCommands) as $lbl => $cmd){
179 if($cmd === $command){
180 unset($this->knownCommands[$lbl]);
184 $command->unregister($this);
189 private function registerAlias(Command $command,
bool $isAlias,
string $fallbackPrefix,
string $label) : bool{
190 $this->knownCommands[$fallbackPrefix .
":" . $label] = $command;
191 if(($command instanceof VanillaCommand || $isAlias) && isset($this->knownCommands[$label])){
195 if(isset($this->knownCommands[$label]) && $this->knownCommands[$label]->getLabel() === $label){
200 $command->setLabel($label);
203 $this->knownCommands[$label] = $command;
208 public function dispatch(CommandSender $sender,
string $commandLine) : bool{
209 $args = CommandStringHelper::parseQuoteAware($commandLine);
211 $sentCommandLabel = array_shift($args);
212 if($sentCommandLabel !==
null && ($target = $this->getCommand($sentCommandLabel)) !==
null){
213 $timings = Timings::getCommandDispatchTimings($target->getLabel());
214 $timings->startTiming();
217 if($target->testPermission($sender)){
218 $target->execute($sender, $sentCommandLabel, $args);
220 }
catch(InvalidCommandSyntaxException $e){
221 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
223 $timings->stopTiming();
228 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel ??
"",
"/help")->prefix(TextFormat::RED));
232 public function clearCommands() : void{
233 foreach($this->knownCommands as $command){
234 $command->unregister($this);
236 $this->knownCommands = [];
237 $this->setDefaultCommands();
240 public function getCommand(
string $name) : ?Command{
241 return $this->knownCommands[$name] ?? null;
249 return $this->knownCommands;
252 public function registerServerAliases() : void{
253 $values = $this->
server->getCommandAliases();
255 foreach(Utils::stringifyKeys($values) as $alias => $commandStrings){
256 if(str_contains($alias,
":")){
257 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_illegal($alias)));
265 foreach($commandStrings as $commandString){
266 $args = CommandStringHelper::parseQuoteAware($commandString);
267 $commandName = array_shift($args) ??
"";
268 $command = $this->getCommand($commandName);
270 if($command ===
null){
271 $bad[] = $commandString;
272 }elseif(strcasecmp($commandName, $alias) === 0){
273 $recursive[] = $commandString;
275 $targets[] = $commandString;
279 if(count($recursive) > 0){
280 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_recursive($alias, implode(
", ", $recursive))));
285 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_notFound($alias, implode(
", ", $bad))));
290 $lowerAlias = strtolower($alias);
291 if(count($targets) > 0){
292 $this->knownCommands[$lowerAlias] =
new FormattedCommandAlias($lowerAlias, $targets);
294 unset($this->knownCommands[$lowerAlias]);