vminecraftChat.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. private static int msgLength(String str){
  61. int length = 0;
  62. for(int x = 0; x<str.length(); x++)
  63. {
  64. if(str.charAt(x) == '§')
  65. x++;
  66. else if("i;,.:|!".indexOf(str.charAt(x)) != -1)
  67. length+=2;
  68. else if("l'".indexOf(str.charAt(x)) != -1)
  69. length+=3;
  70. else if("tI[]".indexOf(str.charAt(x)) != -1)
  71. length+=4;
  72. else if("kf{}<>\"*()".indexOf(str.charAt(x)) != -1)
  73. length+=5;
  74. else if("hequcbrownxjmpsvazydgTHEQUCKBROWNFXJMPSVLAZYDG1234567890#\\/?$%-=_+&".indexOf(str.charAt(x)) != -1)
  75. length+=6;
  76. else if("@~".indexOf(str.charAt(x)) != -1)
  77. length+=7;
  78. else if(str.charAt(x)==' ')
  79. length+=4;
  80. }
  81. return length;
  82. }
  83. public static String rainbow(String msg){
  84. String temp = "";
  85. //The array of colors to use
  86. String[] rainbow = new String[] {Colors.Red, Colors.Rose,
  87. Colors.Yellow, Colors.Green, Colors.Blue,
  88. Colors.LightPurple, Colors.Purple};
  89. int counter=0;
  90. //Loop through the message applying the colors
  91. for(int x=0; x<msg.length(); x++)
  92. {
  93. temp+=rainbow[counter]+msg.charAt(x);
  94. if(msg.charAt(x)!=' ') counter++;
  95. if(counter==7) counter = 0;
  96. }
  97. return temp;
  98. }
  99. //=====================================================================
  100. //Function: nameColor
  101. //Input: Player player: The player to get name as color
  102. //Output: String: The name colored
  103. //Use: Returns the colored name;
  104. //=====================================================================
  105. public static String nameColor(Player player){
  106. //Get the prefix
  107. String[] playerPrefix = new String[]{player.getPrefix()};
  108. //Add the name
  109. String output = player.getName();
  110. //Add the color if there is one
  111. if(player.getColor() != null && player.getColor() != "")
  112. output = player.getColor().substring(0,2) + output;
  113. //Add the prefix if there is one
  114. if(playerPrefix[0] != null && playerPrefix[0] != "")
  115. output = applyColors(playerPrefix)[0].substring(3) + output;
  116. //Return the name
  117. return output;
  118. }
  119. //=====================================================================
  120. //Function: colorChange
  121. //Input: char colour: The color code to find the color for
  122. //Output: String: The color that the code identified
  123. //Use: Finds a color giving a color code
  124. //=====================================================================
  125. public static String colorChange(char colour)
  126. {
  127. String color = "";
  128. switch(colour)
  129. {
  130. case '0':
  131. color = Colors.Black;
  132. break;
  133. case '1':
  134. color = Colors.Navy;
  135. break;
  136. case '2':
  137. color = Colors.Green;
  138. break;
  139. case '3':
  140. color = Colors.Blue;
  141. break;
  142. case '4':
  143. color = Colors.Red;
  144. break;
  145. case '5':
  146. color = Colors.Purple;
  147. break;
  148. case '6':
  149. color = Colors.Gold;
  150. break;
  151. case '7':
  152. color = Colors.LightGray;
  153. break;
  154. case '8':
  155. color = Colors.Gray;
  156. break;
  157. case '9':
  158. color = Colors.DarkPurple;
  159. break;
  160. case 'a':
  161. color = Colors.LightGreen;
  162. break;
  163. case 'b':
  164. color = Colors.LightBlue;
  165. break;
  166. case 'c':
  167. color = Colors.Rose;
  168. break;
  169. case 'd':
  170. color = Colors.LightPurple;
  171. break;
  172. case 'e':
  173. color = Colors.Yellow;
  174. break;
  175. case 'f':
  176. color = Colors.White;
  177. break;
  178. case 'A':
  179. color = Colors.LightGreen;
  180. break;
  181. case 'B':
  182. color = Colors.LightBlue;
  183. break;
  184. case 'C':
  185. color = Colors.Rose;
  186. break;
  187. case 'D':
  188. color = Colors.LightPurple;
  189. break;
  190. case 'E':
  191. color = Colors.Yellow;
  192. break;
  193. case 'F':
  194. color = Colors.White;
  195. break;
  196. default:
  197. color = Colors.White;
  198. break;
  199. }
  200. return color;
  201. }
  202. //=====================================================================
  203. //Function: adminChat
  204. //Input: Player player: The player talking
  205. // String message: The message to apply the effect to
  206. //Output: boolean: If this feature is enabled
  207. //Use: Sends messages only to admins
  208. //=====================================================================
  209. public static boolean adminChat(Player player, String message){
  210. //Check if the player can use this feature
  211. if(player.isAdmin() || player.canUseCommand("/adminchat"))
  212. {
  213. //Special formatting for adminchat {Username}
  214. String adminchat = Colors.DarkPurple + "{" + nameColor(player)
  215. + Colors.DarkPurple +"}" + Colors.White + " ";
  216. String[] msg = wordWrap(adminchat + message.substring(1, message.length()));
  217. //Get the player from the playerlist to send the message to.
  218. for (Player p: etc.getServer().getPlayerList()) {
  219. //If p is not null
  220. if (p != null) {
  221. //And if p is an admin or has access to adminchat
  222. if (p.isAdmin() || (p.canUseCommand("/adminchat"))) {
  223. //Output the first line
  224. for(String str: msg)
  225. p.sendMessage(str);
  226. }
  227. }
  228. }
  229. //So you can read adminchat from the server console
  230. log.log(Level.INFO, "@" + "<" + nameColor(player)
  231. + Colors.White +"> " + message);
  232. return true;
  233. }
  234. return false;
  235. }
  236. //=====================================================================
  237. //Function: quote
  238. //Input: Player player: The player talking
  239. // String message: The message to apply the effect to
  240. //Output: boolean: If this feature is enabled
  241. //Use: Displays a message as a quote
  242. //=====================================================================
  243. public static boolean quote(Player player, String message)
  244. {
  245. //Format the name
  246. String playerName = Colors.White + "<" + nameColor(player)
  247. + Colors.White + "> ";
  248. if(vminecraftSettings.getInstance().greentext()) {
  249. //Log the chat
  250. log.log(Level.INFO, "<"+player.getName()+"> " +message);
  251. //Get the multi line array
  252. String[] msg = wordWrap(playerName + Colors.LightGreen + message);
  253. //Output the lines
  254. for(String str: msg)
  255. gmsg(Colors.LightGreen + str);
  256. return true;
  257. }
  258. return false;
  259. }
  260. //=====================================================================
  261. //Function: rage
  262. //Input: Player player: The player talking
  263. // String message: The message to apply the effect to
  264. //Output: boolean: If this feature is enabled
  265. //Use: Displays a message in red
  266. //=====================================================================
  267. public static boolean rage(Player player, String message)
  268. {
  269. //Format the name
  270. String playerName = Colors.White + "<"
  271. + nameColor(player) + Colors.White +"> ";
  272. if (vminecraftSettings.getInstance().FFF()) {
  273. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  274. //Get the multi line array
  275. String[] msg = wordWrap(playerName + Colors.Red + message);
  276. //Output the message
  277. for(String str: msg)
  278. gmsg(Colors.Red + str);
  279. return true;
  280. }
  281. return false;
  282. }
  283. //=====================================================================
  284. //Function: quakeColors
  285. //Input: Player player: The player talking
  286. // String message: The message to apply the effect to
  287. //Output: boolean: If this feature is enabled
  288. //Use: Displays a message in red
  289. //=====================================================================
  290. public static boolean quakeColors(Player player, String message)
  291. {
  292. //Format the name
  293. String playerName = Colors.White + "<"
  294. + nameColor(player) + Colors.White +"> ";
  295. if(vminecraftSettings.getInstance().quakeColors()) {
  296. //Log the chat
  297. log.log(Level.INFO, "<"+player.getName()+"> "+message);
  298. //Get the multi line array
  299. String[] msg = wordWrap(playerName + message);
  300. //Apply colors to the lines
  301. applyColors(msg);
  302. //Output the message
  303. for(String str: msg)
  304. gmsg(str);
  305. //Loop through the string finding the color codes and inserting them
  306. return true;
  307. }
  308. return false;
  309. }
  310. //=====================================================================
  311. //Function: applyColors
  312. //Input: String[] message: The lines to be colored
  313. //Output: String[]: The lines, but colorful
  314. //Use: Colors each line
  315. //=====================================================================
  316. private static String[] applyColors(String[] message)
  317. {
  318. //The color to start the line with
  319. String recentColor = Colors.White;
  320. //Go through each line
  321. int counter = 0;
  322. for(String msg: message)
  323. {
  324. //Start the line with the most recent color
  325. String temp = recentColor;
  326. //Loop through looking for a color code
  327. for(int x = 0; x< msg.length(); x++)
  328. {
  329. if(msg.charAt(x)=='^' && x != msg.length() - 1)
  330. {
  331. //Set the most recent color to the new color
  332. recentColor = vminecraftChat.colorChange(msg.charAt(x+1));
  333. temp += recentColor;
  334. x++;
  335. }
  336. else{
  337. temp += msg.charAt(x);
  338. }
  339. }
  340. //Replace the message with the colorful message
  341. message[counter] = temp;
  342. counter++;
  343. }
  344. return message;
  345. }
  346. }