vMinecraftCommands.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. player.setHealth(vMinecraftSettings.getInstance().ezModoHealth());
  456. }
  457. return EXIT_SUCCESS;
  458. }
  459. return EXIT_FAIL;
  460. }
  461. //=====================================================================
  462. //Function: ezlist (/ezlist)
  463. //Input: Player player: The player using the command
  464. // String[] args: Ignored
  465. //Output: int: Exit Code
  466. //Use: List all invulnerable players
  467. //=====================================================================
  468. public static int ezlist(Player player, String[] args)
  469. {
  470. //If the feature is enabled list the players
  471. if(vMinecraftSettings.getInstance().cmdEzModo()) {
  472. player.sendMessage("Ezmodo: " + vMinecraftSettings.getInstance().ezModoList());
  473. return EXIT_SUCCESS;
  474. }
  475. return EXIT_FAIL;
  476. }
  477. //=====================================================================
  478. //Function: modifySplit (/modify)
  479. //Input: Player player: The player using the command
  480. // String[] args: Player, Command, Arguments
  481. //Output: int: Exit Code
  482. //Use: List all invulnerable players
  483. //=====================================================================
  484. public static int modifySplit(Player player, String[] args)
  485. {
  486. //Exploit fix for people giving themselves commands
  487. if(args[1].equals("commands"))
  488. return EXIT_FAIL;
  489. return EXIT_CONTINUE;
  490. }
  491. //=====================================================================
  492. //Function: Time Reverse
  493. //Input: long time: The time to reverse to.
  494. //Output: int: Exit Code
  495. //Use: List all invulnerable players
  496. //=====================================================================
  497. public static int timeReverse(long tarTime)
  498. {
  499. long curTime = etc.getServer().getRelativeTime();
  500. //if(cur)
  501. return EXIT_SUCCESS;
  502. }
  503. }
  504. //=====================================================================
  505. //Class: commandList
  506. //Use: The list of commands that will be checked for
  507. //Author: cerevisiae
  508. //=====================================================================
  509. class commandList {
  510. command[] commands;
  511. protected static final Logger log = Logger.getLogger("Minecraft");
  512. static final int EXIT_FAIL = 0,
  513. EXIT_SUCCESS = 1,
  514. EXIT_CONTINUE = 2;
  515. //=====================================================================
  516. //Function: commandList
  517. //Input: None
  518. //Output: None
  519. //Use: Initialize the array of commands
  520. //=====================================================================
  521. public commandList(){
  522. commands = new command[0];
  523. }
  524. //=====================================================================
  525. //Function: register
  526. //Input: String name: The name of the command
  527. // String func: The function to be called
  528. //Output: boolean: Whether the command was input successfully or not
  529. //Use: Registers a command to the command list for checking later
  530. //=====================================================================
  531. public boolean register(String name, String func){
  532. //If the command list isn't empty
  533. if(commands.length > 0)
  534. {
  535. //Check to make sure the command doesn't already exist
  536. for(int i = 0; i < commands.length; i++)
  537. if(commands[i].getName().equalsIgnoreCase(name))
  538. return false;
  539. //Create a new temp array
  540. command[] temp = new command[commands.length + 1];
  541. //Copy the old command list over
  542. System.arraycopy(commands, 0, temp, 0, commands.length);
  543. //Set commands to equal the new array
  544. commands = temp;
  545. } else {
  546. commands = new command[1];
  547. }
  548. //Add the new function to the list
  549. commands[commands.length - 1] = new command(name, func);
  550. //exit successfully
  551. return true;
  552. }
  553. //=====================================================================
  554. //Function: register
  555. //Input: String name: The name of the command
  556. // String func: The function to be called
  557. // String info: The information for the command to put in help
  558. //Output: boolean: Whether the command was input successfully or not
  559. //Use: Registers a command to the command list for checking later
  560. //=====================================================================
  561. public boolean register(String name, String func, String info){
  562. //Add to the /help list
  563. etc.getInstance().addCommand(name, info);
  564. //Finish registering
  565. return register(name, func);
  566. }
  567. //=====================================================================
  568. //Function: register
  569. //Input: String name: The name of the command
  570. // String func: The function to be called
  571. //Output: boolean: Whether the command was input successfully or not
  572. //Use: Registers a command to the command list for checking later
  573. //=====================================================================
  574. public boolean registerAlias(String name, String com, String[] args){
  575. //If the command list isn't empty
  576. if(commands.length > 0)
  577. {
  578. //Check to make sure the command doesn't already exist
  579. for(int i = 0; i < commands.length; i++)
  580. if(commands[i].getName().equalsIgnoreCase(name))
  581. return false;
  582. //Create a new temp array
  583. command[] temp = new command[commands.length + 1];
  584. //Copy the old command list over
  585. System.arraycopy(commands, 0, temp, 0, commands.length);
  586. //Set commands to equal the new array
  587. commands = temp;
  588. } else {
  589. commands = new command[1];
  590. }
  591. //Add the new function to the list
  592. commands[commands.length - 1] = new commandRef(name, com, args);
  593. //exit successfully
  594. return true;
  595. }
  596. //=====================================================================
  597. //Function: register
  598. //Input: String name: The name of the command
  599. // String func: The function to be called
  600. //Output: boolean: Whether the command was input successfully or not
  601. //Use: Registers a command to the command list for checking later
  602. //=====================================================================
  603. public boolean registerAlias(String name, String com){
  604. //If the command list isn't empty
  605. if(commands.length > 0)
  606. {
  607. //Check to make sure the command doesn't already exist
  608. for(int i = 0; i < commands.length; i++)
  609. if(commands[i].getName().equalsIgnoreCase(name))
  610. return false;
  611. //Create a new temp array
  612. command[] temp = new command[commands.length + 1];
  613. //Copy the old command list over
  614. System.arraycopy(commands, 0, temp, 0, commands.length);
  615. //Set commands to equal the new array
  616. commands = temp;
  617. } else {
  618. commands = new command[1];
  619. }
  620. //Add the new function to the list
  621. commands[commands.length - 1] = new commandRef(name, com);
  622. //exit successfully
  623. return true;
  624. }
  625. //=====================================================================
  626. //Function: call
  627. //Input: String name: The name of the command to be run
  628. //Output: boolean: If the command was called successfully
  629. //Use: Attempts to call a command
  630. //=====================================================================
  631. public int call(String name, Player player, String[] arg){
  632. //Make sure the user has access to the command
  633. if(!player.canUseCommand(name)) {
  634. return EXIT_FAIL;
  635. }
  636. //Search for the command
  637. for(command cmd : commands)
  638. {
  639. //When found
  640. if(cmd.getName().equalsIgnoreCase(name))
  641. {
  642. try {
  643. //Call the command and return results
  644. return cmd.call(player, arg);
  645. } catch (SecurityException e) {
  646. log.log(Level.SEVERE, "Exception while running command", e);
  647. } catch (IllegalArgumentException e) {
  648. log.log(Level.SEVERE, "The Command Entered Doesn't Exist", e);
  649. return EXIT_FAIL;
  650. }
  651. }
  652. }
  653. //Something went wrong
  654. return EXIT_FAIL;
  655. }
  656. //=====================================================================
  657. //Class: command
  658. //Use: The specific command
  659. //Author: cerevisiae
  660. //=====================================================================
  661. private class command
  662. {
  663. private String commandName;
  664. private String function;
  665. //=====================================================================
  666. //Function: command
  667. //Input: None
  668. //Output: None
  669. //Use: Initialize the command
  670. //=====================================================================
  671. public command(String name, String func){
  672. commandName = name;
  673. function = func;
  674. }
  675. //=====================================================================
  676. //Function: getName
  677. //Input: None
  678. //Output: String: The command name
  679. //Use: Returns the command name
  680. //=====================================================================
  681. public String getName(){return commandName;}
  682. //=====================================================================
  683. //Function: call
  684. //Input: String[] arg: The arguments for the command
  685. //Output: boolean: If the command was called successfully
  686. //Use: Attempts to call the command
  687. //=====================================================================
  688. int call(Player player, String[] arg)
  689. {
  690. Method m;
  691. try {
  692. m = vMinecraftCommands.class.getMethod(function, Player.class, String[].class);
  693. m.setAccessible(true);
  694. return (Integer) m.invoke(null, player, arg);
  695. } catch (SecurityException e) {
  696. e.printStackTrace();
  697. } catch (NoSuchMethodException e) {
  698. e.printStackTrace();
  699. } catch (IllegalArgumentException e) {
  700. e.printStackTrace();
  701. } catch (IllegalAccessException e) {
  702. e.printStackTrace();
  703. } catch (InvocationTargetException e) {
  704. e.printStackTrace();
  705. }
  706. return 1;
  707. }
  708. }
  709. //=====================================================================
  710. //Class: commandRef
  711. //Use: A command referencing another command
  712. //Author: cerevisiae
  713. //=====================================================================
  714. private class commandRef extends command
  715. {
  716. private String reference;
  717. private String[] args;
  718. //=====================================================================
  719. //Function: command
  720. //Input: String name: The command name
  721. // String com: The command to run
  722. // String[] arg: the arguments to apply
  723. //Output: None
  724. //Use: Initialize the command
  725. //=====================================================================
  726. public commandRef(String name, String com, String[] arg){
  727. super(name, "");
  728. reference = com;
  729. args = arg;
  730. }
  731. //=====================================================================
  732. //Function: command
  733. //Input: String name: The command name
  734. // String com: The command to run
  735. //Output: None
  736. //Use: Initialize the command
  737. //=====================================================================
  738. public commandRef(String name, String com){
  739. super(name, "");
  740. reference = com;
  741. args = null;
  742. }
  743. //=====================================================================
  744. //Function: call
  745. //Input: String[] arg: The arguments for the command
  746. //Output: boolean: If the command was called successfully
  747. //Use: Attempts to call the command
  748. //=====================================================================
  749. int call(Player player, String[] arg)
  750. {
  751. if(args != null) {
  752. String[] temp = new String[args.length];
  753. System.arraycopy(args, 0, temp, 0, args.length);
  754. //Insert the arguments into the pre-set arguments
  755. int lastSet = 0,
  756. argCount = 0;
  757. for(String argument : temp)
  758. {
  759. if(argument.startsWith("%"))
  760. {
  761. int argNum = Integer.parseInt(argument.substring(1));
  762. if( argNum < arg.length )
  763. {
  764. temp[lastSet] = arg[argNum];
  765. argCount++;
  766. }
  767. }
  768. lastSet++;
  769. }
  770. //Append the rest of the arguments to the argument array
  771. if(lastSet < temp.length + arg.length - argCount)
  772. {
  773. String[] temp2 = new String[temp.length + arg.length - argCount];
  774. System.arraycopy(temp, 0, temp2, 0, temp.length);
  775. System.arraycopy(arg, argCount, temp2,
  776. temp.length, arg.length - argCount);
  777. temp = temp2;
  778. }
  779. //Call the referenced command
  780. player.command(reference + " " + etc.combineSplit(0, temp, " "));
  781. } else
  782. player.command(reference);
  783. return EXIT_SUCCESS;
  784. }
  785. }
  786. }