vMinecraftCommands.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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("/suicide", "/wrists");
  45. cl.registerAlias("/clearinventory", "/ci");
  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. vMinecraftChat.gmsg(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(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 = null;
  318. for( Player p : etc.getServer().getPlayerList())
  319. {
  320. if (p.getName().equalsIgnoreCase(args[0]))
  321. {
  322. playerTarget = p;
  323. }
  324. }
  325. //If the player exists
  326. if (playerTarget != null){
  327. //Displaying the information
  328. player.sendMessage(Colors.Blue + "Whois results for " +
  329. vMinecraftChat.getName(playerTarget));
  330. //Group
  331. for(String group: playerTarget.getGroups())
  332. player.sendMessage(Colors.Blue + "Groups: " + group);
  333. //Admin
  334. player.sendMessage(Colors.Blue+"Admin: " +
  335. String.valueOf(playerTarget.isAdmin()));
  336. //IP
  337. player.sendMessage(Colors.Blue+"IP: " + playerTarget.getIP());
  338. //Restrictions
  339. player.sendMessage(Colors.Blue+"Can ignore restrictions: " +
  340. String.valueOf(playerTarget.canIgnoreRestrictions()));
  341. //Give the user an error if the player doesn't exist
  342. } else {
  343. player.sendMessage(Colors.Rose+"Player not found.");
  344. }
  345. }
  346. return EXIT_SUCCESS;
  347. }
  348. return EXIT_SUCCESS;
  349. }
  350. //=====================================================================
  351. //Function: who (/who)
  352. //Input: Player player: The player using the command
  353. // String[] args: Ignored
  354. //Output: int: Exit Code
  355. //Use: Displays the connected players
  356. //=====================================================================
  357. public static int who(Player player, String[] args)
  358. {
  359. //If the command is enabled
  360. if (vMinecraftSettings.getInstance().cmdWho()) {
  361. //Loop through all players counting them and adding to the list
  362. int count=0;
  363. String tempList = "";
  364. for( Player p : etc.getServer().getPlayerList())
  365. {
  366. if(p != null){
  367. if(count == 0)
  368. tempList += vMinecraftChat.getName(p);
  369. else
  370. tempList += Colors.White + ", " + vMinecraftChat.getName(p);
  371. count++;
  372. }
  373. }
  374. //Get the max players from the config
  375. PropertiesFile server = new PropertiesFile("server.properties");
  376. try {
  377. server.load();
  378. } catch (IOException e) {
  379. e.printStackTrace();
  380. }
  381. int maxPlayers = server.getInt("max-players");
  382. //Output the player list
  383. vMinecraftChat.sendMessage(player, Colors.Rose + "Player List ("
  384. + count + "/" + maxPlayers +"): " + tempList);
  385. return EXIT_SUCCESS;
  386. }
  387. return EXIT_FAIL;
  388. }
  389. //=====================================================================
  390. //Function: say (/say)
  391. //Input: Player player: The player using the command
  392. // String[] args: The message to apply the effect to
  393. //Output: int: Exit Code
  394. //Use: Announces the message to all players
  395. //=====================================================================
  396. public static int say(Player player, String[] args)
  397. {
  398. //If the command is enabled
  399. if (vMinecraftSettings.getInstance().cmdSay()) {
  400. //Make sure a message is supplied or output an error
  401. if (args.length < 1) {
  402. player.sendMessage(Colors.Rose + "Usage is /say [message]");
  403. }
  404. //Display the message globally
  405. vMinecraftChat.gmsg(Colors.Yellow + etc.combineSplit(0, args, " "));
  406. return EXIT_SUCCESS;
  407. }
  408. return EXIT_FAIL;
  409. }
  410. //=====================================================================
  411. //Function: slay (/slay)
  412. //Input: Player player: The player using the command
  413. // String[] args: The target for the command
  414. //Output: int: Exit Code
  415. //Use: Kill the target player
  416. //=====================================================================
  417. public static int slay(Player player, String[] args)
  418. {
  419. //Check if the command is enabled
  420. if(vMinecraftSettings.getInstance().cmdEzModo()) {
  421. //Get the player by name
  422. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  423. //If the player doesn't exist don't run
  424. if(playerTarget == null)
  425. return EXIT_FAIL;
  426. //If the player isn't invulnerable kill them
  427. if (!vMinecraftSettings.getInstance().isEzModo(playerTarget.getName())) {
  428. playerTarget.setHealth(0);
  429. vMinecraftChat.gmsg(vMinecraftChat.getName(player)
  430. + Colors.LightBlue + " has slain "
  431. + vMinecraftChat.getName(playerTarget));
  432. //Otherwise output error to the user
  433. } else {
  434. player.sendMessage(Colors.Rose + "That player is currently in ezmodo! Hahahaha");
  435. }
  436. return EXIT_SUCCESS;
  437. }
  438. return EXIT_FAIL;
  439. }
  440. //=====================================================================
  441. //Function: invuln (/ezmodo)
  442. //Input: Player player: The player using the command
  443. // String[] args: The target for the command
  444. //Output: int: Exit Code
  445. //Use: Kill the target player
  446. //=====================================================================
  447. public static int invuln(Player player, String[] args)
  448. {
  449. //If the command is enabled
  450. if (vMinecraftSettings.getInstance().cmdEzModo()) {
  451. //If the player is already invulnerable, turn ezmodo off.
  452. if (vMinecraftSettings.getInstance().isEzModo(player.getName())) {
  453. player.sendMessage(Colors.Red + "ezmodo = off");
  454. vMinecraftSettings.getInstance().removeEzModo(player.getName());
  455. //Otherwise make them invulnerable
  456. } else {
  457. player.sendMessage(Colors.LightBlue + "eh- maji? ezmodo!?");
  458. player.sendMessage(Colors.Rose + "kimo-i");
  459. player.sendMessage(Colors.LightBlue + "Easy Mode ga yurusareru no wa shougakusei made dayo ne");
  460. player.sendMessage(Colors.Red + "**Laughter**");
  461. vMinecraftSettings.getInstance().addEzModo(player.getName());
  462. player.setHealth(vMinecraftSettings.getInstance().ezModoHealth());
  463. }
  464. return EXIT_SUCCESS;
  465. }
  466. return EXIT_FAIL;
  467. }
  468. //=====================================================================
  469. //Function: ezlist (/ezlist)
  470. //Input: Player player: The player using the command
  471. // String[] args: Ignored
  472. //Output: int: Exit Code
  473. //Use: List all invulnerable players
  474. //=====================================================================
  475. public static int ezlist(Player player, String[] args)
  476. {
  477. //If the feature is enabled list the players
  478. if(vMinecraftSettings.getInstance().cmdEzModo()) {
  479. player.sendMessage("Ezmodo: " + vMinecraftSettings.getInstance().ezModoList());
  480. return EXIT_SUCCESS;
  481. }
  482. return EXIT_FAIL;
  483. }
  484. //=====================================================================
  485. //Function: modifySplit (/modify)
  486. //Input: Player player: The player using the command
  487. // String[] args: Player, Command, Arguments
  488. //Output: int: Exit Code
  489. //Use: List all invulnerable players
  490. //=====================================================================
  491. public static int modifySplit(Player player, String[] args)
  492. {
  493. //Exploit fix for people giving themselves commands
  494. if(args[1].equals("commands"))
  495. return EXIT_FAIL;
  496. return EXIT_CONTINUE;
  497. }
  498. //=====================================================================
  499. //Function: Time Reverse
  500. //Input: long time: The time to reverse to.
  501. //Output: int: Exit Code
  502. //Use: List all invulnerable players
  503. //=====================================================================
  504. public static int timeReverse(long tarTime)
  505. {
  506. long curTime = etc.getServer().getRelativeTime();
  507. //if(cur)
  508. return EXIT_SUCCESS;
  509. }
  510. }
  511. //=====================================================================
  512. //Class: commandList
  513. //Use: The list of commands that will be checked for
  514. //Author: cerevisiae
  515. //=====================================================================
  516. class commandList {
  517. command[] commands;
  518. protected static final Logger log = Logger.getLogger("Minecraft");
  519. static final int EXIT_FAIL = 0,
  520. EXIT_SUCCESS = 1,
  521. EXIT_CONTINUE = 2;
  522. //=====================================================================
  523. //Function: commandList
  524. //Input: None
  525. //Output: None
  526. //Use: Initialize the array of commands
  527. //=====================================================================
  528. public commandList(){
  529. commands = new command[0];
  530. }
  531. //=====================================================================
  532. //Function: register
  533. //Input: String name: The name of the command
  534. // String func: The function to be called
  535. //Output: boolean: Whether the command was input successfully or not
  536. //Use: Registers a command to the command list for checking later
  537. //=====================================================================
  538. public boolean register(String name, String func){
  539. //If the command list isn't empty
  540. if(commands.length > 0)
  541. {
  542. //Check to make sure the command doesn't already exist
  543. for(int i = 0; i < commands.length; i++)
  544. if(commands[i].getName().equalsIgnoreCase(name))
  545. return false;
  546. //Create a new temp array
  547. command[] temp = new command[commands.length + 1];
  548. //Copy the old command list over
  549. System.arraycopy(commands, 0, temp, 0, commands.length);
  550. //Set commands to equal the new array
  551. commands = temp;
  552. } else {
  553. commands = new command[1];
  554. }
  555. //Add the new function to the list
  556. commands[commands.length - 1] = new command(name, func);
  557. //exit successfully
  558. return true;
  559. }
  560. //=====================================================================
  561. //Function: register
  562. //Input: String name: The name of the command
  563. // String func: The function to be called
  564. // String info: The information for the command to put in help
  565. //Output: boolean: Whether the command was input successfully or not
  566. //Use: Registers a command to the command list for checking later
  567. //=====================================================================
  568. public boolean register(String name, String func, String info){
  569. //Add to the /help list
  570. etc.getInstance().addCommand(name, info);
  571. //Finish registering
  572. return register(name, func);
  573. }
  574. //=====================================================================
  575. //Function: register
  576. //Input: String name: The name of the command
  577. // String func: The function to be called
  578. //Output: boolean: Whether the command was input successfully or not
  579. //Use: Registers a command to the command list for checking later
  580. //=====================================================================
  581. public boolean registerAlias(String name, String com, String[] args){
  582. //If the command list isn't empty
  583. if(commands.length > 0)
  584. {
  585. //Check to make sure the command doesn't already exist
  586. for(int i = 0; i < commands.length; i++)
  587. if(commands[i].getName().equalsIgnoreCase(name))
  588. return false;
  589. //Create a new temp array
  590. command[] temp = new command[commands.length + 1];
  591. //Copy the old command list over
  592. System.arraycopy(commands, 0, temp, 0, commands.length);
  593. //Set commands to equal the new array
  594. commands = temp;
  595. } else {
  596. commands = new command[1];
  597. }
  598. //Add the new function to the list
  599. commands[commands.length - 1] = new commandRef(name, com, args);
  600. //exit successfully
  601. return true;
  602. }
  603. //=====================================================================
  604. //Function: register
  605. //Input: String name: The name of the command
  606. // String func: The function to be called
  607. //Output: boolean: Whether the command was input successfully or not
  608. //Use: Registers a command to the command list for checking later
  609. //=====================================================================
  610. public boolean registerAlias(String name, String com){
  611. //If the command list isn't empty
  612. if(commands.length > 0)
  613. {
  614. //Check to make sure the command doesn't already exist
  615. for(int i = 0; i < commands.length; i++)
  616. if(commands[i].getName().equalsIgnoreCase(name))
  617. return false;
  618. //Create a new temp array
  619. command[] temp = new command[commands.length + 1];
  620. //Copy the old command list over
  621. System.arraycopy(commands, 0, temp, 0, commands.length);
  622. //Set commands to equal the new array
  623. commands = temp;
  624. } else {
  625. commands = new command[1];
  626. }
  627. //Add the new function to the list
  628. commands[commands.length - 1] = new commandRef(name, com);
  629. //exit successfully
  630. return true;
  631. }
  632. //=====================================================================
  633. //Function: call
  634. //Input: String name: The name of the command to be run
  635. //Output: boolean: If the command was called successfully
  636. //Use: Attempts to call a command
  637. //=====================================================================
  638. public int call(String name, Player player, String[] arg){
  639. //Make sure the user has access to the command
  640. if(!player.canUseCommand(name)) {
  641. return EXIT_FAIL;
  642. }
  643. //Search for the command
  644. for(command cmd : commands)
  645. {
  646. //When found
  647. if(cmd.getName().equalsIgnoreCase(name))
  648. {
  649. try {
  650. //Call the command and return results
  651. return cmd.call(player, arg);
  652. } catch (SecurityException e) {
  653. log.log(Level.SEVERE, "Exception while running command", e);
  654. } catch (IllegalArgumentException e) {
  655. log.log(Level.SEVERE, "The Command Entered Doesn't Exist", e);
  656. return EXIT_FAIL;
  657. }
  658. }
  659. }
  660. //Something went wrong
  661. return EXIT_FAIL;
  662. }
  663. //=====================================================================
  664. //Class: command
  665. //Use: The specific command
  666. //Author: cerevisiae
  667. //=====================================================================
  668. private class command
  669. {
  670. private String commandName;
  671. private String function;
  672. //=====================================================================
  673. //Function: command
  674. //Input: None
  675. //Output: None
  676. //Use: Initialize the command
  677. //=====================================================================
  678. public command(String name, String func){
  679. commandName = name;
  680. function = func;
  681. }
  682. //=====================================================================
  683. //Function: getName
  684. //Input: None
  685. //Output: String: The command name
  686. //Use: Returns the command name
  687. //=====================================================================
  688. public String getName(){return commandName;}
  689. //=====================================================================
  690. //Function: call
  691. //Input: String[] arg: The arguments for the command
  692. //Output: boolean: If the command was called successfully
  693. //Use: Attempts to call the command
  694. //=====================================================================
  695. int call(Player player, String[] arg)
  696. {
  697. Method m;
  698. try {
  699. m = vMinecraftCommands.class.getMethod(function, Player.class, String[].class);
  700. m.setAccessible(true);
  701. return (Integer) m.invoke(null, player, arg);
  702. } catch (SecurityException e) {
  703. e.printStackTrace();
  704. } catch (NoSuchMethodException e) {
  705. e.printStackTrace();
  706. } catch (IllegalArgumentException e) {
  707. e.printStackTrace();
  708. } catch (IllegalAccessException e) {
  709. e.printStackTrace();
  710. } catch (InvocationTargetException e) {
  711. e.printStackTrace();
  712. }
  713. return 1;
  714. }
  715. }
  716. //=====================================================================
  717. //Class: commandRef
  718. //Use: A command referencing another command
  719. //Author: cerevisiae
  720. //=====================================================================
  721. private class commandRef extends command
  722. {
  723. private String reference;
  724. private String[] args;
  725. //=====================================================================
  726. //Function: command
  727. //Input: String name: The command name
  728. // String com: The command to run
  729. // String[] arg: the arguments to apply
  730. //Output: None
  731. //Use: Initialize the command
  732. //=====================================================================
  733. public commandRef(String name, String com, String[] arg){
  734. super(name, "");
  735. reference = com;
  736. args = arg;
  737. }
  738. //=====================================================================
  739. //Function: command
  740. //Input: String name: The command name
  741. // String com: The command to run
  742. //Output: None
  743. //Use: Initialize the command
  744. //=====================================================================
  745. public commandRef(String name, String com){
  746. super(name, "");
  747. reference = com;
  748. args = null;
  749. }
  750. //=====================================================================
  751. //Function: call
  752. //Input: String[] arg: The arguments for the command
  753. //Output: boolean: If the command was called successfully
  754. //Use: Attempts to call the command
  755. //=====================================================================
  756. int call(Player player, String[] arg)
  757. {
  758. if(args != null) {
  759. String[] temp = new String[args.length];
  760. System.arraycopy(args, 0, temp, 0, args.length);
  761. //Insert the arguments into the pre-set arguments
  762. int lastSet = 0,
  763. argCount = 0;
  764. for(String argument : temp)
  765. {
  766. if(argument.startsWith("%"))
  767. {
  768. int argNum = Integer.parseInt(argument.substring(1));
  769. if( argNum < arg.length )
  770. {
  771. temp[lastSet] = arg[argNum];
  772. argCount++;
  773. }
  774. }
  775. lastSet++;
  776. }
  777. //Append the rest of the arguments to the argument array
  778. if(lastSet < temp.length + arg.length - argCount)
  779. {
  780. String[] temp2 = new String[temp.length + arg.length - argCount];
  781. System.arraycopy(temp, 0, temp2, 0, temp.length);
  782. System.arraycopy(arg, argCount, temp2,
  783. temp.length, arg.length - argCount);
  784. temp = temp2;
  785. }
  786. //Call the referenced command
  787. player.command(reference + " " + etc.combineSplit(0, temp, " "));
  788. } else
  789. player.command(reference);
  790. return EXIT_SUCCESS;
  791. }
  792. }
  793. }