vminecraftCommands.java 30 KB

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