2
0

vMinecraftChat.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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) == Colors.White.charAt(0))
  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.isEmpty())
  129. output = applyColors(playerPrefix.substring(1)) + 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. //Cut off the @ prefix
  231. if(message.startsWith("@"))
  232. message = message.substring(1, message.length());
  233. String[] msg = wordWrap(adminchat + message);
  234. //Get the player from the playerlist to send the message to.
  235. for (Player p: etc.getServer().getPlayerList()) {
  236. //If p is not null
  237. if (p != null) {
  238. //And if p is an admin or has access to adminchat
  239. if (p.isAdmin() || (p.canUseCommand("/adminchat"))) {
  240. //Output the first line
  241. for(String str: msg)
  242. p.sendMessage(str);
  243. }
  244. }
  245. }
  246. //So you can read adminchat from the server console
  247. log.log(Level.INFO, "@" + "<" + getName(player)
  248. + Colors.White +"> " + message);
  249. return true;
  250. }
  251. return false;
  252. }
  253. //=====================================================================
  254. //Function: quote
  255. //Input: Player player: The player talking
  256. // String message: The message to apply the effect to
  257. //Output: boolean: If this feature is enabled
  258. //Use: Displays a message as a quote
  259. //=====================================================================
  260. public static boolean quote(Player player, String message)
  261. {
  262. //Format the name
  263. String playerName = Colors.White + "<" + getName(player)
  264. + Colors.White + "> ";
  265. if(vMinecraftSettings.getInstance().greentext()) {
  266. //Log the chat
  267. log.log(Level.INFO, "<"+player.getName()+"> " +message);
  268. //Get the multi line array
  269. String[] msg = wordWrap(playerName + Colors.LightGreen + message);
  270. //Output the lines
  271. for(String str: msg)
  272. gmsg(Colors.LightGreen + str);
  273. return true;
  274. }
  275. return false;
  276. }
  277. //=====================================================================
  278. //Function: rage
  279. //Input: Player player: The player talking
  280. // String message: The message to apply the effect to
  281. //Output: boolean: If this feature is enabled
  282. //Use: Displays a message in red
  283. //=====================================================================
  284. public static boolean rage(Player player, String message)
  285. {
  286. //Format the name
  287. String playerName = Colors.White + "<"
  288. + getName(player) + Colors.White +"> ";
  289. if (vMinecraftSettings.getInstance().FFF()) {
  290. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  291. //Get the multi line array
  292. String[] msg = wordWrap(playerName + Colors.Red + message);
  293. //Output the message
  294. for(String str: msg)
  295. gmsg(Colors.Red + str);
  296. return true;
  297. }
  298. return false;
  299. }
  300. //=====================================================================
  301. //Function: quakeColors
  302. //Input: Player player: The player talking
  303. // String message: The message to apply the effect to
  304. //Output: boolean: If this feature is enabled
  305. //Use: Displays a message in red
  306. //=====================================================================
  307. public static boolean quakeColors(Player player, String message)
  308. {
  309. //Format the name
  310. String playerName = Colors.White + "<"
  311. + getName(player) + Colors.White +"> ";
  312. if(vMinecraftSettings.getInstance().quakeColors()) {
  313. //Log the chat
  314. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  315. //Get the multi line array
  316. String[] msg = wordWrap(playerName + message);
  317. //Apply colors to the lines
  318. applyColors(msg);
  319. //Output the message
  320. for(String str: msg)
  321. gmsg(str);
  322. //Loop through the string finding the color codes and inserting them
  323. return true;
  324. }
  325. return false;
  326. }
  327. //=====================================================================
  328. //Function: applyColors
  329. //Input: String[] message: The lines to be colored
  330. //Output: String[]: The lines, but colorful
  331. //Use: Colors each line
  332. //=====================================================================
  333. public static String[] applyColors(String[] message)
  334. {
  335. if(message != null && message[0] != null && !message[0].isEmpty()){
  336. //The color to start the line with
  337. String recentColor = Colors.White;
  338. //Go through each line
  339. int counter = 0;
  340. for(String msg: message)
  341. {
  342. //Start the line with the most recent color
  343. String temp = recentColor;
  344. //Loop through looking for a color code
  345. for(int x = 0; x< msg.length(); x++)
  346. {
  347. //If the char is a ^ or §
  348. if(msg.charAt(x) == '^' || msg.charAt(x) == Colors.White.charAt(0))
  349. {
  350. if(x != msg.length() - 1)
  351. {
  352. //If the following character is a color code
  353. if(vMinecraftChat.colorChange(msg.charAt(x+1)) != null)
  354. {
  355. //Set the most recent color to the new color
  356. recentColor = vMinecraftChat.colorChange(msg.charAt(x+1));
  357. //Add the color
  358. temp += recentColor;
  359. //Skip these chars
  360. x++;
  361. //Otherwise ignore it.
  362. } else {
  363. temp += msg.charAt(x);
  364. }
  365. //Insert the character
  366. } else {
  367. temp += msg.charAt(x);
  368. }
  369. } else {
  370. temp += msg.charAt(x);
  371. }
  372. }
  373. //Replace the message with the colorful message
  374. message[counter] = temp;
  375. counter++;
  376. }
  377. }
  378. return message;
  379. }
  380. //=====================================================================
  381. //Function: applyColors
  382. //Input: String message: The line to be colored
  383. //Output: String: The line, but colorful
  384. //Use: Colors a line
  385. //=====================================================================
  386. public static String applyColors(String message)
  387. {
  388. return applyColors(message, Colors.White);
  389. }
  390. //=====================================================================
  391. //Function: applyColors
  392. //Input: String message: The line to be colored
  393. // String color: The color to start the line with
  394. //Output: String: The line, but colorful
  395. //Use: Colors a line
  396. //=====================================================================
  397. public static String applyColors(String message, String color)
  398. {
  399. if(message != null && !message.isEmpty())
  400. {
  401. //The color to start the line with
  402. if(color == null)
  403. color = Colors.White;
  404. //Start the line with the most recent color
  405. String temp = color;
  406. //Loop through looking for a color code
  407. for(int x = 0; x< message.length(); x++)
  408. {
  409. //If the char is a ^ or '§'
  410. if(message.charAt(x) == '^' || message.charAt(x) == Colors.White.charAt(0))
  411. {
  412. if(x != message.length() - 1)
  413. {
  414. //If the following character is a color code
  415. if(vMinecraftChat.colorChange(message.charAt(x+1)) != null)
  416. {
  417. //Set the most recent color to the new color
  418. color = vMinecraftChat.colorChange(message.charAt(x+1));
  419. //Add the color
  420. temp += color;
  421. //Skip these chars
  422. x++;
  423. //Otherwise ignore it.
  424. } else {
  425. temp += message.charAt(x);
  426. }
  427. //Insert the character
  428. } else {
  429. temp += message.charAt(x);
  430. }
  431. //Insert the character
  432. } else {
  433. temp += message.charAt(x);
  434. }
  435. }
  436. message = temp;
  437. }
  438. return message;
  439. }
  440. }