vMinecraftChat.java 15 KB

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