PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/command/SimpleCommandMap.php

https://gitlab.com/Skull3x/Genisys
PHP | 364 lines | 284 code | 50 blank | 30 comment | 21 complexity | 072b5ade054ebf7e20a2fc94aa4d1abd MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * ____ _ _ __ __ _ __ __ ____
  5. * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
  6. * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
  7. * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
  8. * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * @author PocketMine Team
  16. * @link http://www.pocketmine.net/
  17. *
  18. *
  19. */
  20. namespace pocketmine\command;
  21. use pocketmine\command\defaults\BanCommand;
  22. use pocketmine\command\defaults\BanIpCommand;
  23. use pocketmine\command\defaults\BanListCommand;
  24. use pocketmine\command\defaults\BiomeCommand;
  25. use pocketmine\command\defaults\CaveCommand;
  26. use pocketmine\command\defaults\ChunkInfoCommand;
  27. use pocketmine\command\defaults\DefaultGamemodeCommand;
  28. use pocketmine\command\defaults\DeopCommand;
  29. use pocketmine\command\defaults\DifficultyCommand;
  30. use pocketmine\command\defaults\DumpMemoryCommand;
  31. use pocketmine\command\defaults\EffectCommand;
  32. use pocketmine\command\defaults\EnchantCommand;
  33. use pocketmine\command\defaults\GamemodeCommand;
  34. use pocketmine\command\defaults\GarbageCollectorCommand;
  35. use pocketmine\command\defaults\GiveCommand;
  36. use pocketmine\command\defaults\HelpCommand;
  37. use pocketmine\command\defaults\KickCommand;
  38. use pocketmine\command\defaults\KillCommand;
  39. use pocketmine\command\defaults\ListCommand;
  40. use pocketmine\command\defaults\LoadPluginCommand;
  41. use pocketmine\command\defaults\LvdatCommand;
  42. use pocketmine\command\defaults\MeCommand;
  43. use pocketmine\command\defaults\OpCommand;
  44. use pocketmine\command\defaults\PardonCommand;
  45. use pocketmine\command\defaults\PardonIpCommand;
  46. use pocketmine\command\defaults\ParticleCommand;
  47. use pocketmine\command\defaults\PluginsCommand;
  48. use pocketmine\command\defaults\ReloadCommand;
  49. use pocketmine\command\defaults\SaveCommand;
  50. use pocketmine\command\defaults\SaveOffCommand;
  51. use pocketmine\command\defaults\SaveOnCommand;
  52. use pocketmine\command\defaults\SayCommand;
  53. use pocketmine\command\defaults\SeedCommand;
  54. use pocketmine\command\defaults\SetBlockCommand;
  55. use pocketmine\command\defaults\SetWorldSpawnCommand;
  56. use pocketmine\command\defaults\SpawnpointCommand;
  57. use pocketmine\command\defaults\StatusCommand;
  58. use pocketmine\command\defaults\StopCommand;
  59. use pocketmine\command\defaults\SummonCommand;
  60. use pocketmine\command\defaults\TeleportCommand;
  61. use pocketmine\command\defaults\TellCommand;
  62. use pocketmine\command\defaults\TimeCommand;
  63. use pocketmine\command\defaults\TimingsCommand;
  64. use pocketmine\command\defaults\VanillaCommand;
  65. use pocketmine\command\defaults\VersionCommand;
  66. use pocketmine\command\defaults\WhitelistCommand;
  67. use pocketmine\command\defaults\XpCommand;
  68. use pocketmine\command\defaults\FillCommand;
  69. use pocketmine\event\TranslationContainer;
  70. use pocketmine\Player;
  71. use pocketmine\Server;
  72. use pocketmine\utils\MainLogger;
  73. use pocketmine\utils\TextFormat;
  74. use pocketmine\command\defaults\MakeServerCommand;
  75. use pocketmine\command\defaults\ExtractPluginCommand;
  76. use pocketmine\command\defaults\ExtractPharCommand;
  77. use pocketmine\command\defaults\MakePluginCommand;
  78. use pocketmine\command\defaults\BancidbynameCommand;
  79. use pocketmine\command\defaults\BanipbynameCommand;
  80. use pocketmine\command\defaults\BanCidCommand;
  81. use pocketmine\command\defaults\PardonCidCommand;
  82. use pocketmine\command\defaults\WeatherCommand;
  83. class SimpleCommandMap implements CommandMap{
  84. /**
  85. * @var Command[]
  86. */
  87. protected $knownCommands = [];
  88. /** @var Server */
  89. private $server;
  90. public function __construct(Server $server){
  91. $this->server = $server;
  92. $this->setDefaultCommands();
  93. }
  94. private function setDefaultCommands(){
  95. $this->register("pocketmine", new WeatherCommand("weather"));
  96. $this->register("pocketmine", new BanCidCommand("bancid"));
  97. $this->register("pocketmine", new PardonCidCommand("pardoncid"));
  98. $this->register("pocketmine", new BancidbynameCommand("bancidbyname"));
  99. $this->register("pocketmine", new BanipbynameCommand("banipbyname"));
  100. $this->register("pocketmine", new ExtractPharCommand("extractphar"));
  101. $this->register("pocketmine", new ExtractPluginCommand("extractplugin"));
  102. $this->register("pocketmine", new MakePluginCommand("makeplugin"));
  103. $this->register("pocketmine", new MakeServerCommand("ms"));
  104. //$this->register("pocketmine", new MakeServerCommand("makeserver"));
  105. $this->register("pocketmine", new ExtractPluginCommand("ep"));
  106. $this->register("pocketmine", new MakePluginCommand("mp"));
  107. $this->register("pocketmine", new LoadPluginCommand("loadplugin"));
  108. $this->register("pocketmine", new LvdatCommand("lvdat"));
  109. $this->register("pocketmine", new BiomeCommand("biome"));
  110. $this->register("pocketmine", new CaveCommand("cave"));
  111. $this->register("pocketmine", new ChunkInfoCommand("chunkinfo"));
  112. $this->register("pocketmine", new VersionCommand("version"));
  113. $this->register("pocketmine", new FillCommand("fill"));
  114. $this->register("pocketmine", new PluginsCommand("plugins"));
  115. $this->register("pocketmine", new SeedCommand("seed"));
  116. $this->register("pocketmine", new HelpCommand("help"));
  117. $this->register("pocketmine", new StopCommand("stop"));
  118. $this->register("pocketmine", new TellCommand("tell"));
  119. $this->register("pocketmine", new DefaultGamemodeCommand("defaultgamemode"));
  120. $this->register("pocketmine", new BanCommand("ban"));
  121. $this->register("pocketmine", new BanIpCommand("ban-ip"));
  122. $this->register("pocketmine", new BanListCommand("banlist"));
  123. $this->register("pocketmine", new PardonCommand("pardon"));
  124. $this->register("pocketmine", new PardonIpCommand("pardon-ip"));
  125. $this->register("pocketmine", new SayCommand("say"));
  126. $this->register("pocketmine", new MeCommand("me"));
  127. $this->register("pocketmine", new ListCommand("list"));
  128. $this->register("pocketmine", new DifficultyCommand("difficulty"));
  129. $this->register("pocketmine", new KickCommand("kick"));
  130. $this->register("pocketmine", new OpCommand("op"));
  131. $this->register("pocketmine", new DeopCommand("deop"));
  132. $this->register("pocketmine", new WhitelistCommand("whitelist"));
  133. $this->register("pocketmine", new SaveOnCommand("save-on"));
  134. $this->register("pocketmine", new SaveOffCommand("save-off"));
  135. $this->register("pocketmine", new SaveCommand("save-all"));
  136. $this->register("pocketmine", new GiveCommand("give"));
  137. $this->register("pocketmine", new EffectCommand("effect"));
  138. $this->register("pocketmine", new EnchantCommand("enchant"));
  139. $this->register("pocketmine", new ParticleCommand("particle"));
  140. $this->register("pocketmine", new GamemodeCommand("gamemode"));
  141. $this->register("pocketmine", new KillCommand("kill"));
  142. $this->register("pocketmine", new SpawnpointCommand("spawnpoint"));
  143. $this->register("pocketmine", new SetWorldSpawnCommand("setworldspawn"));
  144. $this->register("pocketmine", new SummonCommand("summon"));
  145. $this->register("pocketmine", new TeleportCommand("tp"));
  146. $this->register("pocketmine", new TimeCommand("time"));
  147. $this->register("pocketmine", new TimingsCommand("timings"));
  148. $this->register("pocketmine", new ReloadCommand("reload"));
  149. $this->register("pocketmine", new XpCommand("xp"));
  150. $this->register("pocketmine", new SetBlockCommand("setblock"));
  151. if($this->server->getProperty("debug.commands", false)){
  152. $this->register("pocketmine", new StatusCommand("status"));
  153. $this->register("pocketmine", new GarbageCollectorCommand("gc"));
  154. $this->register("pocketmine", new DumpMemoryCommand("dumpmemory"));
  155. }
  156. }
  157. public function registerAll($fallbackPrefix, array $commands){
  158. foreach($commands as $command){
  159. $this->register($fallbackPrefix, $command);
  160. }
  161. }
  162. public function register($fallbackPrefix, Command $command, $label = null){
  163. if($label === null){
  164. $label = $command->getName();
  165. }
  166. $label = strtolower(trim($label));
  167. $fallbackPrefix = strtolower(trim($fallbackPrefix));
  168. $registered = $this->registerAlias($command, false, $fallbackPrefix, $label);
  169. $aliases = $command->getAliases();
  170. foreach($aliases as $index => $alias){
  171. if(!$this->registerAlias($command, true, $fallbackPrefix, $alias)){
  172. unset($aliases[$index]);
  173. }
  174. }
  175. $command->setAliases($aliases);
  176. if(!$registered){
  177. $command->setLabel($fallbackPrefix . ":" . $label);
  178. }
  179. $command->register($this);
  180. return $registered;
  181. }
  182. private function registerAlias(Command $command, $isAlias, $fallbackPrefix, $label){
  183. $this->knownCommands[$fallbackPrefix . ":" . $label] = $command;
  184. if(($command instanceof VanillaCommand or $isAlias) and isset($this->knownCommands[$label])){
  185. return false;
  186. }
  187. if(isset($this->knownCommands[$label]) and $this->knownCommands[$label]->getLabel() !== null and $this->knownCommands[$label]->getLabel() === $label){
  188. return false;
  189. }
  190. if(!$isAlias){
  191. $command->setLabel($label);
  192. }
  193. $this->knownCommands[$label] = $command;
  194. return true;
  195. }
  196. private function dispatchAdvanced(CommandSender $sender, Command $command, $label, array $args, $offset = 0){
  197. if(isset($args[$offset])){
  198. $argsTemp = $args;
  199. switch($args[$offset]){
  200. case "@a":
  201. $p = $this->server->getOnlinePlayers();
  202. if(count($p) <= 0){
  203. $sender->sendMessage(TextFormat::RED . "No players online"); //TODO: add language
  204. }else{
  205. foreach($p as $player){
  206. $argsTemp[$offset] = $player->getName();
  207. $this->dispatchAdvanced($sender, $command, $label, $argsTemp, $offset + 1);
  208. }
  209. }
  210. break;
  211. case "@r":
  212. $players = $this->server->getOnlinePlayers();
  213. if(count($players) > 0){
  214. $argsTemp[$offset] = $players[array_rand($players)]->getName();
  215. $this->dispatchAdvanced($sender, $command, $label, $argsTemp, $offset + 1);
  216. }
  217. break;
  218. case "@p":
  219. if($sender instanceof Player){
  220. $argsTemp[$offset] = $sender->getName();
  221. $this->dispatchAdvanced($sender, $command, $label, $argsTemp, $offset + 1);
  222. }else{
  223. $sender->sendMessage(TextFormat::RED . "You must be a player!"); //TODO: add language
  224. }
  225. break;
  226. default:
  227. $this->dispatchAdvanced($sender, $command, $label, $argsTemp, $offset + 1);
  228. }
  229. }else $command->execute($sender, $label, $args);
  230. }
  231. public function dispatch(CommandSender $sender, $commandLine){
  232. $args = explode(" ", $commandLine);
  233. if(count($args) === 0){
  234. return false;
  235. }
  236. $sentCommandLabel = strtolower(array_shift($args));
  237. $target = $this->getCommand($sentCommandLabel);
  238. if($target === null){
  239. return false;
  240. }
  241. $target->timings->startTiming();
  242. try{
  243. if($this->server->advancedCommandSelector){
  244. $this->dispatchAdvanced($sender, $target, $sentCommandLabel, $args);
  245. }else{
  246. $target->execute($sender, $sentCommandLabel, $args);
  247. }
  248. }catch(\Throwable $e){
  249. $sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.exception"));
  250. $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.command.exception", [$commandLine, (string) $target, $e->getMessage()]));
  251. $logger = $sender->getServer()->getLogger();
  252. if($logger instanceof MainLogger){
  253. $logger->logException($e);
  254. }
  255. }
  256. $target->timings->stopTiming();
  257. return true;
  258. }
  259. public function clearCommands(){
  260. foreach($this->knownCommands as $command){
  261. $command->unregister($this);
  262. }
  263. $this->knownCommands = [];
  264. $this->setDefaultCommands();
  265. }
  266. public function getCommand($name){
  267. if(isset($this->knownCommands[$name])){
  268. return $this->knownCommands[$name];
  269. }
  270. return null;
  271. }
  272. /**
  273. * @return Command[]
  274. */
  275. public function getCommands(){
  276. return $this->knownCommands;
  277. }
  278. /**
  279. * @return void
  280. */
  281. public function registerServerAliases(){
  282. $values = $this->server->getCommandAliases();
  283. foreach($values as $alias => $commandStrings){
  284. if(strpos($alias, ":") !== false or strpos($alias, " ") !== false){
  285. $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.illegal", [$alias]));
  286. continue;
  287. }
  288. $targets = [];
  289. $bad = "";
  290. foreach($commandStrings as $commandString){
  291. $args = explode(" ", $commandString);
  292. $command = $this->getCommand($args[0]);
  293. if($command === null){
  294. if(strlen($bad) > 0){
  295. $bad .= ", ";
  296. }
  297. $bad .= $commandString;
  298. }else{
  299. $targets[] = $commandString;
  300. }
  301. }
  302. if(strlen($bad) > 0){
  303. $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.notFound", [$alias, $bad]));
  304. continue;
  305. }
  306. //These registered commands have absolute priority
  307. if(count($targets) > 0){
  308. $this->knownCommands[strtolower($alias)] = new FormattedCommandAlias(strtolower($alias), $targets);
  309. }else{
  310. unset($this->knownCommands[strtolower($alias)]);
  311. }
  312. }
  313. }
  314. }