vMinecraftChat.java 16 KB

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