vminecraftCommands.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. && vminecraftSettings.getInstance().getRules().length != 0) {
  168. //Display them
  169. for (String str : vminecraftSettings.getInstance().getRules()) {
  170. if(str.isEmpty())
  171. player.sendMessage(Colors.Blue+str);
  172. }
  173. return true;
  174. }
  175. return false;
  176. }
  177. //=====================================================================
  178. //Function: fabulous (/fabulous)
  179. //Input: Player player: The player using the command
  180. // String[] args: The message to apply the effect to
  181. //Output: boolean: If the user has access to the command
  182. // and it is enabled
  183. //Use: Makes the text rainbow colored
  184. //=====================================================================
  185. public static boolean fabulous(Player player, String[] args)
  186. {
  187. //If the command is enabled
  188. if(vminecraftSettings.getInstance().cmdFabulous()) {
  189. String playerName = "<" + vminecraftChat.nameColor(player)
  190. + Colors.White + "> ";
  191. //Make sure a message has been specified
  192. if (args.length < 1) {return false;}
  193. String str = " ";
  194. //Merge the message again
  195. str = etc.combineSplit(0, args, " ");
  196. //Output for server
  197. log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\"");
  198. //Prepend the player name
  199. String[] message = vminecraftChat.wordWrap(playerName + str);
  200. //Output the first line
  201. vminecraftChat.gmsg( playerName + vminecraftChat.rainbow(message[0]));
  202. //Get the rest of the lines and display them.
  203. String[] tempOut = new String[message.length - 1];
  204. System.arraycopy(message, 1, tempOut, 0, tempOut.length);
  205. for(String msg: tempOut)
  206. vminecraftChat.gmsg(vminecraftChat.rainbow(msg));
  207. return true;
  208. }
  209. return false;
  210. }
  211. //=====================================================================
  212. //Function: whois (/whois)
  213. //Input: Player player: The player using the command
  214. // String[] args: The player to find info on
  215. //Output: boolean: If the user has access to the command
  216. // and it is enabled
  217. //Use: Displays information about the player specified
  218. //=====================================================================
  219. public static boolean whois(Player player, String[] args)
  220. {
  221. //If the command is enabled
  222. if (vminecraftSettings.getInstance().cmdWhoIs()) {
  223. //If a player is specified
  224. if (args.length < 1)
  225. player.sendMessage(Colors.Rose + "Usage is /whois [player]");
  226. else {
  227. //Get the player by name
  228. Player playerTarget = null;
  229. for( Player p : etc.getServer().getPlayerList())
  230. {
  231. if (p.getName().equalsIgnoreCase(args[0]))
  232. {
  233. playerTarget = p;
  234. }
  235. }
  236. //If the player exists
  237. if (playerTarget != null){
  238. //Displaying the information
  239. player.sendMessage(Colors.Blue + "Whois results for " +
  240. vminecraftChat.nameColor(playerTarget));
  241. //Group
  242. for(String group: playerTarget.getGroups())
  243. player.sendMessage(Colors.Blue + "Groups: " + group);
  244. //Admin
  245. player.sendMessage(Colors.Blue+"Admin: " +
  246. String.valueOf(playerTarget.canIgnoreRestrictions()));
  247. //IP
  248. player.sendMessage(Colors.Blue+"IP: " + playerTarget.getIP());
  249. //Restrictions
  250. player.sendMessage(Colors.Blue+"Can ignore restrictions: " +
  251. String.valueOf(playerTarget.canIgnoreRestrictions()));
  252. //Give the user an error if the player doesn't exist
  253. } else {
  254. player.sendMessage(Colors.Rose+"Player not found.");
  255. }
  256. }
  257. return true;
  258. }
  259. return false;
  260. }
  261. //=====================================================================
  262. //Function: who (/who)
  263. //Input: Player player: The player using the command
  264. // String[] args: Ignored
  265. //Output: boolean: If the user has access to the command
  266. // and it is enabled
  267. //Use: Displays the connected players
  268. //=====================================================================
  269. public static boolean 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 true;
  300. }
  301. return false;
  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: boolean: If the user has access to the command
  308. // and it is enabled
  309. //Use: Announces the message to all players
  310. //=====================================================================
  311. public static boolean say(Player player, String[] args)
  312. {
  313. //If the command is enabled
  314. if (vminecraftSettings.getInstance().cmdSay()) {
  315. //Make sure a message is supplied or output an error
  316. if (args.length < 1) {
  317. player.sendMessage(Colors.Rose + "Usage is /say [message]");
  318. }
  319. //Display the message globally
  320. vminecraftChat.gmsg(Colors.Yellow + etc.combineSplit(0, args, " "));
  321. return true;
  322. }
  323. return false;
  324. }
  325. //=====================================================================
  326. //Function: slay (/slay)
  327. //Input: Player player: The player using the command
  328. // String[] args: The target for the command
  329. //Output: boolean: If the user has access to the command
  330. // and it is enabled
  331. //Use: Kill the target player
  332. //=====================================================================
  333. public static boolean slay(Player player, String[] args)
  334. {
  335. //Check if the command is enabled
  336. if(vminecraftSettings.getInstance().cmdEzModo()) {
  337. //Get the player by name
  338. Player playerTarget = etc.getServer().matchPlayer(args[0]);
  339. //If the player doesn't exist don't run
  340. if(playerTarget == null)
  341. return false;
  342. //If the player isn't invulnerable kill them
  343. if (!vminecraftSettings.getInstance().isEzModo(playerTarget.getName())) {
  344. playerTarget.setHealth(0);
  345. vminecraftChat.gmsg(player.getColor() + player.getName() + Colors.LightBlue + " has slain " + playerTarget.getColor() + playerTarget.getName());
  346. //Otherwise output error to the user
  347. } else {
  348. player.sendMessage(Colors.Rose + "That player is currently in ezmodo! Hahahaha");
  349. }
  350. return true;
  351. }
  352. return false;
  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: boolean: If the user has access to the command
  359. // and it is enabled
  360. //Use: Kill the target player
  361. //=====================================================================
  362. public static boolean invuln(Player player, String[] args)
  363. {
  364. //If the command is enabled
  365. if (vminecraftSettings.getInstance().cmdEzModo()) {
  366. //If the player is already invulnerable, turn ezmodo off.
  367. if (vminecraftSettings.getInstance().isEzModo(player.getName())) {
  368. player.sendMessage(Colors.Red + "ezmodo = off");
  369. vminecraftSettings.getInstance().removeEzModo(player.getName());
  370. //Otherwise make them invulnerable
  371. } else {
  372. player.sendMessage(Colors.LightBlue + "eh- maji? ezmodo!?");
  373. player.sendMessage(Colors.Rose + "kimo-i");
  374. player.sendMessage(Colors.LightBlue + "Easy Mode ga yurusareru no wa shougakusei made dayo ne");
  375. player.sendMessage(Colors.Red + "**Laughter**");
  376. vminecraftSettings.getInstance().addEzModo(player.getName());
  377. player.setHealth(vminecraftSettings.getInstance().ezModoHealth());
  378. }
  379. return true;
  380. }
  381. return false;
  382. }
  383. //=====================================================================
  384. //Function: ezlist (/ezlist)
  385. //Input: Player player: The player using the command
  386. // String[] args: Ignored
  387. //Output: boolean: If the user has access to the command
  388. // and it is enabled
  389. //Use: List all invulnerable players
  390. //=====================================================================
  391. public static boolean ezlist(Player player, String[] args)
  392. {
  393. //If the feature is enabled list the players
  394. if(vminecraftSettings.getInstance().cmdEzModo()) {
  395. player.sendMessage("Ezmodo: " + vminecraftSettings.getInstance().ezModoList());
  396. return true;
  397. }
  398. return false;
  399. }
  400. //Disable using /modify to add commands (need to make a boolean settings for this)
  401. //ezlist
  402. //ezmodo
  403. /*
  404. //Promote
  405. if (vminecraftSettings.getInstance().cmdPromote() && split[0].equalsIgnoreCase("/promote")) {
  406. if(split.length != 2)
  407. {
  408. player.sendMessage(Colors.Rose + "Usage is /promote [Player]");
  409. }
  410. Player playerTarget = null;
  411. if(split.length==2){
  412. for( Player p : etc.getServer().getPlayerList())
  413. {
  414. if (p.getName().equalsIgnoreCase(split[1]))
  415. {
  416. playerTarget = p;
  417. }
  418. }
  419. if( playerTarget!=null)
  420. {
  421. String playerTargetGroup[] = playerTarget.getGroups();
  422. String playerGroup[] = player.getGroups();
  423. player.sendMessage("Debug data:");
  424. player.sendMessage("PlayerTarget: "+playerTargetGroup[0]);
  425. player.sendMessage("Player: "+playerGroup[0]);
  426. if(playerTargetGroup[0].equals("admins"))
  427. {
  428. player.sendMessage(Colors.Rose + "You can not promote " + split[1] + " any higher.");
  429. }
  430. if(playerTargetGroup[0].equals("mods") && (playerGroup[0].equals("owner")))
  431. {
  432. playerTarget.setGroups(ranks.Admins);
  433. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  434. String message = Colors.Yellow + split[1] + " was promoted to" + Colors.Rose + " Admin";
  435. other.gmsg(message);
  436. }
  437. else if (playerTargetGroup[0].equals("trusted") && (playerGroup[0].equals("admins") || playerGroup[0].equals("owner")))
  438. {
  439. playerTarget.setGroups(ranks.Mods);
  440. playerTargetGroup[0]="Mods";
  441. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  442. String message = Colors.Yellow + split[1] + " was promoted to" + Colors.DarkPurple + " Mod";
  443. other.gmsg(message);
  444. }
  445. else if (playerTargetGroup[0].equals("default") && (playerGroup[0].equals("mods") || playerGroup[0].equals("admins") || player.isInGroup("owner")))
  446. {
  447. playerTarget.setGroups(ranks.Trusted);
  448. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  449. String message = Colors.Yellow + split[1] + " was promoted to" + Colors.LightGreen + " Trusted";
  450. other.gmsg(message);
  451. }
  452. return true;
  453. }
  454. else{
  455. player.sendMessage(Colors.Rose + "Player not found");
  456. }
  457. log.log(Level.INFO, "Command used by " + player + " " + split[0] +" "+split[1]+" ");
  458. }
  459. }
  460. //Demote
  461. if (vminecraftSettings.getInstance().cmdPromote() && split[0].equalsIgnoreCase("/promote"))
  462. {
  463. if(split.length != 2)
  464. {
  465. player.sendMessage(Colors.Rose + "Usage is /demote [Player]");
  466. }
  467. Player playerTarget = null;
  468. for( Player p : etc.getServer().getPlayerList())
  469. {
  470. if (p.getName().equalsIgnoreCase(split[1]))
  471. {
  472. playerTarget = p;
  473. }
  474. }
  475. if( playerTarget!=null)
  476. {
  477. if(playerTarget.isInGroup("admins") && (player.isInGroup("superadmins")))
  478. {
  479. playerTarget.setGroups(ranks.Mods);
  480. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  481. String message = Colors.Yellow + split[1] + " was demoted to" + Colors.DarkPurple + " Mod";
  482. other.gmsg(message);
  483. }
  484. if(playerTarget.isInGroup("mods") && (player.isInGroup("admins") || player.isInGroup("superadmins")))
  485. {
  486. playerTarget.setGroups(ranks.Trusted);
  487. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  488. String message = Colors.Yellow + split[1] + " was demoted to" + Colors.LightGreen + " Trusted";
  489. other.gmsg(message);
  490. }
  491. else if (playerTarget.isInGroup("trusted") && (player.isInGroup("mods") || player.isInGroup("superadmins") || player.isInGroup("admins")))
  492. {
  493. playerTarget.setGroups(ranks.Def);
  494. etc.getInstance().getDataSource().modifyPlayer(playerTarget);
  495. String message = Colors.Yellow + split[1] + " was demoted to" + Colors.White + " Default";
  496. other.gmsg(message);
  497. }
  498. else if (playerTarget.isInGroup("default") && (player.isInGroup("mods") || player.isInGroup("admins") || player.isInGroup("superadmins")))
  499. {
  500. player.sendMessage(Colors.Rose + "You can not demote " + split[1] + " any lower.");
  501. }
  502. }
  503. else{
  504. player.sendMessage(Colors.Rose + "Player not found");
  505. }
  506. log.log(Level.INFO, "Command used by " + player + " " + split[0] +" "+split[1]+" ");
  507. return true;
  508. }*/
  509. }
  510. //=====================================================================
  511. //Class: commandList
  512. //Use: The list of commands that will be checked for
  513. //Author: cerevisiae
  514. //=====================================================================
  515. class commandList {
  516. command[] commands;
  517. protected static final Logger log = Logger.getLogger("Minecraft");
  518. //=====================================================================
  519. //Function: commandList
  520. //Input: None
  521. //Output: None
  522. //Use: Initialize the array of commands
  523. //=====================================================================
  524. public commandList(){
  525. commands = new command[0];
  526. }
  527. //=====================================================================
  528. //Function: register
  529. //Input: String name: The name of the command
  530. // String func: The function to be called
  531. //Output: boolean: Whether the command was input successfully or not
  532. //Use: Registers a command to the command list for checking later
  533. //=====================================================================
  534. public boolean register(String name, String func){
  535. //If the command list isn't empty
  536. if(commands.length > 0)
  537. {
  538. //Check to make sure the command doesn't already exist
  539. for(int i = 0; i < commands.length; i++)
  540. if(commands[i].getName().equalsIgnoreCase(name))
  541. return false;
  542. //Create a new temp array
  543. command[] temp = new command[commands.length + 1];
  544. //Copy the old command list over
  545. System.arraycopy(commands, 0, temp, 0, commands.length);
  546. //Set commands to equal the new array
  547. commands = temp;
  548. } else {
  549. commands = new command[1];
  550. }
  551. //Add the new function to the list
  552. commands[commands.length - 1] = new command(name, func);
  553. //exit successfully
  554. return true;
  555. }
  556. //=====================================================================
  557. //Function: register
  558. //Input: String name: The name of the command
  559. // String func: The function to be called
  560. // String info: The information for the command to put in help
  561. //Output: boolean: Whether the command was input successfully or not
  562. //Use: Registers a command to the command list for checking later
  563. //=====================================================================
  564. public boolean register(String name, String func, String info){
  565. //Add to the /help list
  566. etc.getInstance().addCommand(name, info);
  567. //Finish registering
  568. return register(name, func);
  569. }
  570. //=====================================================================
  571. //Function: register
  572. //Input: String name: The name of the command
  573. // String func: The function to be called
  574. //Output: boolean: Whether the command was input successfully or not
  575. //Use: Registers a command to the command list for checking later
  576. //=====================================================================
  577. public boolean registerAlias(String name, String com, String[] args){
  578. //If the command list isn't empty
  579. if(commands.length > 0)
  580. {
  581. //Check to make sure the command doesn't already exist
  582. for(int i = 0; i < commands.length; i++)
  583. if(commands[i].getName().equalsIgnoreCase(name))
  584. return false;
  585. //Create a new temp array
  586. command[] temp = new command[commands.length + 1];
  587. //Copy the old command list over
  588. System.arraycopy(commands, 0, temp, 0, commands.length);
  589. //Set commands to equal the new array
  590. commands = temp;
  591. } else {
  592. commands = new command[1];
  593. }
  594. //Add the new function to the list
  595. commands[commands.length - 1] = new commandRef(name, com, args);
  596. //exit successfully
  597. return true;
  598. }
  599. //=====================================================================
  600. //Function: register
  601. //Input: String name: The name of the command
  602. // String func: The function to be called
  603. //Output: boolean: Whether the command was input successfully or not
  604. //Use: Registers a command to the command list for checking later
  605. //=====================================================================
  606. public boolean registerAlias(String name, String com){
  607. //If the command list isn't empty
  608. if(commands.length > 0)
  609. {
  610. //Check to make sure the command doesn't already exist
  611. for(int i = 0; i < commands.length; i++)
  612. if(commands[i].getName().equalsIgnoreCase(name))
  613. return false;
  614. //Create a new temp array
  615. command[] temp = new command[commands.length + 1];
  616. //Copy the old command list over
  617. System.arraycopy(commands, 0, temp, 0, commands.length);
  618. //Set commands to equal the new array
  619. commands = temp;
  620. } else {
  621. commands = new command[1];
  622. }
  623. //Add the new function to the list
  624. commands[commands.length - 1] = new commandRef(name, com);
  625. //exit successfully
  626. return true;
  627. }
  628. //=====================================================================
  629. //Function: call
  630. //Input: String name: The name of the command to be run
  631. //Output: boolean: If the command was called successfully
  632. //Use: Attempts to call a command
  633. //=====================================================================
  634. public boolean call(String name, Player player, String[] arg){
  635. //Make sure the user has access to the command
  636. if(!player.canUseCommand(name)) {
  637. return false;
  638. }
  639. //Search for the command
  640. for(command cmd : commands)
  641. {
  642. //When found
  643. if(cmd.getName().equalsIgnoreCase(name))
  644. {
  645. try {
  646. //Call the command and return results
  647. return cmd.call(player, arg);
  648. } catch (SecurityException e) {
  649. log.log(Level.SEVERE, "Exception while running command", e);
  650. } catch (IllegalArgumentException e) {
  651. log.log(Level.SEVERE, "The Command Entered Doesn't Exist", e);
  652. return false;
  653. }
  654. }
  655. }
  656. //Something went wrong
  657. return false;
  658. }
  659. //=====================================================================
  660. //Class: command
  661. //Use: The specific command
  662. //Author: cerevisiae
  663. //=====================================================================
  664. private class command{
  665. private String commandName;
  666. private String function;
  667. //=====================================================================
  668. //Function: command
  669. //Input: None
  670. //Output: None
  671. //Use: Initialize the command
  672. //=====================================================================
  673. public command(String name, String func){
  674. commandName = name;
  675. function = func;
  676. }
  677. //=====================================================================
  678. //Function: getName
  679. //Input: None
  680. //Output: String: The command name
  681. //Use: Returns the command name
  682. //=====================================================================
  683. public String getName(){return commandName;}
  684. //=====================================================================
  685. //Function: call
  686. //Input: String[] arg: The arguments for the command
  687. //Output: boolean: If the command was called successfully
  688. //Use: Attempts to call the command
  689. //=====================================================================
  690. boolean call(Player player, String[] arg)
  691. {
  692. Method m;
  693. try {
  694. m = vminecraftCommands.class.getMethod(function, Player.class, String[].class);
  695. m.setAccessible(true);
  696. return (Boolean) m.invoke(null, player, arg);
  697. } catch (SecurityException e) {
  698. e.printStackTrace();
  699. } catch (NoSuchMethodException e) {
  700. e.printStackTrace();
  701. } catch (IllegalArgumentException e) {
  702. e.printStackTrace();
  703. } catch (IllegalAccessException e) {
  704. e.printStackTrace();
  705. } catch (InvocationTargetException e) {
  706. e.printStackTrace();
  707. }
  708. return true;
  709. }
  710. }
  711. //=====================================================================
  712. //Class: commandRef
  713. //Use: A command referencing another command
  714. //Author: cerevisiae
  715. //=====================================================================
  716. private class commandRef extends command{
  717. private String reference;
  718. private String[] args;
  719. //=====================================================================
  720. //Function: command
  721. //Input: String name: The command name
  722. // String com: The command to run
  723. // String[] arg: the arguments to apply
  724. //Output: None
  725. //Use: Initialize the command
  726. //=====================================================================
  727. public commandRef(String name, String com, String[] arg){
  728. super(name, "");
  729. reference = com;
  730. args = arg;
  731. }
  732. //=====================================================================
  733. //Function: command
  734. //Input: String name: The command name
  735. // String com: The command to run
  736. //Output: None
  737. //Use: Initialize the command
  738. //=====================================================================
  739. public commandRef(String name, String com){
  740. super(name, "");
  741. reference = com;
  742. args = null;
  743. }
  744. //=====================================================================
  745. //Function: call
  746. //Input: String[] arg: The arguments for the command
  747. //Output: boolean: If the command was called successfully
  748. //Use: Attempts to call the command
  749. //=====================================================================
  750. boolean call(Player player, String[] arg)
  751. {
  752. if(args != null) {
  753. String[] temp = new String[args.length];
  754. System.arraycopy(args, 0, temp, 0, args.length);
  755. //Insert the arguments into the pre-set arguments
  756. int lastSet = 0,
  757. argCount = 0;
  758. for(String argument : temp)
  759. {
  760. if(argument.startsWith("%"))
  761. {
  762. int argNum = Integer.parseInt(argument.substring(1));
  763. if( argNum < arg.length )
  764. {
  765. temp[lastSet] = arg[argNum];
  766. argCount++;
  767. }
  768. }
  769. lastSet++;
  770. }
  771. //Append the rest of the arguments to the argument array
  772. if(lastSet < temp.length + arg.length - argCount)
  773. {
  774. String[] temp2 = new String[temp.length + arg.length - argCount];
  775. System.arraycopy(temp, 0, temp2, 0, temp.length);
  776. System.arraycopy(arg, argCount, temp2,
  777. temp.length, arg.length - argCount);
  778. temp = temp2;
  779. }
  780. //Call the referenced command
  781. player.command(reference + " " + etc.combineSplit(0, temp, " "));
  782. } else
  783. player.command(reference);
  784. /*if(temp != null)
  785. etc.getServer().useConsoleCommand(reference + " " + etc.combineSplit(0, temp, " "), player);
  786. else
  787. etc.getServer().useConsoleCommand(reference, player);*/
  788. return true;
  789. }
  790. }
  791. }