vminecraftCommands.java 28 KB

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