vMinecraftCommands.java 30 KB

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