vMinecraftChat.java 16 KB

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