vminecraftCommands.java 31 KB

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