vMinecraftCommands.java 30 KB

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