vminecraftCommands.java 30 KB

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