vminecraftChat.java 14 KB

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