vminecraftChat.java 14 KB

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