vminecraftCommands.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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("/wrists", "/suicide");
  44. cl.registerAlias("/ci", "/clearinventory");
  45. }
  46. public static int adminChatToggle(Player player, String[] args)
  47. {
  48. if(vminecraftSettings.getInstance().adminChatToggle())
  49. {
  50. if (vminecraftSettings.getInstance().cmdAdminToggle) {
  51. //If the player is already toggled for admin chat, remove them
  52. if (vminecraftSettings.getInstance().isAdminToggled(player.getName())) {
  53. player.sendMessage(Colors.Red + "Admin Chat Toggle = off");
  54. vminecraftSettings.getInstance().removeAdminToggled(player.getName());
  55. //Otherwise include them
  56. } else {
  57. player.sendMessage(Colors.Blue + "Admin Chat Toggled on");
  58. vminecraftSettings.getInstance().addAdminToggled(player.getName());
  59. }
  60. return EXIT_SUCCESS;
  61. }
  62. }
  63. return EXIT_FAIL;
  64. }
  65. //=====================================================================
  66. //Function: heal (/heal)
  67. //Input: Player player: The player using the command
  68. // String[] args: The arguments for the command. Should be a
  69. // player name or blank
  70. //Output: int: Exit Code
  71. //Use: Heals yourself or a specified player.
  72. //=====================================================================
  73. public static int heal(Player player, String[] args)
  74. {
  75. if(vminecraftSettings.getInstance().cmdHeal())
  76. {
  77. //If a target wasn't specified, heal the user.
  78. if (args.length < 1){
  79. player.setHealth(20);
  80. player.sendMessage("Your health is restored");
  81. //If a target was specified, try to find them and then heal them
  82. //Otherwise report the error
  83. } else if (args.length > 0){
  84. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  85. if (playerTarget != null){
  86. playerTarget.setHealth(20);
  87. player.sendMessage(Colors.Blue + "You have healed " + vminecraftChat.getName(playerTarget));
  88. playerTarget.sendMessage(Colors.Blue + "You have been healed by " + vminecraftChat.getName(player));
  89. }
  90. else if (playerTarget == null){
  91. vminecraftChat.gmsg(Colors.Rose + "Couldn't find that player");
  92. }
  93. }
  94. return EXIT_SUCCESS;
  95. }
  96. return EXIT_FAIL;
  97. }
  98. //=====================================================================
  99. //Function: suicide (/suicide, /wrists)
  100. //Input: Player player: The player using the command
  101. // String[] args: Ignored
  102. //Output: int: Exit Code
  103. //Use: Kills yourself
  104. //=====================================================================
  105. public static int suicide(Player player, String[] args)
  106. {
  107. if(vminecraftSettings.getInstance().cmdSuicide())
  108. {
  109. //Set your health to 0. Not much to it.
  110. player.setHealth(0);
  111. return EXIT_SUCCESS;
  112. }
  113. return EXIT_FAIL;
  114. }
  115. //=====================================================================
  116. //Function: teleport (/tp)
  117. //Input: Player player: The player using the command
  118. // String[] args: The arguments for the command. Should be a
  119. // player name
  120. //Output: int: Exit Code
  121. //Use: Teleports the user to another player
  122. //=====================================================================
  123. public static int teleport(Player player, String[] args)
  124. {
  125. //Get if the command is enabled
  126. if(vminecraftSettings.getInstance().cmdTp())
  127. {
  128. //Make sure a player has been specified and return an error if not
  129. if (args.length < 1) {
  130. player.sendMessage(Colors.Rose + "Correct usage is: /tp [player]");
  131. } else {
  132. //Find the player by name
  133. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  134. //Target player isn't found
  135. if(playerTarget == null)
  136. player.sendMessage(Colors.Rose + "Can't find user "
  137. + args[0] + ".");
  138. //If it's you, return witty message
  139. else if (player.getName().equalsIgnoreCase(args[0]))
  140. player.sendMessage(Colors.Rose + "You're already here!");
  141. //If the player is higher rank than you, inform the user
  142. else if (!player.hasControlOver(playerTarget))
  143. player.sendMessage(Colors.Red +
  144. "That player has higher permissions than you.");
  145. //If the player exists transport the user to the player
  146. else {
  147. log.log(Level.INFO, player.getName() + " teleported to " +
  148. playerTarget.getName());
  149. player.teleportTo(playerTarget);
  150. //Otherwise inform the user that the player doesn't exist
  151. }
  152. }
  153. return EXIT_SUCCESS;
  154. }
  155. return EXIT_FAIL;
  156. }
  157. //=====================================================================
  158. //Function: masstp (/masstp)
  159. //Input: Player player: The player using the command
  160. // String[] args: Should be empty or is ignored
  161. //Output: int: Exit Code
  162. //Use: Teleports all players to the user
  163. //=====================================================================
  164. public static int masstp(Player player, String[] args)
  165. {
  166. //If the command is enabled
  167. if(vminecraftSettings.getInstance().cmdMasstp()) {
  168. //Go through all players and move them to the user
  169. for (Player p : etc.getServer().getPlayerList()) {
  170. if (!p.hasControlOver(player)) {
  171. p.teleportTo(player);
  172. }
  173. }
  174. //Inform the user that the command has executed successfully
  175. player.sendMessage(Colors.Blue+"Summoning successful.");
  176. return EXIT_SUCCESS;
  177. }
  178. return EXIT_FAIL;
  179. }
  180. //=====================================================================
  181. //Function: tphere (/tphere)
  182. //Input: Player player: The player using the command
  183. // String[] args: The arguments for the command. Should be a
  184. // player name
  185. //Output: int: Exit Code
  186. //Use: Teleports the user to another player
  187. //=====================================================================
  188. public static int tphere(Player player, String[] args)
  189. {
  190. //Check if the command is enabled.
  191. if (vminecraftSettings.getInstance().cmdTphere()) {
  192. //Make sure a player is specified
  193. if (args.length < 1) {
  194. player.sendMessage(Colors.Rose + "Correct usage is: /tphere [player]");
  195. } else {
  196. //Get the player by name
  197. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  198. //If the target doesn't exist
  199. if(playerTarget == null)
  200. player.sendMessage(Colors.Rose + "Can't find user " + args[0] + ".");
  201. //If the player has a higher rank than the user, return error
  202. else if (!player.hasControlOver(playerTarget))
  203. player.sendMessage(Colors.Red + "That player has higher permissions than you.");
  204. //If the user teleports themselves, mock them
  205. else if (player.getName().equalsIgnoreCase(args[0]))
  206. player.sendMessage(Colors.Rose + "Wow look at that! You teleported yourself to yourself!");
  207. //If the target exists, teleport them to the user
  208. else {
  209. log.log(Level.INFO, player.getName() + " teleported " + player.getName() + " to their self.");
  210. playerTarget.teleportTo(player);
  211. }
  212. }
  213. return EXIT_SUCCESS;
  214. }
  215. return EXIT_FAIL;
  216. }
  217. //=====================================================================
  218. //Function: reload (/reload)
  219. //Input: Player player: The player using the command
  220. // String[] args: Ignored
  221. //Output: int: Exit Code
  222. //Use: Reloads the settings for vminecraft
  223. //=====================================================================
  224. public static int reload(Player player, String[] args)
  225. {
  226. vminecraftSettings.getInstance().loadSettings();
  227. return EXIT_FAIL;
  228. }
  229. //=====================================================================
  230. //Function: rules (/rules)
  231. //Input: Player player: The player using the command
  232. // String[] args: Ignored
  233. //Output: int: Exit Code
  234. //Use: Lists the rules
  235. //=====================================================================
  236. public static int rules(Player player, String[] args)
  237. {
  238. //If the rules exist
  239. if(vminecraftSettings.getInstance().cmdRules()
  240. && vminecraftSettings.getInstance().getRules().length > 0) {
  241. //Apply QuakeCode Colors to the rules
  242. String[] rules = vminecraftChat.applyColors(
  243. vminecraftSettings.getInstance().getRules());
  244. //Display them
  245. for (String str : rules ) {
  246. if(!str.isEmpty())
  247. player.sendMessage(Colors.Blue + str);
  248. else
  249. player.sendMessage(Colors.Blue + "!!!The Rules Have Not Been Set!!!");
  250. }
  251. return EXIT_SUCCESS;
  252. }
  253. return EXIT_FAIL;
  254. }
  255. //=====================================================================
  256. //Function: fabulous (/fabulous)
  257. //Input: Player player: The player using the command
  258. // String[] args: The message to apply the effect to
  259. //Output: int: Exit Code
  260. //Use: Makes the text rainbow colored
  261. //=====================================================================
  262. public static int fabulous(Player player, String[] args)
  263. {
  264. //If the command is enabled
  265. if(vminecraftSettings.getInstance().cmdFabulous()) {
  266. //Format the name
  267. String playerName = Colors.White + "<"
  268. + vminecraftChat.getName(player) + Colors.White +"> ";
  269. //Make sure a message has been specified
  270. if (args.length < 1) {return EXIT_FAIL;}
  271. String str = " ";
  272. //Merge the message again
  273. str = etc.combineSplit(0, args, " ");
  274. //Output for server
  275. log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\"");
  276. //Prepend the player name and cut into lines.
  277. String[] message = vminecraftChat.wordWrap(playerName + str);
  278. //Output the message
  279. for(String msg: message)
  280. {
  281. if (msg.contains(playerName))
  282. vminecraftChat.gmsg( playerName
  283. + vminecraftChat.rainbow(
  284. msg.substring(playerName.length())));
  285. else
  286. vminecraftChat.gmsg(vminecraftChat.rainbow(msg));
  287. }
  288. return EXIT_SUCCESS;
  289. }
  290. return EXIT_FAIL;
  291. }
  292. //=====================================================================
  293. //Function: whois (/whois)
  294. //Input: Player player: The player using the command
  295. // String[] args: The player to find info on
  296. //Output: int: Exit Code
  297. //Use: Displays information about the player specified
  298. //=====================================================================
  299. public static int whois(Player player, String[] args)
  300. {
  301. //If the command is enabled
  302. if (vminecraftSettings.getInstance().cmdWhoIs()) {
  303. //If a player is specified
  304. if (args.length < 1)
  305. player.sendMessage(Colors.Rose + "Usage is /whois [player]");
  306. else {
  307. //Get the player by name
  308. Player playerTarget = null;
  309. for( Player p : etc.getServer().getPlayerList())
  310. {
  311. if (p.getName().equalsIgnoreCase(args[0]))
  312. {
  313. playerTarget = p;
  314. }
  315. }
  316. //If the player exists
  317. if (playerTarget != null){
  318. //Displaying the information
  319. player.sendMessage(Colors.Blue + "Whois results for " +
  320. vminecraftChat.getName(playerTarget));
  321. //Group
  322. for(String group: playerTarget.getGroups())
  323. player.sendMessage(Colors.Blue + "Groups: " + group);
  324. //Admin
  325. player.sendMessage(Colors.Blue+"Admin: " +
  326. String.valueOf(playerTarget.isAdmin()));
  327. //IP
  328. player.sendMessage(Colors.Blue+"IP: " + playerTarget.getIP());
  329. //Restrictions
  330. player.sendMessage(Colors.Blue+"Can ignore restrictions: " +
  331. String.valueOf(playerTarget.canIgnoreRestrictions()));
  332. //Give the user an error if the player doesn't exist
  333. } else {
  334. player.sendMessage(Colors.Rose+"Player not found.");
  335. }
  336. }
  337. return EXIT_SUCCESS;
  338. }
  339. return EXIT_SUCCESS;
  340. }
  341. //=====================================================================
  342. //Function: who (/who)
  343. //Input: Player player: The player using the command
  344. // String[] args: Ignored
  345. //Output: int: Exit Code
  346. //Use: Displays the connected players
  347. //=====================================================================
  348. public static int who(Player player, String[] args)
  349. {
  350. //If the command is enabled
  351. if (vminecraftSettings.getInstance().cmdWho()) {
  352. //Loop through all players counting them and adding to the list
  353. int count=0;
  354. String tempList = "";
  355. for( Player p : etc.getServer().getPlayerList())
  356. {
  357. if(p != null){
  358. if(count == 0)
  359. tempList += vminecraftChat.getName(p);
  360. else
  361. tempList += Colors.White + ", " + vminecraftChat.getName(p);
  362. count++;
  363. }
  364. }
  365. //Get the max players from the config
  366. PropertiesFile server = new PropertiesFile("server.properties");
  367. try {
  368. server.load();
  369. } catch (IOException e) {
  370. e.printStackTrace();
  371. }
  372. int maxPlayers = server.getInt("max-players");
  373. //Output the player list
  374. String[] tempOut = vminecraftChat.wordWrap(Colors.Rose + "Player List ("
  375. + count + "/" + maxPlayers +"): " + tempList);
  376. for(String msg: tempOut)
  377. player.sendMessage( msg );
  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(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(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. }