vMinecraftChat.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. //Get the prefix
  199. String playerPrefix = player.getPrefix();
  200. //Add the name
  201. String 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. if(playerPrefix != null && !playerPrefix.isEmpty())
  207. output = applyColors(playerPrefix.substring(1)) + output;
  208. //Return the name
  209. return output;
  210. }
  211. //=====================================================================
  212. //Function: colorChange
  213. //Input: char colour: The color code to find the color for
  214. //Output: String: The color that the code identified
  215. //Use: Finds a color giving a color code
  216. //=====================================================================
  217. public static String colorChange(char colour)
  218. {
  219. String color = "";
  220. switch(colour)
  221. {
  222. case '0':
  223. color = Colors.Black;
  224. break;
  225. case '1':
  226. color = Colors.Navy;
  227. break;
  228. case '2':
  229. color = Colors.Green;
  230. break;
  231. case '3':
  232. color = Colors.Blue;
  233. break;
  234. case '4':
  235. color = Colors.Red;
  236. break;
  237. case '5':
  238. color = Colors.Purple;
  239. break;
  240. case '6':
  241. color = Colors.Gold;
  242. break;
  243. case '7':
  244. color = Colors.LightGray;
  245. break;
  246. case '8':
  247. color = Colors.Gray;
  248. break;
  249. case '9':
  250. color = Colors.DarkPurple;
  251. break;
  252. case 'a':
  253. color = Colors.LightGreen;
  254. break;
  255. case 'b':
  256. color = Colors.LightBlue;
  257. break;
  258. case 'c':
  259. color = Colors.Rose;
  260. break;
  261. case 'd':
  262. color = Colors.LightPurple;
  263. break;
  264. case 'e':
  265. color = Colors.Yellow;
  266. break;
  267. case 'f':
  268. color = Colors.White;
  269. break;
  270. case 'A':
  271. color = Colors.LightGreen;
  272. break;
  273. case 'B':
  274. color = Colors.LightBlue;
  275. break;
  276. case 'C':
  277. color = Colors.Rose;
  278. break;
  279. case 'D':
  280. color = Colors.LightPurple;
  281. break;
  282. case 'E':
  283. color = Colors.Yellow;
  284. break;
  285. case 'F':
  286. color = Colors.White;
  287. break;
  288. case 'R':
  289. color = "~";
  290. break;
  291. case 'r':
  292. color = "~";
  293. break;
  294. default:
  295. color = null;
  296. break;
  297. }
  298. return color;
  299. }
  300. //=====================================================================
  301. //Function: adminChat
  302. //Input: Player player: The player talking
  303. // String message: The message to apply the effect to
  304. //Output: boolean: If this feature is enabled
  305. //Use: Sends messages only to admins
  306. //=====================================================================
  307. public static boolean adminChat(Player player, String message){
  308. //Check if the player can use this feature
  309. if(player.isAdmin() || player.canUseCommand("/adminchat"))
  310. {
  311. //Special formatting for adminchat {Username}
  312. String adminchat = Colors.DarkPurple + "{" + getName(player)
  313. + Colors.DarkPurple +"} ";
  314. //Cut off the @ prefix
  315. if(message.startsWith("@"))
  316. message = message.substring(1, message.length());
  317. //Get the player from the playerlist to send the message to.
  318. for (Player p: etc.getServer().getPlayerList()) {
  319. //If p is not null
  320. if (p != null) {
  321. //And if p is an admin or has access to adminchat send message
  322. if (p.isAdmin() || (p.canUseCommand("/adminchat"))) {
  323. sendMessage(player, p, adminchat + message);
  324. }
  325. }
  326. }
  327. //So you can read adminchat from the server console
  328. log.log(Level.INFO, "@" + "<" + player.getName()
  329. + Colors.White +"> " + message);
  330. return true;
  331. }
  332. return false;
  333. }
  334. //=====================================================================
  335. //Function: quote
  336. //Input: Player player: The player talking
  337. // String message: The message to apply the effect to
  338. //Output: boolean: If this feature is enabled
  339. //Use: Displays a message as a quote
  340. //=====================================================================
  341. public static boolean quote(Player player, String message)
  342. {
  343. //Format the name
  344. String playerName = Colors.White + "<" + getName(player)
  345. + Colors.White + "> ";
  346. if(vMinecraftSettings.getInstance().greentext()) {
  347. //Log the chat
  348. log.log(Level.INFO, "<"+player.getName()+"> " + message);
  349. //Output the message
  350. gmsg(player, playerName + Colors.LightGreen + message);
  351. return true;
  352. }
  353. return false;
  354. }
  355. //=====================================================================
  356. //Function: rage
  357. //Input: Player player: The player talking
  358. // String message: The message to apply the effect to
  359. //Output: boolean: If this feature is enabled
  360. //Use: Displays a message in red
  361. //=====================================================================
  362. public static boolean rage(Player player, String message)
  363. {
  364. //Format the name
  365. String playerName = Colors.White + "<"
  366. + getName(player) + Colors.White +"> ";
  367. if (vMinecraftSettings.getInstance().FFF()) {
  368. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  369. //Output the message
  370. gmsg(player, playerName + Colors.Red + message);
  371. return true;
  372. }
  373. return false;
  374. }
  375. //=====================================================================
  376. //Function: quakeColors
  377. //Input: Player player: The player talking
  378. // String message: The message to apply the effect to
  379. //Output: boolean: If this feature is enabled
  380. //Use: Displays a message in red
  381. //=====================================================================
  382. public static boolean quakeColors(Player player, String message)
  383. {
  384. //Format the name
  385. String playerName = Colors.White + "<"
  386. + getName(player) + Colors.White +"> ";
  387. if(vMinecraftSettings.getInstance().quakeColors()) {
  388. //Log the chat
  389. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  390. //Output the message
  391. gmsg(player, playerName + message);
  392. //Loop through the string finding the color codes and inserting them
  393. return true;
  394. }
  395. return false;
  396. }
  397. //=====================================================================
  398. //Function: emote
  399. //Input: Player player: The player talking
  400. // String message: The message to apply the effect to
  401. //Output: boolean: If this feature is enabled
  402. //Use: /me but with our custom colors applied
  403. //=====================================================================
  404. public static boolean emote(Player player, String message)
  405. {
  406. gmsg(player, "* " + getName(player) + " " + Colors.White + message);
  407. return true;
  408. }
  409. //=====================================================================
  410. //Function: applyColors
  411. //Input: String[] message: The lines to be colored
  412. //Output: String[]: The lines, but colorful
  413. //Use: Colors each line
  414. //=====================================================================
  415. public static String[] applyColors(String[] message)
  416. {
  417. if(message != null && message[0] != null && !message[0].isEmpty()){
  418. //The color to start the line with
  419. String recentColor = Colors.White;
  420. //Go through each line
  421. int counter = 0;
  422. for(String msg: message)
  423. {
  424. //Start the line with the most recent color
  425. String temp = recentColor;
  426. //Loop through looking for a color code
  427. for(int x = 0; x< msg.length(); x++)
  428. {
  429. //If the char is a ^ or �
  430. if(msg.charAt(x) == '^' || msg.charAt(x) == Colors.White.charAt(0))
  431. {
  432. if(x != msg.length() - 1)
  433. {
  434. //If the following character is a color code
  435. if(vMinecraftChat.colorChange(msg.charAt(x+1)) != null)
  436. {
  437. //Set the most recent color to the new color
  438. recentColor = vMinecraftChat.colorChange(msg.charAt(x+1));
  439. //Add the color
  440. temp += recentColor;
  441. //Skip these chars
  442. x++;
  443. //Otherwise ignore it.
  444. } else {
  445. temp += msg.charAt(x);
  446. }
  447. //Insert the character
  448. } else {
  449. temp += msg.charAt(x);
  450. }
  451. } else {
  452. temp += msg.charAt(x);
  453. }
  454. }
  455. //Replace the message with the colorful message
  456. message[counter] = temp;
  457. counter++;
  458. }
  459. }
  460. return message;
  461. }
  462. //=====================================================================
  463. //Function: applyColors
  464. //Input: String message: The line to be colored
  465. //Output: String: The line, but colorful
  466. //Use: Colors a line
  467. //=====================================================================
  468. public static String applyColors(String message)
  469. {
  470. return applyColors(message, Colors.White);
  471. }
  472. //=====================================================================
  473. //Function: applyColors
  474. //Input: String message: The line to be colored
  475. // String color: The color to start the line with
  476. //Output: String: The line, but colorful
  477. //Use: Colors a line
  478. //=====================================================================
  479. public static String applyColors(String message, String color)
  480. {
  481. if(message != null && !message.isEmpty())
  482. {
  483. //The color to start the line with
  484. if(color == null)
  485. color = Colors.White;
  486. //Start the line with the most recent color
  487. String temp = color;
  488. //Loop through looking for a color code
  489. for(int x = 0; x< message.length(); x++)
  490. {
  491. //If the char is a ^ or '�'
  492. if(message.charAt(x) == '^' || message.charAt(x) == Colors.White.charAt(0))
  493. {
  494. if(x != message.length() - 1)
  495. {
  496. //If the following character is a color code
  497. if(vMinecraftChat.colorChange(message.charAt(x+1)) != null)
  498. {
  499. //Set the most recent color to the new color
  500. color = vMinecraftChat.colorChange(message.charAt(x+1));
  501. //Add the color
  502. temp += color;
  503. //Skip these chars
  504. x++;
  505. //Otherwise ignore it.
  506. } else {
  507. temp += message.charAt(x);
  508. }
  509. //Insert the character
  510. } else {
  511. temp += message.charAt(x);
  512. }
  513. //Insert the character
  514. } else {
  515. temp += message.charAt(x);
  516. }
  517. }
  518. message = temp;
  519. }
  520. return message;
  521. }
  522. }