vMinecraftChat.java 17 KB

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