vminecraftCommands.java 28 KB

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