vMinecraftCommands.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. import java.io.IOException;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. //=====================================================================
  7. //Class: vMinecraftCommands
  8. //Use: Encapsulates all commands added by this mod
  9. //Author: nos, trapalice, cerevisiae
  10. //=====================================================================
  11. public class vMinecraftCommands{
  12. //Log output
  13. protected static final Logger log = Logger.getLogger("Minecraft");
  14. static final int EXIT_FAIL = 0,
  15. EXIT_SUCCESS = 1,
  16. EXIT_CONTINUE = 2;
  17. //The list of commands for vMinecraft
  18. public static commandList cl = new commandList();
  19. //=====================================================================
  20. //Function: loadCommands
  21. //Input: None
  22. //Output: None
  23. //Use: Imports all the commands into the command list
  24. //=====================================================================
  25. public static void loadCommands(){
  26. //If we had commands we would add them here.
  27. cl.register("/tp", "teleport");
  28. cl.register("/masstp", "masstp", "Teleports those with lower permissions to you");
  29. cl.register("/reload", "reload");
  30. cl.register("/rules", "rules", "Displays the rules");
  31. cl.register("/fabulous", "fabulous", "makes text SUUUPER");
  32. cl.register("/whois", "whois", "/whois [user]");
  33. cl.register("/who", "who");
  34. cl.register("/say", "say");
  35. cl.register("/slay", "slay", "Kill target player");
  36. cl.register("/ezmodo", "invuln", "Toggle invulnerability");
  37. cl.register("/ezlist", "ezlist", "List invulnerable players");
  38. cl.register("/heal", "heal", "heal yourself or other players");
  39. cl.register("/suicide", "suicide", "kill yourself... you loser");
  40. cl.register("/a", "adminChatToggle", "toggle admin chat for every message");
  41. cl.register("/modify", "modifySplit");
  42. cl.register("/me", "me");
  43. cl.registerAlias("/playerlist", "/who");
  44. cl.registerAlias("/wrists", "/suicide");
  45. cl.registerAlias("/ci", "/clearinventory");
  46. }
  47. //=====================================================================
  48. //Function: me (/me)
  49. //Input: Player player: The player using the command
  50. //Output: int: Exit Code
  51. //Use: The player uses this to emote, but now its colorful.
  52. //=====================================================================
  53. public static int me(Player player, String[] args)
  54. {
  55. String str = etc.combineSplit(0, args, " ");
  56. if (args.length < 1) {return EXIT_FAIL;}
  57. vMinecraftChat.emote(player, str);
  58. return EXIT_SUCCESS;
  59. }
  60. //=====================================================================
  61. //Function: adminChatToggle (/a)
  62. //Input: Player player: The player using the command
  63. //Output: int: Exit Code
  64. //Use: Toggles the player into admin chat. Every message they
  65. // send will be piped to admin chat.
  66. //=====================================================================
  67. public static int adminChatToggle(Player player, String[] args)
  68. {
  69. if(vMinecraftSettings.getInstance().adminChatToggle())
  70. {
  71. //If the player is already toggled for admin chat, remove them
  72. if (vMinecraftSettings.getInstance().isAdminToggled(player.getName())) {
  73. player.sendMessage(Colors.Red + "Admin Chat Toggle = off");
  74. vMinecraftSettings.getInstance().removeAdminToggled(player.getName());
  75. //Otherwise include them
  76. } else {
  77. player.sendMessage(Colors.Blue + "Admin Chat Toggled on");
  78. vMinecraftSettings.getInstance().addAdminToggled(player.getName());
  79. }
  80. return EXIT_SUCCESS;
  81. }
  82. return EXIT_FAIL;
  83. }
  84. //=====================================================================
  85. //Function: heal (/heal)
  86. //Input: Player player: The player using the command
  87. // String[] args: The arguments for the command. Should be a
  88. // player name or blank
  89. //Output: int: Exit Code
  90. //Use: Heals yourself or a specified player.
  91. //=====================================================================
  92. public static int heal(Player player, String[] args)
  93. {
  94. if(vMinecraftSettings.getInstance().cmdHeal())
  95. {
  96. //If a target wasn't specified, heal the user.
  97. if (args.length < 1){
  98. player.setHealth(20);
  99. player.sendMessage("Your health is restored");
  100. //If a target was specified, try to find them and then heal them
  101. //Otherwise report the error
  102. } else if (args.length > 0){
  103. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  104. if (playerTarget != null){
  105. playerTarget.setHealth(20);
  106. player.sendMessage(Colors.Blue + "You have healed " + vMinecraftChat.getName(playerTarget));
  107. playerTarget.sendMessage(Colors.Blue + "You have been healed by " + vMinecraftChat.getName(player));
  108. }
  109. else if (playerTarget == null){
  110. player.sendMessage(Colors.Rose + "Couldn't find that player");
  111. }
  112. }
  113. return EXIT_SUCCESS;
  114. }
  115. return EXIT_FAIL;
  116. }
  117. //=====================================================================
  118. //Function: suicide (/suicide, /wrists)
  119. //Input: Player player: The player using the command
  120. // String[] args: Ignored
  121. //Output: int: Exit Code
  122. //Use: Kills yourself
  123. //=====================================================================
  124. public static int suicide(Player player, String[] args)
  125. {
  126. if(vMinecraftSettings.getInstance().cmdSuicide())
  127. {
  128. //Set your health to 0. Not much to it.
  129. player.setHealth(0);
  130. return EXIT_SUCCESS;
  131. }
  132. return EXIT_FAIL;
  133. }
  134. //=====================================================================
  135. //Function: teleport (/tp)
  136. //Input: Player player: The player using the command
  137. // String[] args: The arguments for the command. Should be a
  138. // player name
  139. //Output: int: Exit Code
  140. //Use: Teleports the user to another player
  141. //=====================================================================
  142. public static int teleport(Player player, String[] args)
  143. {
  144. //Get if the command is enabled
  145. if(vMinecraftSettings.getInstance().cmdTp())
  146. {
  147. //Make sure a player has been specified and return an error if not
  148. if (args.length < 1) {
  149. player.sendMessage(Colors.Rose + "Correct usage is: /tp [player]");
  150. } else {
  151. //Find the player by name
  152. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  153. //Target player isn't found
  154. if(playerTarget == null)
  155. player.sendMessage(Colors.Rose + "Can't find user "
  156. + args[0] + ".");
  157. //If it's you, return witty message
  158. else if (player.getName().equalsIgnoreCase(args[0]))
  159. player.sendMessage(Colors.Rose + "You're already here!");
  160. //If the player is higher rank than you, inform the user
  161. else if (!player.hasControlOver(playerTarget))
  162. player.sendMessage(Colors.Red +
  163. "That player has higher permissions than you.");
  164. //If the player exists transport the user to the player
  165. else {
  166. log.log(Level.INFO, player.getName() + " teleported to " +
  167. playerTarget.getName());
  168. player.teleportTo(playerTarget);
  169. //Otherwise inform the user that the player doesn't exist
  170. }
  171. }
  172. return EXIT_SUCCESS;
  173. }
  174. return EXIT_FAIL;
  175. }
  176. //=====================================================================
  177. //Function: masstp (/masstp)
  178. //Input: Player player: The player using the command
  179. // String[] args: Should be empty or is ignored
  180. //Output: int: Exit Code
  181. //Use: Teleports all players to the user
  182. //=====================================================================
  183. public static int masstp(Player player, String[] args)
  184. {
  185. //If the command is enabled
  186. if(vMinecraftSettings.getInstance().cmdMasstp()) {
  187. //Go through all players and move them to the user
  188. for (Player p : etc.getServer().getPlayerList()) {
  189. if (!p.hasControlOver(player)) {
  190. p.teleportTo(player);
  191. }
  192. }
  193. //Inform the user that the command has executed successfully
  194. player.sendMessage(Colors.Blue+"Summoning successful.");
  195. return EXIT_SUCCESS;
  196. }
  197. return EXIT_FAIL;
  198. }
  199. //=====================================================================
  200. //Function: tphere (/tphere)
  201. //Input: Player player: The player using the command
  202. // String[] args: The arguments for the command. Should be a
  203. // player name
  204. //Output: int: Exit Code
  205. //Use: Teleports the user to another player
  206. //=====================================================================
  207. public static int tphere(Player player, String[] args)
  208. {
  209. //Check if the command is enabled.
  210. if (vMinecraftSettings.getInstance().cmdTphere()) {
  211. //Make sure a player is specified
  212. if (args.length < 1) {
  213. player.sendMessage(Colors.Rose + "Correct usage is: /tphere [player]");
  214. } else {
  215. //Get the player by name
  216. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  217. //If the target doesn't exist
  218. if(playerTarget == null)
  219. player.sendMessage(Colors.Rose + "Can't find user " + args[0] + ".");
  220. //If the player has a higher rank than the user, return error
  221. else if (!player.hasControlOver(playerTarget))
  222. player.sendMessage(Colors.Red + "That player has higher permissions than you.");
  223. //If the user teleports themselves, mock them
  224. else if (player.getName().equalsIgnoreCase(args[0]))
  225. player.sendMessage(Colors.Rose + "Wow look at that! You teleported yourself to yourself!");
  226. //If the target exists, teleport them to the user
  227. else {
  228. log.log(Level.INFO, player.getName() + " teleported " + player.getName() + " to their self.");
  229. playerTarget.teleportTo(player);
  230. }
  231. }
  232. return EXIT_SUCCESS;
  233. }
  234. return EXIT_FAIL;
  235. }
  236. //=====================================================================
  237. //Function: reload (/reload)
  238. //Input: Player player: The player using the command
  239. // String[] args: Ignored
  240. //Output: int: Exit Code
  241. //Use: Reloads the settings for vMinecraft
  242. //=====================================================================
  243. public static int reload(Player player, String[] args)
  244. {
  245. vMinecraftSettings.getInstance().loadSettings();
  246. return EXIT_FAIL;
  247. }
  248. //=====================================================================
  249. //Function: rules (/rules)
  250. //Input: Player player: The player using the command
  251. // String[] args: Ignored
  252. //Output: int: Exit Code
  253. //Use: Lists the rules
  254. //=====================================================================
  255. public static int rules(Player player, String[] args)
  256. {
  257. //If the rules exist
  258. if(vMinecraftSettings.getInstance().cmdRules()
  259. && vMinecraftSettings.getInstance().getRules().length > 0) {
  260. //Apply QuakeCode Colors to the rules
  261. String[] rules = vMinecraftChat.applyColors(
  262. vMinecraftSettings.getInstance().getRules());
  263. //Display them
  264. for (String str : rules ) {
  265. if(!str.isEmpty())
  266. player.sendMessage(Colors.Blue + str);
  267. else
  268. player.sendMessage(Colors.Blue + "!!!The Rules Have Not Been Set!!!");
  269. }
  270. return EXIT_SUCCESS;
  271. }
  272. return EXIT_FAIL;
  273. }
  274. //=====================================================================
  275. //Function: fabulous (/fabulous)
  276. //Input: Player player: The player using the command
  277. // String[] args: The message to apply the effect to
  278. //Output: int: Exit Code
  279. //Use: Makes the text rainbow colored
  280. //=====================================================================
  281. public static int fabulous(Player player, String[] args)
  282. {
  283. //If the command is enabled
  284. if(vMinecraftSettings.getInstance().cmdFabulous()) {
  285. //Format the name
  286. String playerName = Colors.White + "<"
  287. + vMinecraftChat.getName(player) + Colors.White +"> ";
  288. //Make sure a message has been specified
  289. if (args.length < 1) {return EXIT_FAIL;}
  290. String str = " ";
  291. //Merge the message again
  292. str = etc.combineSplit(0, args, " ");
  293. //Output for server
  294. log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\"");
  295. //Prepend the player name and cut into lines.
  296. vMinecraftChat.gmsg(player, playerName + vMinecraftChat.rainbow(str));
  297. return EXIT_SUCCESS;
  298. }
  299. return EXIT_FAIL;
  300. }
  301. //=====================================================================
  302. //Function: whois (/whois)
  303. //Input: Player player: The player using the command
  304. // String[] args: The player to find info on
  305. //Output: int: Exit Code
  306. //Use: Displays information about the player specified
  307. //=====================================================================
  308. public static int whois(Player player, String[] args)
  309. {
  310. //If the command is enabled
  311. if (vMinecraftSettings.getInstance().cmdWhoIs()) {
  312. //If a player is specified
  313. if (args.length < 1)
  314. player.sendMessage(Colors.Rose + "Usage is /whois [player]");
  315. else {
  316. //Get the player by name
  317. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  318. //If the player exists
  319. if (playerTarget != null){
  320. //Displaying the information
  321. player.sendMessage(Colors.Blue + "Whois results for " +
  322. vMinecraftChat.getName(playerTarget));
  323. //Group
  324. for(String group: playerTarget.getGroups())
  325. player.sendMessage(Colors.Blue + "Groups: " + group);
  326. //Admin
  327. player.sendMessage(Colors.Blue+"Admin: " +
  328. String.valueOf(playerTarget.isAdmin()));
  329. //IP
  330. player.sendMessage(Colors.Blue+"IP: " + playerTarget.getIP());
  331. //Restrictions
  332. player.sendMessage(Colors.Blue+"Can ignore restrictions: " +
  333. String.valueOf(playerTarget.canIgnoreRestrictions()));
  334. //Give the user an error if the player doesn't exist
  335. } else {
  336. player.sendMessage(Colors.Rose+"Player not found.");
  337. }
  338. }
  339. return EXIT_SUCCESS;
  340. }
  341. return EXIT_SUCCESS;
  342. }
  343. //=====================================================================
  344. //Function: who (/who)
  345. //Input: Player player: The player using the command
  346. // String[] args: Ignored
  347. //Output: int: Exit Code
  348. //Use: Displays the connected players
  349. //=====================================================================
  350. public static int who(Player player, String[] args)
  351. {
  352. //If the command is enabled
  353. if (vMinecraftSettings.getInstance().cmdWho()) {
  354. //Loop through all players counting them and adding to the list
  355. int count=0;
  356. String tempList = "";
  357. for( Player p : etc.getServer().getPlayerList())
  358. {
  359. if(p != null){
  360. if(count == 0)
  361. tempList += vMinecraftChat.getName(p);
  362. else
  363. tempList += Colors.White + ", " + vMinecraftChat.getName(p);
  364. count++;
  365. }
  366. }
  367. //Get the max players from the config
  368. PropertiesFile server = new PropertiesFile("server.properties");
  369. try {
  370. server.load();
  371. } catch (IOException e) {
  372. e.printStackTrace();
  373. }
  374. int maxPlayers = server.getInt("max-players");
  375. //Output the player list
  376. vMinecraftChat.sendMessage(player, player, Colors.Rose + "Player List ("
  377. + count + "/" + maxPlayers +"): " + tempList);
  378. return EXIT_SUCCESS;
  379. }
  380. return EXIT_FAIL;
  381. }
  382. //=====================================================================
  383. //Function: say (/say)
  384. //Input: Player player: The player using the command
  385. // String[] args: The message to apply the effect to
  386. //Output: int: Exit Code
  387. //Use: Announces the message to all players
  388. //=====================================================================
  389. public static int say(Player player, String[] args)
  390. {
  391. //If the command is enabled
  392. if (vMinecraftSettings.getInstance().cmdSay()) {
  393. //Make sure a message is supplied or output an error
  394. if (args.length < 1) {
  395. player.sendMessage(Colors.Rose + "Usage is /say [message]");
  396. }
  397. //Display the message globally
  398. vMinecraftChat.gmsg(player, Colors.Yellow + etc.combineSplit(0, args, " "));
  399. return EXIT_SUCCESS;
  400. }
  401. return EXIT_FAIL;
  402. }
  403. //=====================================================================
  404. //Function: slay (/slay)
  405. //Input: Player player: The player using the command
  406. // String[] args: The target for the command
  407. //Output: int: Exit Code
  408. //Use: Kill the target player
  409. //=====================================================================
  410. public static int slay(Player player, String[] args)
  411. {
  412. //Check if the command is enabled
  413. if(vMinecraftSettings.getInstance().cmdEzModo()) {
  414. //Get the player by name
  415. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  416. //If the player doesn't exist don't run
  417. if(playerTarget == null)
  418. return EXIT_FAIL;
  419. //If the player isn't invulnerable kill them
  420. if (!vMinecraftSettings.getInstance().isEzModo(playerTarget.getName())) {
  421. playerTarget.setHealth(0);
  422. vMinecraftChat.gmsg(player, vMinecraftChat.getName(player)
  423. + Colors.LightBlue + " has slain "
  424. + vMinecraftChat.getName(playerTarget));
  425. //Otherwise output error to the user
  426. } else {
  427. player.sendMessage(Colors.Rose + "That player is currently in ezmodo! Hahahaha");
  428. }
  429. return EXIT_SUCCESS;
  430. }
  431. return EXIT_FAIL;
  432. }
  433. //=====================================================================
  434. //Function: invuln (/ezmodo)
  435. //Input: Player player: The player using the command
  436. // String[] args: The target for the command
  437. //Output: int: Exit Code
  438. //Use: Kill the target player
  439. //=====================================================================
  440. public static int invuln(Player player, String[] args)
  441. {
  442. //If the command is enabled
  443. if (vMinecraftSettings.getInstance().cmdEzModo()) {
  444. //If the player is already invulnerable, turn ezmodo off.
  445. if (vMinecraftSettings.getInstance().isEzModo(player.getName())) {
  446. player.sendMessage(Colors.Red + "ezmodo = off");
  447. vMinecraftSettings.getInstance().removeEzModo(player.getName());
  448. //Otherwise make them invulnerable
  449. } else {
  450. player.sendMessage(Colors.LightBlue + "eh- maji? ezmodo!?");
  451. player.sendMessage(Colors.Rose + "kimo-i");
  452. player.sendMessage(Colors.LightBlue + "Easy Mode ga yurusareru no wa shougakusei made dayo ne");
  453. player.sendMessage(Colors.Red + "**Laughter**");
  454. vMinecraftSettings.getInstance().addEzModo(player.getName());
  455. }
  456. return EXIT_SUCCESS;
  457. }
  458. return EXIT_FAIL;
  459. }
  460. //=====================================================================
  461. //Function: ezlist (/ezlist)
  462. //Input: Player player: The player using the command
  463. // String[] args: Ignored
  464. //Output: int: Exit Code
  465. //Use: List all invulnerable players
  466. //=====================================================================
  467. public static int ezlist(Player player, String[] args)
  468. {
  469. //If the feature is enabled list the players
  470. if(vMinecraftSettings.getInstance().cmdEzModo()) {
  471. player.sendMessage("Ezmodo: " + vMinecraftSettings.getInstance().ezModoList());
  472. return EXIT_SUCCESS;
  473. }
  474. return EXIT_FAIL;
  475. }
  476. //=====================================================================
  477. //Function: modifySplit (/modify)
  478. //Input: Player player: The player using the command
  479. // String[] args: Player, Command, Arguments
  480. //Output: int: Exit Code
  481. //Use: List all invulnerable players
  482. //=====================================================================
  483. public static int modifySplit(Player player, String[] args)
  484. {
  485. //Exploit fix for people giving themselves commands
  486. if(args[1].equals("commands"))
  487. return EXIT_FAIL;
  488. return EXIT_CONTINUE;
  489. }
  490. //=====================================================================
  491. //Function: Time Reverse
  492. //Input: long time: The time to reverse to.
  493. //Output: int: Exit Code
  494. //Use: List all invulnerable players
  495. //=====================================================================
  496. public static int timeReverse(long tarTime)
  497. {
  498. long curTime = etc.getServer().getRelativeTime();
  499. //if(cur)
  500. return EXIT_SUCCESS;
  501. }
  502. //=====================================================================
  503. //Function: privateMessage(/msg)
  504. //Input: long time: The time to reverse to.
  505. //Output: int: Exit Code
  506. //Use: List all invulnerable players
  507. //=====================================================================
  508. public static int privateMessage(Player player, String[] message)
  509. {
  510. long curTime = etc.getServer().getRelativeTime();
  511. //if(cur)
  512. return EXIT_SUCCESS;
  513. }
  514. }
  515. //=====================================================================
  516. //Class: commandList
  517. //Use: The list of commands that will be checked for
  518. //Author: cerevisiae
  519. //=====================================================================
  520. class commandList {
  521. command[] commands;
  522. protected static final Logger log = Logger.getLogger("Minecraft");
  523. static final int EXIT_FAIL = 0,
  524. EXIT_SUCCESS = 1,
  525. EXIT_CONTINUE = 2;
  526. //=====================================================================
  527. //Function: commandList
  528. //Input: None
  529. //Output: None
  530. //Use: Initialize the array of commands
  531. //=====================================================================
  532. public commandList(){
  533. commands = new command[0];
  534. }
  535. //=====================================================================
  536. //Function: register
  537. //Input: String name: The name of the command
  538. // String func: The function to be called
  539. //Output: boolean: Whether the command was input successfully or not
  540. //Use: Registers a command to the command list for checking later
  541. //=====================================================================
  542. public boolean register(String name, String func){
  543. //If the command list isn't empty
  544. if(commands.length > 0)
  545. {
  546. //Check to make sure the command doesn't already exist
  547. for(int i = 0; i < commands.length; i++)
  548. if(commands[i].getName().equalsIgnoreCase(name))
  549. return false;
  550. //Create a new temp array
  551. command[] temp = new command[commands.length + 1];
  552. //Copy the old command list over
  553. System.arraycopy(commands, 0, temp, 0, commands.length);
  554. //Set commands to equal the new array
  555. commands = temp;
  556. } else {
  557. commands = new command[1];
  558. }
  559. //Add the new function to the list
  560. commands[commands.length - 1] = new command(name, func);
  561. //exit successfully
  562. return true;
  563. }
  564. //=====================================================================
  565. //Function: register
  566. //Input: String name: The name of the command
  567. // String func: The function to be called
  568. // String info: The information for the command to put in help
  569. //Output: boolean: Whether the command was input successfully or not
  570. //Use: Registers a command to the command list for checking later
  571. //=====================================================================
  572. public boolean register(String name, String func, String info){
  573. //Add to the /help list
  574. etc.getInstance().addCommand(name, info);
  575. //Finish registering
  576. return register(name, func);
  577. }
  578. //=====================================================================
  579. //Function: register
  580. //Input: String name: The name of the command
  581. // String func: The function to be called
  582. //Output: boolean: Whether the command was input successfully or not
  583. //Use: Registers a command to the command list for checking later
  584. //=====================================================================
  585. public boolean registerAlias(String name, String com, String[] args){
  586. //If the command list isn't empty
  587. if(commands.length > 0)
  588. {
  589. //Check to make sure the command doesn't already exist
  590. for(int i = 0; i < commands.length; i++)
  591. if(commands[i].getName().equalsIgnoreCase(name))
  592. return false;
  593. //Create a new temp array
  594. command[] temp = new command[commands.length + 1];
  595. //Copy the old command list over
  596. System.arraycopy(commands, 0, temp, 0, commands.length);
  597. //Set commands to equal the new array
  598. commands = temp;
  599. } else {
  600. commands = new command[1];
  601. }
  602. //Add the new function to the list
  603. commands[commands.length - 1] = new commandRef(name, com, args);
  604. //exit successfully
  605. return true;
  606. }
  607. //=====================================================================
  608. //Function: register
  609. //Input: String name: The name of the command
  610. // String func: The function to be called
  611. //Output: boolean: Whether the command was input successfully or not
  612. //Use: Registers a command to the command list for checking later
  613. //=====================================================================
  614. public boolean registerAlias(String name, String com){
  615. //If the command list isn't empty
  616. if(commands.length > 0)
  617. {
  618. //Check to make sure the command doesn't already exist
  619. for(int i = 0; i < commands.length; i++)
  620. if(commands[i].getName().equalsIgnoreCase(name))
  621. return false;
  622. //Create a new temp array
  623. command[] temp = new command[commands.length + 1];
  624. //Copy the old command list over
  625. System.arraycopy(commands, 0, temp, 0, commands.length);
  626. //Set commands to equal the new array
  627. commands = temp;
  628. } else {
  629. commands = new command[1];
  630. }
  631. //Add the new function to the list
  632. commands[commands.length - 1] = new commandRef(name, com);
  633. //exit successfully
  634. return true;
  635. }
  636. //=====================================================================
  637. //Function: call
  638. //Input: String name: The name of the command to be run
  639. //Output: boolean: If the command was called successfully
  640. //Use: Attempts to call a command
  641. //=====================================================================
  642. public int call(String name, Player player, String[] arg){
  643. //Make sure the user has access to the command
  644. if(!player.canUseCommand(name)) {
  645. return EXIT_FAIL;
  646. }
  647. //Search for the command
  648. for(command cmd : commands)
  649. {
  650. //When found
  651. if(cmd.getName().equalsIgnoreCase(name))
  652. {
  653. try {
  654. //Call the command and return results
  655. return cmd.call(player, arg);
  656. } catch (SecurityException e) {
  657. log.log(Level.SEVERE, "Exception while running command", e);
  658. } catch (IllegalArgumentException e) {
  659. log.log(Level.SEVERE, "The Command Entered Doesn't Exist", e);
  660. return EXIT_FAIL;
  661. }
  662. }
  663. }
  664. //Something went wrong
  665. return EXIT_FAIL;
  666. }
  667. //=====================================================================
  668. //Class: command
  669. //Use: The specific command
  670. //Author: cerevisiae
  671. //=====================================================================
  672. private class command
  673. {
  674. private String commandName;
  675. private String function;
  676. //=====================================================================
  677. //Function: command
  678. //Input: None
  679. //Output: None
  680. //Use: Initialize the command
  681. //=====================================================================
  682. public command(String name, String func){
  683. commandName = name;
  684. function = func;
  685. }
  686. //=====================================================================
  687. //Function: getName
  688. //Input: None
  689. //Output: String: The command name
  690. //Use: Returns the command name
  691. //=====================================================================
  692. public String getName(){return commandName;}
  693. //=====================================================================
  694. //Function: call
  695. //Input: String[] arg: The arguments for the command
  696. //Output: boolean: If the command was called successfully
  697. //Use: Attempts to call the command
  698. //=====================================================================
  699. int call(Player player, String[] arg)
  700. {
  701. Method m;
  702. try {
  703. m = vMinecraftCommands.class.getMethod(function, Player.class, String[].class);
  704. m.setAccessible(true);
  705. return (Integer) m.invoke(null, player, arg);
  706. } catch (SecurityException e) {
  707. e.printStackTrace();
  708. } catch (NoSuchMethodException e) {
  709. e.printStackTrace();
  710. } catch (IllegalArgumentException e) {
  711. e.printStackTrace();
  712. } catch (IllegalAccessException e) {
  713. e.printStackTrace();
  714. } catch (InvocationTargetException e) {
  715. e.printStackTrace();
  716. }
  717. return 1;
  718. }
  719. }
  720. //=====================================================================
  721. //Class: commandRef
  722. //Use: A command referencing another command
  723. //Author: cerevisiae
  724. //=====================================================================
  725. private class commandRef extends command
  726. {
  727. private String reference;
  728. private String[] args;
  729. //=====================================================================
  730. //Function: command
  731. //Input: String name: The command name
  732. // String com: The command to run
  733. // String[] arg: the arguments to apply
  734. //Output: None
  735. //Use: Initialize the command
  736. //=====================================================================
  737. public commandRef(String name, String com, String[] arg){
  738. super(name, "");
  739. reference = com;
  740. args = arg;
  741. }
  742. //=====================================================================
  743. //Function: command
  744. //Input: String name: The command name
  745. // String com: The command to run
  746. //Output: None
  747. //Use: Initialize the command
  748. //=====================================================================
  749. public commandRef(String name, String com){
  750. super(name, "");
  751. reference = com;
  752. args = null;
  753. }
  754. //=====================================================================
  755. //Function: call
  756. //Input: String[] arg: The arguments for the command
  757. //Output: boolean: If the command was called successfully
  758. //Use: Attempts to call the command
  759. //=====================================================================
  760. int call(Player player, String[] arg)
  761. {
  762. if(args != null) {
  763. String[] temp = new String[args.length];
  764. System.arraycopy(args, 0, temp, 0, args.length);
  765. //Insert the arguments into the pre-set arguments
  766. int lastSet = 0,
  767. argCount = 0;
  768. for(String argument : temp)
  769. {
  770. if(argument.startsWith("%"))
  771. {
  772. int argNum = Integer.parseInt(argument.substring(1));
  773. if( argNum < arg.length )
  774. {
  775. temp[lastSet] = arg[argNum];
  776. argCount++;
  777. }
  778. }
  779. lastSet++;
  780. }
  781. //Append the rest of the arguments to the argument array
  782. if(lastSet < temp.length + arg.length - argCount)
  783. {
  784. String[] temp2 = new String[temp.length + arg.length - argCount];
  785. System.arraycopy(temp, 0, temp2, 0, temp.length);
  786. System.arraycopy(arg, argCount, temp2,
  787. temp.length, arg.length - argCount);
  788. temp = temp2;
  789. }
  790. //Call the referenced command
  791. player.command(reference + " " + etc.combineSplit(0, temp, " "));
  792. } else
  793. player.command(reference);
  794. return EXIT_SUCCESS;
  795. }
  796. }
  797. }