84 protected array $knownCommands = [];
86 public function __construct(
private Server $server){
87 $this->setDefaultCommands();
90 private function setDefaultCommands() :
void{
135 public function registerAll(
string $fallbackPrefix, array $commands) : void{
136 foreach($commands as $command){
137 $this->
register($fallbackPrefix, $command);
141 public function register(
string $fallbackPrefix,
Command $command, ?
string $label =
null) :
bool{
142 if(count($command->getPermissions()) === 0){
143 throw new \InvalidArgumentException(
"Commands must have a permission set");
147 $label = $command->getLabel();
149 $label = trim($label);
150 $fallbackPrefix = strtolower(trim($fallbackPrefix));
152 $registered = $this->registerAlias($command,
false, $fallbackPrefix, $label);
154 $aliases = $command->getAliases();
155 foreach($aliases as $index => $alias){
156 if(!$this->registerAlias($command,
true, $fallbackPrefix, $alias)){
157 unset($aliases[$index]);
160 $command->setAliases($aliases);
163 $command->setLabel($fallbackPrefix .
":" . $label);
166 $command->register($this);
171 public function unregister(Command $command) : bool{
172 foreach($this->knownCommands as $lbl => $cmd){
173 if($cmd === $command){
174 unset($this->knownCommands[$lbl]);
178 $command->unregister($this);
183 private function registerAlias(Command $command,
bool $isAlias,
string $fallbackPrefix,
string $label) : bool{
184 $this->knownCommands[$fallbackPrefix .
":" . $label] = $command;
185 if(($command instanceof VanillaCommand || $isAlias) && isset($this->knownCommands[$label])){
189 if(isset($this->knownCommands[$label]) && $this->knownCommands[$label]->getLabel() === $label){
194 $command->setLabel($label);
197 $this->knownCommands[$label] = $command;
202 public function dispatch(CommandSender $sender,
string $commandLine) : bool{
203 $args = CommandStringHelper::parseQuoteAware($commandLine);
205 $sentCommandLabel = array_shift($args);
206 if($sentCommandLabel !==
null && ($target = $this->getCommand($sentCommandLabel)) !==
null){
207 $timings = Timings::getCommandDispatchTimings($target->getLabel());
208 $timings->startTiming();
211 if($target->testPermission($sender)){
212 $target->execute($sender, $sentCommandLabel, $args);
214 }
catch(InvalidCommandSyntaxException $e){
215 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
217 $timings->stopTiming();
222 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel ??
"",
"/help")->prefix(TextFormat::RED));
226 public function clearCommands() : void{
227 foreach($this->knownCommands as $command){
228 $command->unregister($this);
230 $this->knownCommands = [];
231 $this->setDefaultCommands();
234 public function getCommand(
string $name) : ?Command{
235 return $this->knownCommands[$name] ?? null;
242 return $this->knownCommands;
245 public function registerServerAliases() : void{
246 $values = $this->
server->getCommandAliases();
248 foreach($values as $alias => $commandStrings){
249 if(str_contains($alias,
":")){
250 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_illegal($alias)));
258 foreach($commandStrings as $commandString){
259 $args = CommandStringHelper::parseQuoteAware($commandString);
260 $commandName = array_shift($args) ??
"";
261 $command = $this->getCommand($commandName);
263 if($command ===
null){
264 $bad[] = $commandString;
265 }elseif(strcasecmp($commandName, $alias) === 0){
266 $recursive[] = $commandString;
268 $targets[] = $commandString;
272 if(count($recursive) > 0){
273 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_recursive($alias, implode(
", ", $recursive))));
278 $this->
server->getLogger()->warning($this->
server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_notFound($alias, implode(
", ", $bad))));
283 $lowerAlias = strtolower($alias);
284 if(count($targets) > 0){
285 $this->knownCommands[$lowerAlias] =
new FormattedCommandAlias($lowerAlias, $targets);
287 unset($this->knownCommands[$lowerAlias]);