vMinecraftChat.java 17 KB

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