vminecraftCommands.java 28 KB

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