2
0

vminecraftCommands.java 29 KB

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