vminecraftChat.java 13 KB

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