vminecraftCommands.java 30 KB

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