PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SimpleCommandMap.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\command;
23 
60 
61 class SimpleCommandMap implements CommandMap{
62 
66  protected $knownCommands = [];
67 
69  private $server;
70 
71  public function __construct(Server $server){
72  $this->server = $server;
73  $this->setDefaultCommands();
74  }
75 
76  private function setDefaultCommands(){
77  $this->register("pocketmine", new VersionCommand("version"));
78  $this->register("pocketmine", new PluginsCommand("plugins"));
79  $this->register("pocketmine", new SeedCommand("seed"));
80  $this->register("pocketmine", new HelpCommand("help"));
81  $this->register("pocketmine", new StopCommand("stop"));
82  $this->register("pocketmine", new TellCommand("tell"));
83  $this->register("pocketmine", new DefaultGamemodeCommand("defaultgamemode"));
84  $this->register("pocketmine", new BanCommand("ban"));
85  $this->register("pocketmine", new BanIpCommand("ban-ip"));
86  $this->register("pocketmine", new BanListCommand("banlist"));
87  $this->register("pocketmine", new PardonCommand("pardon"));
88  $this->register("pocketmine", new PardonIpCommand("pardon-ip"));
89  $this->register("pocketmine", new SayCommand("say"));
90  $this->register("pocketmine", new MeCommand("me"));
91  $this->register("pocketmine", new ListCommand("list"));
92  $this->register("pocketmine", new DifficultyCommand("difficulty"));
93  $this->register("pocketmine", new KickCommand("kick"));
94  $this->register("pocketmine", new OpCommand("op"));
95  $this->register("pocketmine", new DeopCommand("deop"));
96  $this->register("pocketmine", new WhitelistCommand("whitelist"));
97  $this->register("pocketmine", new SaveOnCommand("save-on"));
98  $this->register("pocketmine", new SaveOffCommand("save-off"));
99  $this->register("pocketmine", new SaveCommand("save-all"));
100  $this->register("pocketmine", new GiveCommand("give"));
101  $this->register("pocketmine", new GamemodeCommand("gamemode"));
102  $this->register("pocketmine", new KillCommand("kill"));
103  $this->register("pocketmine", new SpawnpointCommand("spawnpoint"));
104  $this->register("pocketmine", new SetWorldSpawnCommand("setworldspawn"));
105  $this->register("pocketmine", new TeleportCommand("tp"));
106  $this->register("pocketmine", new TimeCommand("time"));
107  $this->register("pocketmine", new TimingsCommand("timings"));
108  $this->register("pocketmine", new ReloadCommand("reload"));
109 
110  if($this->server->getProperty("debug.commands", false) === true){
111  $this->register("pocketmine", new StatusCommand("status"));
112  }
113  }
114 
115 
116  public function registerAll($fallbackPrefix, array $commands){
117  foreach($commands as $command){
118  $this->register($fallbackPrefix, $command);
119  }
120  }
121 
122  public function register($fallbackPrefix, Command $command, $label = null){
123  if($label === null){
124  $label = $command->getName();
125  }
126  $label = strtolower(trim($label));
127  $fallbackPrefix = strtolower(trim($fallbackPrefix));
128 
129  $registered = $this->registerAlias($command, false, $fallbackPrefix, $label);
130 
131  $aliases = $command->getAliases();
132  foreach($aliases as $index => $alias){
133  if(!$this->registerAlias($command, true, $fallbackPrefix, $alias)){
134  unset($aliases[$index]);
135  }
136  }
137  $command->setAliases($aliases);
138 
139  if(!$registered){
140  $command->setLabel($fallbackPrefix . ":" . $label);
141  }
142 
143  $command->register($this);
144 
145  return $registered;
146  }
147 
148  private function registerAlias(Command $command, $isAlias, $fallbackPrefix, $label){
149  $this->knownCommands[$fallbackPrefix . ":" . $label] = $command;
150  if(($command instanceof VanillaCommand or $isAlias) and isset($this->knownCommands[$label])){
151  return false;
152  }
153 
154  if(isset($this->knownCommands[$label]) and $this->knownCommands[$label]->getLabel() !== null and $this->knownCommands[$label]->getLabel() === $label){
155  return false;
156  }
157 
158  if(!$isAlias){
159  $command->setLabel($label);
160  }
161 
162  $this->knownCommands[$label] = $command;
163 
164  return true;
165  }
166 
167  public function dispatch(CommandSender $sender, $commandLine){
168  $args = explode(" ", $commandLine);
169 
170  if(count($args) === 0){
171  return false;
172  }
173 
174  $sentCommandLabel = strtolower(array_shift($args));
175  $target = $this->getCommand($sentCommandLabel);
176 
177  if($target === null){
178  return false;
179  }
180 
181  $target->timings->startTiming();
182  try{
183  $target->execute($sender, $sentCommandLabel, $args);
184  }catch(\Exception $e){
185  $this->server->getLogger()->critical("Unhandled exception executing command '" . $commandLine . "' in " . $target . ": " . $e->getMessage());
186  if(($logger = $sender->getServer()->getLogger()) instanceof MainLogger){
187  $logger->logException($e);
188  }
189  }
190  $target->timings->stopTiming();
191 
192  return true;
193  }
194 
195  public function clearCommands(){
196  foreach($this->knownCommands as $command){
197  $command->unregister($this);
198  }
199  $this->knownCommands = [];
200  $this->setDefaultCommands();
201  }
202 
203  public function getCommand($name){
204  if(isset($this->knownCommands[$name])){
205  return $this->knownCommands[$name];
206  }
207 
208  return null;
209  }
210 
214  public function getCommands(){
215  return $this->knownCommands;
216  }
217 
218 
222  public function registerServerAliases(){
223  $values = $this->server->getCommandAliases();
224 
225  foreach($values as $alias => $commandStrings){
226  if(strpos($alias, ":") !== false or strpos($alias, " ") !== false){
227  $this->server->getLogger()->warning("Could not register alias " . $alias . " because it contains illegal characters");
228  continue;
229  }
230 
231  $targets = [];
232 
233  $bad = "";
234  foreach($commandStrings as $commandString){
235  $args = explode(" ", $commandString);
236  $command = $this->getCommand($args[0]);
237 
238  if($command === null){
239  if(strlen($bad) > 0){
240  $bad .= ", ";
241  }
242  $bad .= $commandString;
243  }else{
244  $targets[] = $commandString;
245  }
246  }
247 
248  if(strlen($bad) > 0){
249  $this->server->getLogger()->warning("Could not register alias " . $alias . " because it contains commands that do not exist: " . $bad);
250  continue;
251  }
252 
253  //These registered commands have absolute priority
254  if(count($targets) > 0){
255  $this->knownCommands[strtolower($alias)] = new FormattedCommandAlias(strtolower($alias), $targets);
256  }else{
257  unset($this->knownCommands[strtolower($alias)]);
258  }
259 
260  }
261  }
262 
263 
264 }
dispatch(CommandSender $sender, $commandLine)
registerAll($fallbackPrefix, array $commands)