vMinecraftChat.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. import java.util.ArrayList;
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4. //=====================================================================
  5. //Class: vMinecraftChat
  6. //Use: Encapsulates all chat commands added by this mod
  7. //Author: nossr50, TrapAlice, cerevisiae
  8. //=====================================================================
  9. public class vMinecraftChat {
  10. protected static final Logger log = Logger.getLogger("Minecraft");
  11. protected static final int lineLength = 312;
  12. //The array of colors to use
  13. protected static final String[] rainbow = new String[] {
  14. Colors.Red,
  15. Colors.Rose,
  16. Colors.Gold,
  17. Colors.Yellow,
  18. Colors.LightGreen,
  19. Colors.Green,
  20. Colors.LightBlue,
  21. Colors.Blue,
  22. Colors.Navy,
  23. Colors.DarkPurple,
  24. Colors.Purple,
  25. Colors.LightPurple};
  26. protected static final String[] xmas = new String[] {
  27. Colors.Red,
  28. Colors.Red,
  29. Colors.White,
  30. Colors.White,
  31. Colors.Green,
  32. Colors.Green,
  33. };
  34. //=====================================================================
  35. //Function: gmsg
  36. //Input: Player sender: The player sending the message
  37. // String msg: The message to be broadcast to all players
  38. //Output: None
  39. //Use: Outputs a message to everybody
  40. //=====================================================================
  41. public static void gmsg(Player sender, String msg){
  42. if(sender != null && sender.isMuted())
  43. sender.sendMessage(Colors.Red + "You have been muted.");
  44. for (Player receiver : etc.getServer().getPlayerList()) {
  45. if (receiver == null) return;
  46. if(vMinecraftUsers.getProfile(receiver) == null) return;
  47. //Check if the person has the sender ignored
  48. if(sender != null)
  49. if(vMinecraftUsers.getProfile(receiver).isIgnored(sender))
  50. return;
  51. String[] message = applyColors(wordWrap(msg));
  52. for(String out : message)
  53. receiver.sendMessage(out);
  54. }
  55. }
  56. //=====================================================================
  57. //Function: gmsg
  58. //Input: String msg: The message to be broadcast to all players
  59. //Output: None
  60. //Use: Outputs a message to everybody
  61. //=====================================================================
  62. public static void gmsg(String msg){gmsg(null, msg);}
  63. //=====================================================================
  64. //Function: sendMessage
  65. //Input: Player sender: The player sending the message
  66. // Player receiver: The player receiving the message
  67. // String msg: The message to be broadcast to all players
  68. //Output: None
  69. //Use: Outputs a message to everybody
  70. //=====================================================================
  71. public static void sendMessage(Player sender, Player receiver, String msg){
  72. if(sender != null && sender.isMuted())
  73. sender.sendMessage(Colors.Red + "You have been muted.");
  74. //Check if the receiver has the sender ignored
  75. if(vMinecraftUsers.getProfile(receiver) == null)
  76. return;
  77. if(sender != null)
  78. if(vMinecraftUsers.getProfile(receiver).isIgnored(sender))
  79. {
  80. sendMessage(sender, sender, Colors.Rose + receiver.getName()
  81. + " has you on their ignore list.");
  82. return;
  83. }
  84. String[] message = applyColors(wordWrap(msg));
  85. for(String out : message)
  86. receiver.sendMessage(out);
  87. //Tell them if they are
  88. }
  89. //=====================================================================
  90. //Function: sendMessage
  91. //Input: Player receiver: The player receiving the message
  92. // String msg: The message to be broadcast to all players
  93. //Output: None
  94. //Use: Outputs a message to everybody
  95. //=====================================================================
  96. public static void sendMessage(Player receiver, String msg)
  97. {
  98. sendMessage(null, receiver, msg);
  99. }
  100. //=====================================================================
  101. //Function: wordWrap
  102. //Input: String msg: The message to be wrapped
  103. //Output: String[]: The array of substrings
  104. //Use: Cuts the message apart into whole words short enough to fit
  105. // on one line
  106. //=====================================================================
  107. public static String[] wordWrap(String msg){
  108. //Split each word apart
  109. ArrayList<String> split = new ArrayList<String>();
  110. for(String in : msg.split(" "))
  111. split.add(in);
  112. //Create an arraylist for the output
  113. ArrayList<String> out = new ArrayList<String>();
  114. //While i is less than the length of the array of words
  115. while(!split.isEmpty()){
  116. int len = 0;
  117. //Create an arraylist to hold individual words
  118. ArrayList<String> words = new ArrayList<String>();
  119. //Loop through the words finding their length and increasing
  120. //j, the end point for the sub string
  121. while(!split.isEmpty() && split.get(0) != null && len <= lineLength)
  122. {
  123. int wordLength = msgLength(split.get(0)) + 4;
  124. //If a word is too long for a line
  125. if(wordLength > lineLength)
  126. {
  127. String[] tempArray = wordCut(len, split.remove(0));
  128. words.add(tempArray[0]);
  129. split.add(tempArray[1]);
  130. }
  131. //If the word is not too long to fit
  132. len += wordLength;
  133. if( len < lineLength)
  134. words.add(split.remove(0));
  135. }
  136. //Merge them and add them to the output array.
  137. out.add( etc.combineSplit(0,
  138. words.toArray(new String[words.size()]), " ") + " " );
  139. }
  140. //Convert to an array and return
  141. return out.toArray(new String[out.size()]);
  142. }
  143. //=====================================================================
  144. //Function: msgLength
  145. //Input: String str: The string to find the length of
  146. //Output: int: The length on the screen of a string
  147. //Use: Finds the length on the screen of a string. Ignores colors.
  148. //=====================================================================
  149. public static int msgLength(String str){
  150. int length = 0;
  151. //Loop through all the characters, skipping any color characters
  152. //and their following color codes
  153. for(int x = 0; x<str.length(); x++)
  154. {
  155. if(str.charAt(x) == '^' || str.charAt(x) == Colors.White.charAt(0))
  156. {
  157. if(colorChange(str.charAt(x + 1)) != null)
  158. {
  159. x++;
  160. continue;
  161. }
  162. }
  163. int len = charLength(str.charAt(x));
  164. length += len;
  165. }
  166. return length;
  167. }
  168. //=====================================================================
  169. //Function: wordCut
  170. //Input: String str: The string to find the length of
  171. //Output: String[]: The cut up word
  172. //Use: Cuts apart a word that is too long to fit on one line
  173. //=====================================================================
  174. private static String[] wordCut(int lengthBefore, String str){
  175. int length = lengthBefore;
  176. //Loop through all the characters, skipping any color characters
  177. //and their following color codes
  178. String[] output = new String[2];
  179. int x = 0;
  180. while(length < lineLength && x < str.length())
  181. {
  182. int len = charLength(str.charAt(x));
  183. if( len > 0)
  184. length += len;
  185. else
  186. x++;
  187. x++;
  188. }
  189. if(x > str.length())
  190. x = str.length();
  191. //Add the substring to the output after cutting it
  192. output[0] = str.substring(0, x);
  193. //Add the last of the string to the output.
  194. output[1] = str.substring(x);
  195. return output;
  196. }
  197. //=====================================================================
  198. //Function: charLength
  199. //Input: char x: The character to find the length of.
  200. //Output: int: The length of the character
  201. //Use: Finds the visual length of the character on the screen.
  202. //=====================================================================
  203. private static int charLength(char x)
  204. {
  205. if("i.:,;|!".indexOf(x) != -1)
  206. return 2;
  207. else if("l'".indexOf(x) != -1)
  208. return 3;
  209. else if("tI[]".indexOf(x) != -1)
  210. return 4;
  211. else if("fk{}<>\"*()".indexOf(x) != -1)
  212. return 5;
  213. else if("abcdeghjmnopqrsuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890\\/#?$%-=_+&^".indexOf(x) != -1)
  214. return 6;
  215. else if("@~".indexOf(x) != -1)
  216. return 7;
  217. else if(x==' ')
  218. return 4;
  219. else
  220. return -1;
  221. }
  222. //=====================================================================
  223. //Function: rainbow
  224. //Input: String msg: The string to colorify
  225. //Output: String: The rainbowed result
  226. //Use: Rainbowifies a string;
  227. //=====================================================================
  228. public static String rainbow(String msg){
  229. String temp = "";
  230. int counter=0;
  231. //Loop through the message applying the colors
  232. for(int x=0; x<msg.length(); x++)
  233. {
  234. temp += rainbow[counter]+msg.charAt(x);
  235. if(msg.charAt(x)!=' ') counter++;
  236. if(counter==rainbow.length) counter = 0;
  237. }
  238. return temp;
  239. }
  240. //=====================================================================
  241. //Function: xmas
  242. //Input: String msg: The string to colorify
  243. //Output: String: The xmas colored result
  244. //Use: Makes a string more festive
  245. //=====================================================================
  246. public static String xmas(String msg){
  247. String temp = "";
  248. int counter=0;
  249. //Loop through the message applying the colors
  250. for(int x=0; x<msg.length(); x++)
  251. {
  252. temp += xmas[counter]+msg.charAt(x);
  253. if(msg.charAt(x)!=' ') counter++;
  254. if(counter==xmas.length) counter = 0;
  255. }
  256. return temp;
  257. }
  258. //=====================================================================
  259. //Function: getName
  260. //Input: Player player: The player to get name as color
  261. //Output: String: The name colored
  262. //Use: Returns the colored name;
  263. //=====================================================================
  264. public static String getName(Player player){
  265. //Add the nickname or the name if there is none
  266. String output = vMinecraftUsers.getProfile(player).getNick();
  267. if(output.isEmpty())
  268. output = player.getName();
  269. //Add the color if there is one
  270. if(player.getColor() != null && player.getColor() != "")
  271. output = player.getColor().substring(0,2) + output;
  272. //Add the tag if there is one
  273. output = vMinecraftUsers.getProfile(player).getTag() + output;
  274. //Add the suffix if there is one
  275. output += vMinecraftUsers.getProfile(player).getSuffix();
  276. output = Colors.White + output;
  277. /*if(playerPrefix != null && !playerPrefix.isEmpty())
  278. output = applyColors(playerPrefix.substring(1)) + output;*/
  279. //Return the name
  280. return output;
  281. }
  282. //=====================================================================
  283. //Function: colorChange
  284. //Input: char colour: The color code to find the color for
  285. //Output: String: The color that the code identified
  286. //Use: Finds a color giving a color code
  287. //=====================================================================
  288. public static String colorChange(char colour)
  289. {
  290. String color = "";
  291. switch(colour)
  292. {
  293. case '0':
  294. color = Colors.Black;
  295. break;
  296. case '1':
  297. color = Colors.Navy;
  298. break;
  299. case '2':
  300. color = Colors.Green;
  301. break;
  302. case '3':
  303. color = Colors.Blue;
  304. break;
  305. case '4':
  306. color = Colors.Red;
  307. break;
  308. case '5':
  309. color = Colors.Purple;
  310. break;
  311. case '6':
  312. color = Colors.Gold;
  313. break;
  314. case '7':
  315. color = Colors.LightGray;
  316. break;
  317. case '8':
  318. color = Colors.Gray;
  319. break;
  320. case '9':
  321. color = Colors.DarkPurple;
  322. break;
  323. case 'a':
  324. color = Colors.LightGreen;
  325. break;
  326. case 'b':
  327. color = Colors.LightBlue;
  328. break;
  329. case 'c':
  330. color = Colors.Rose;
  331. break;
  332. case 'd':
  333. color = Colors.LightPurple;
  334. break;
  335. case 'e':
  336. color = Colors.Yellow;
  337. break;
  338. case 'f':
  339. color = Colors.White;
  340. break;
  341. case 'A':
  342. color = Colors.LightGreen;
  343. break;
  344. case 'B':
  345. color = Colors.LightBlue;
  346. break;
  347. case 'C':
  348. color = Colors.Rose;
  349. break;
  350. case 'D':
  351. color = Colors.LightPurple;
  352. break;
  353. case 'E':
  354. color = Colors.Yellow;
  355. break;
  356. case 'F':
  357. color = Colors.White;
  358. break;
  359. case 'R':
  360. color = "^r";
  361. break;
  362. case 'r':
  363. color = "^r";
  364. break;
  365. case 'x':
  366. color = "^x";
  367. break;
  368. case 'X':
  369. color = "^x";
  370. break;
  371. default:
  372. color = null;
  373. break;
  374. }
  375. return color;
  376. }
  377. //=====================================================================
  378. //Function: adminChat
  379. //Input: Player player: The player talking
  380. // String message: The message to apply the effect to
  381. //Output: boolean: If this feature is enabled
  382. //Use: Sends messages only to admins
  383. //=====================================================================
  384. public static boolean adminChat(Player player, String message){
  385. //Check if the player can use this feature
  386. if(player.isAdmin() || player.canUseCommand("/adminchat"))
  387. {
  388. //Special formatting for adminchat {Username}
  389. String adminchat = Colors.DarkPurple + "{" + getName(player)
  390. + Colors.DarkPurple +"} ";
  391. //Cut off the @ prefix
  392. if(message.startsWith("@"))
  393. message = message.substring(1, message.length());
  394. //Get the player from the playerlist to send the message to.
  395. for (Player p: etc.getServer().getPlayerList()) {
  396. //If p is not null
  397. if (p != null) {
  398. //And if p is an admin or has access to adminchat send message
  399. if (p.isAdmin() || (p.canUseCommand("/adminchat"))) {
  400. sendMessage(player, p, adminchat + message);
  401. }
  402. }
  403. }
  404. //So you can read adminchat from the server console
  405. log.log(Level.INFO, "@" + "<" + player.getName() + "> " + message);
  406. return true;
  407. }
  408. return false;
  409. }
  410. public static boolean partyChat(Player player, String message){
  411. if(vMinecraftUsers.getProfile(player).inParty()){
  412. String partychat = Colors.Green + "(" + getName(player) + Colors.Green + ") ";
  413. for (Player p: etc.getServer().getPlayerList()){
  414. if (p != null){
  415. if (vMinecraftUsers.getProfile(p).inParty() && (vMinecraftUsers.getProfile(p).getParty().equals(vMinecraftUsers.getProfile(player).getParty()))){
  416. sendMessage(player, p, partychat + Colors.Green + message);
  417. }
  418. }
  419. }
  420. return true;
  421. }
  422. return false;
  423. }
  424. //=====================================================================
  425. //Function: quote
  426. //Input: Player player: The player talking
  427. // String message: The message to apply the effect to
  428. //Output: boolean: If this feature is enabled
  429. //Use: Displays a message as a quote
  430. //=====================================================================
  431. public static boolean quote(Player player, String message)
  432. {
  433. //Format the name
  434. String playerName = Colors.White + "<" + getName(player)
  435. + Colors.White + "> ";
  436. if(vMinecraftSettings.getInstance().greentext()) {
  437. //Log the chat
  438. log.log(Level.INFO, "<"+player.getName()+"> " + message);
  439. //Output the message
  440. gmsg(player, playerName + Colors.LightGreen + message);
  441. return true;
  442. }
  443. return false;
  444. }
  445. //=====================================================================
  446. //Function: rage
  447. //Input: Player player: The player talking
  448. // String message: The message to apply the effect to
  449. //Output: boolean: If this feature is enabled
  450. //Use: Displays a message in red
  451. //=====================================================================
  452. public static boolean rage(Player player, String message)
  453. {
  454. //Format the name
  455. String playerName = Colors.White + "<"
  456. + getName(player) + Colors.White +"> ";
  457. if (vMinecraftSettings.getInstance().FFF()) {
  458. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  459. //Output the message
  460. gmsg(player, playerName + Colors.Red + message);
  461. return true;
  462. }
  463. return false;
  464. }
  465. //=====================================================================
  466. //Function: quakeColors
  467. //Input: Player player: The player talking
  468. // String message: The message to apply the effect to
  469. //Output: boolean: If this feature is enabled
  470. //Use: Displays a message in red
  471. //=====================================================================
  472. public static boolean quakeColors(Player player, String message)
  473. {
  474. //Format the name
  475. String playerName = Colors.White + "<"
  476. + getName(player) + Colors.White +"> ";
  477. if(vMinecraftSettings.getInstance().quakeColors()) {
  478. String color = vMinecraftUsers.getProfile(player).getColor();
  479. //Log the chat
  480. log.log(Level.INFO, "<"+player.getName()+"> " + message);
  481. //Output the message
  482. gmsg(player, playerName + color + message);
  483. //Loop through the string finding the color codes and inserting them
  484. return true;
  485. }
  486. return false;
  487. }
  488. //=====================================================================
  489. //Function: emote
  490. //Input: Player player: The player talking
  491. // String message: The message to apply the effect to
  492. //Output: boolean: If this feature is enabled
  493. //Use: /me but with our custom colors applied
  494. //=====================================================================
  495. public static boolean emote(Player player, String message)
  496. {
  497. gmsg(player, "* " + getName(player) + " " + Colors.White + message);
  498. return true;
  499. }
  500. //=====================================================================
  501. //Function: applyColors
  502. //Input: String[] message: The lines to be colored
  503. //Output: String[]: The lines, but colorful
  504. //Use: Colors each line
  505. //=====================================================================
  506. public static String[] applyColors(String[] message)
  507. {
  508. if(message != null && message[0] != null && !message[0].isEmpty()){
  509. //The color to start the line with
  510. String recentColor = Colors.White;
  511. //Go through each line
  512. int counter = 0;
  513. int i = 0;
  514. boolean taste = false;
  515. for(String msg: message)
  516. {
  517. //Start the line with the most recent color
  518. String temp = "";
  519. if(!recentColor.equals("^r") && recentColor != null)
  520. temp += recentColor;
  521. //Loop through looking for a color code
  522. for(int x = 0; x< msg.length(); x++)
  523. {
  524. //If the char is a ^ or �
  525. if(taste || msg.charAt(x) == '^'
  526. || msg.charAt(x) == Colors.Red.charAt(0))
  527. {
  528. if(x != msg.length() - 1)
  529. {
  530. //If the following character is a color code
  531. if(vMinecraftChat.colorChange(msg.charAt(x+1)) != null)
  532. {
  533. //Set the most recent color to the new color
  534. recentColor = vMinecraftChat.colorChange(msg.charAt(x+1));
  535. //If the color specified is rainbow
  536. if(taste || recentColor.equals("^r"))
  537. {
  538. //Skip the quake code for rainbow
  539. if(recentColor.equals("^r"))
  540. {
  541. x += 2;
  542. }
  543. //Taste keeps it going with rainbow if there
  544. //are more lines
  545. taste = true;
  546. //Loop through the message applying the colors
  547. while(x < msg.length() && msg.charAt(x) != '^'
  548. && msg.charAt(x) != Colors.Red.charAt(0))
  549. {
  550. temp += rainbow[i] + msg.charAt(x);
  551. if(msg.charAt(x) != ' ') i++;
  552. if(i == rainbow.length) i = 0;
  553. x++;
  554. }
  555. //If it reached another color instead of the end
  556. if(x < msg.length() && msg.charAt(x) == '^'
  557. || x < msg.length()
  558. && msg.charAt(x) == Colors.Red.charAt(0) )
  559. {
  560. taste = false;
  561. i = 0;
  562. x--;
  563. }
  564. }
  565. if(taste || recentColor.equals("^x"))
  566. {
  567. //Skip the quake code for xmas
  568. if(recentColor.equals("^x"))
  569. {
  570. x += 2;
  571. }
  572. //Taste keeps it going with xmas if there
  573. //are more lines
  574. taste = true;
  575. //Loop through the message applying the colors
  576. while(x < msg.length() && msg.charAt(x) != '^'
  577. && msg.charAt(x) != Colors.Red.charAt(0))
  578. {
  579. temp += xmas[i] + msg.charAt(x);
  580. if(msg.charAt(x) != ' ') i++;
  581. if(i == xmas.length) i = 0;
  582. x++;
  583. }
  584. //If it reached another color instead of the end
  585. if(x < msg.length() && msg.charAt(x) == '^'
  586. || x < msg.length()
  587. && msg.charAt(x) == Colors.Red.charAt(0) )
  588. {
  589. taste = false;
  590. i = 0;
  591. x--;
  592. }
  593. }
  594. else
  595. {
  596. //Add the color
  597. temp += recentColor;
  598. //Skip these chars
  599. x++;
  600. }
  601. //Otherwise ignore it.
  602. } else {
  603. temp += msg.charAt(x);
  604. }
  605. //Insert the character
  606. } else {
  607. temp += msg.charAt(x);
  608. }
  609. } else {
  610. temp += msg.charAt(x);
  611. }
  612. }
  613. //Replace the message with the colorful message
  614. message[counter] = temp;
  615. counter++;
  616. }
  617. }
  618. return message;
  619. }
  620. }