vMinecraftChat.java 15 KB

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