vMinecraftChat.java 17 KB

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