vMinecraftChat.java 17 KB

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