2
0

vminecraftChat.java 11 KB

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