vMinecraftChat.java 17 KB

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