vMinecraftChat.java 18 KB

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