vMinecraftChat.java 16 KB

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