vChat.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 vChat {
  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(vUsers.getProfile(receiver) == null) return;
  47. //Check if the person has the sender ignored
  48. if(sender != null)
  49. if(vUsers.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(vUsers.getProfile(receiver) == null)
  76. return;
  77. if(sender != null)
  78. if(vUsers.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((x+1 <= str.length()) && (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 = vUsers.getProfile(player).getNick();
  267. if(output.isEmpty())
  268. output = player.getName();
  269. //Add the color if there is one
  270. if(player.getColor() != null && !"".equals(player.getColor()))
  271. output = player.getColor().substring(0,2) + output;
  272. //Add the tag if there is one
  273. output = vUsers.getProfile(player).getTag() + output;
  274. //Add the suffix if there is one
  275. output += vUsers.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(vConfig.getInstance().partyChat()){
  412. //Cut off the ! prefix
  413. if(message.startsWith("!"))
  414. message = message.substring(1, message.length());
  415. if(vUsers.getProfile(player).inParty()){
  416. String partychat = Colors.Green + "(" + getName(player) + Colors.Green + ") ";
  417. for (Player p: etc.getServer().getPlayerList()){
  418. if (p != null){
  419. if (vUsers.getProfile(p).inParty() && (vUsers.getProfile(p).getParty().equals(vUsers.getProfile(player).getParty()))){
  420. sendMessage(player, p, partychat + Colors.Green + message);
  421. }
  422. }
  423. }
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. //=====================================================================
  430. //Function: quote
  431. //Input: Player player: The player talking
  432. // String message: The message to apply the effect to
  433. //Output: boolean: If this feature is enabled
  434. //Use: Displays a message as a quote
  435. //=====================================================================
  436. public static boolean quote(Player player, String message)
  437. {
  438. //Format the name
  439. String playerName = vmc.getInstance().getGroupPrefix(player) + "<" + getName(player)
  440. + vmc.getInstance().getGroupPrefix(player) + "> ";
  441. if(vConfig.getInstance().greentext()) {
  442. //Log the chat
  443. log.log(Level.INFO, "<"+player.getName()+"> " + message);
  444. //Output the message
  445. gmsg(player, playerName + Colors.LightGreen + message);
  446. return true;
  447. }
  448. return false;
  449. }
  450. //=====================================================================
  451. //Function: rage
  452. //Input: Player player: The player talking
  453. // String message: The message to apply the effect to
  454. //Output: boolean: If this feature is enabled
  455. //Use: Displays a message in red
  456. //=====================================================================
  457. public static boolean rage(Player player, String message)
  458. {
  459. //Format the name
  460. String playerName = vmc.getInstance().getGroupPrefix(player) + "<"
  461. + getName(player) + vmc.getInstance().getGroupPrefix(player) +"> ";
  462. if (vConfig.getInstance().FFF()) {
  463. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  464. //Output the message
  465. gmsg(player, playerName + Colors.Red + message);
  466. return true;
  467. }
  468. return false;
  469. }
  470. //=====================================================================
  471. //Function: quakeColors
  472. //Input: Player player: The player talking
  473. // String message: The message to apply the effect to
  474. //Output: boolean: If this feature is enabled
  475. //Use: Displays a message in red
  476. //=====================================================================
  477. public static boolean quakeColors(Player player, String message)
  478. {
  479. //Format the name
  480. String playerName = vmc.getInstance().getGroupPrefix(player) + "<"
  481. + getName(player) + vmc.getInstance().getGroupPrefix(player) +"> ";
  482. if(vConfig.getInstance().quakeColors()) {
  483. String color = vUsers.getProfile(player).getColor();
  484. //Log the chat
  485. log.log(Level.INFO, "<"+player.getName()+"> " + message);
  486. //Output the message
  487. gmsg(player, playerName + color + message);
  488. //Loop through the string finding the color codes and inserting them
  489. return true;
  490. }
  491. return false;
  492. }
  493. //=====================================================================
  494. //Function: emote
  495. //Input: Player player: The player talking
  496. // String message: The message to apply the effect to
  497. //Output: boolean: If this feature is enabled
  498. //Use: /me but with our custom colors applied
  499. //=====================================================================
  500. public static boolean emote(Player player, String message)
  501. {
  502. gmsg(player, "* " + getName(player) + " " + Colors.White + message);
  503. return true;
  504. }
  505. //=====================================================================
  506. //Function: applyColors
  507. //Input: String[] message: The lines to be colored
  508. //Output: String[]: The lines, but colorful
  509. //Use: Colors each line
  510. //=====================================================================
  511. public static String[] applyColors(String[] message)
  512. {
  513. if(message != null && message[0] != null && !message[0].isEmpty()){
  514. //The color to start the line with
  515. String recentColor = Colors.White;
  516. //Go through each line
  517. int counter = 0;
  518. int i = 0;
  519. boolean taste = false;
  520. boolean xmasparty = false;
  521. for(String msg: message)
  522. {
  523. //Start the line with the most recent color
  524. String temp = "";
  525. if(!recentColor.equals("^r") && recentColor != null)
  526. temp += recentColor;
  527. //Loop through looking for a color code
  528. for(int x = 0; x< msg.length(); x++)
  529. {
  530. //If the char is a ^ or �
  531. if(taste || msg.charAt(x) == '^'
  532. || msg.charAt(x) == Colors.Red.charAt(0))
  533. {
  534. if(x != msg.length() - 1)
  535. {
  536. //If the following character is a color code
  537. if(vChat.colorChange(msg.charAt(x+1)) != null)
  538. {
  539. //Set the most recent color to the new color
  540. recentColor = vChat.colorChange(msg.charAt(x+1));
  541. //If the color specified is rainbow
  542. if(taste || recentColor.equals("^r"))
  543. {
  544. //Skip the quake code for rainbow
  545. if(recentColor.equals("^r"))
  546. {
  547. x += 2;
  548. }
  549. //Taste keeps it going with rainbow if there
  550. //are more lines
  551. taste = true;
  552. //Loop through the message applying the colors
  553. while(x < msg.length() && msg.charAt(x) != '^'
  554. && msg.charAt(x) != Colors.Red.charAt(0))
  555. {
  556. temp += rainbow[i] + msg.charAt(x);
  557. if(msg.charAt(x) != ' ') i++;
  558. if(i == rainbow.length) i = 0;
  559. x++;
  560. }
  561. //If it reached another color instead of the end
  562. if(x < msg.length() && msg.charAt(x) == '^'
  563. || x < msg.length()
  564. && msg.charAt(x) == Colors.Red.charAt(0) )
  565. {
  566. taste = false;
  567. i = 0;
  568. x--;
  569. }
  570. }
  571. if(xmasparty || recentColor.equals("^x"))
  572. {
  573. //Skip the quake code for xmas
  574. if(recentColor.equals("^x"))
  575. {
  576. x += 2;
  577. }
  578. //Taste keeps it going with xmas if there
  579. //are more lines
  580. xmasparty = true;
  581. //Loop through the message applying the colors
  582. while(x < msg.length() && msg.charAt(x) != '^'
  583. && msg.charAt(x) != Colors.Red.charAt(0))
  584. {
  585. temp += xmas[i] + msg.charAt(x);
  586. if(msg.charAt(x) != ' ') i++;
  587. if(i == xmas.length) i = 0;
  588. x++;
  589. }
  590. //If it reached another color instead of the end
  591. if(x < msg.length() && msg.charAt(x) == '^'
  592. || x < msg.length()
  593. && msg.charAt(x) == Colors.Red.charAt(0) )
  594. {
  595. xmasparty = false;
  596. i = 0;
  597. x--;
  598. }
  599. }
  600. else
  601. {
  602. //Add the color
  603. temp += recentColor;
  604. //Skip these chars
  605. x++;
  606. }
  607. //Otherwise ignore it.
  608. } else {
  609. temp += msg.charAt(x);
  610. }
  611. //Insert the character
  612. } else {
  613. temp += msg.charAt(x);
  614. }
  615. } else {
  616. temp += msg.charAt(x);
  617. }
  618. }
  619. //Replace the message with the colorful message
  620. message[counter] = temp;
  621. counter++;
  622. }
  623. }
  624. return message;
  625. }
  626. }