vMinecraftChat.java 17 KB

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