/src/ChatTools.java

https://github.com/Theino/Towny-Obsidian · Java · 103 lines · 69 code · 19 blank · 15 comment · 13 complexity · c6a5d1429572603188b1c5d64a1ec85c MD5 · raw file

  1. import java.util.*;
  2. import java.util.logging.Logger;
  3. public class ChatTools {
  4. public static final int lineLength = 54;
  5. protected static final Logger log = Logger.getLogger("Minecraft");
  6. /*public static ArrayList<String> list(String[] args) {
  7. return list(Arrays.asList(args));
  8. }*/
  9. /*public static ArrayList<String> list(List<String> args) {
  10. if (args.size() > 0) {
  11. String line = "";
  12. for (int i=0; i<args.size()-1;i++)
  13. line += args.get(i) + ", ";
  14. line += args.get(args.size()-1);
  15. return color(line);
  16. }
  17. return new ArrayList<String>();
  18. }*/
  19. public static ArrayList<String> list(Object[] args) {
  20. return list(Arrays.asList(args));
  21. }
  22. public static ArrayList<String> list(List<Object> args) {
  23. if (args.size() > 0) {
  24. String line = "";
  25. for (int i=0; i<args.size()-1;i++)
  26. line += args.get(i) + ", ";
  27. line += args.get(args.size()-1).toString();
  28. return color(line);
  29. }
  30. return new ArrayList<String>();
  31. }
  32. public static ArrayList<String> wordWrap(String[] tokens) {
  33. ArrayList<String> out = new ArrayList<String>();
  34. out.add("");
  35. for (String s : tokens) {
  36. if (stripColour(out.get(out.size()-1)).length() + stripColour(s).length() + 1 > lineLength) {
  37. out.add("");
  38. }
  39. out.set(out.size()-1, out.get(out.size()-1) + s + " ");
  40. }
  41. return out;
  42. }
  43. public static ArrayList<String> color(String line) {
  44. ArrayList<String> out = wordWrap(line.split(" "));
  45. String c = "f";
  46. for (int i = 0; i < out.size(); i++) {
  47. if (!out.get(i).startsWith("§"))
  48. out.set(i, "§" + c + out.get(i));
  49. for (int index = 0; index < lineLength; index++) {
  50. try {
  51. if (out.get(i).substring(index, index+1).equalsIgnoreCase("\u00A7")) {
  52. c = out.get(i).substring(index+1, index+2);
  53. }
  54. } catch(Exception e) {}
  55. }
  56. }
  57. return out;
  58. }
  59. public static String stripColour(String s) {
  60. String out = "";
  61. for (int i=0; i<s.length()-1; i++) {
  62. String c = s.substring(i, i+1);
  63. if (c.equals("§")) {
  64. i += 1;
  65. } else {
  66. out += c;
  67. }
  68. }
  69. return out;
  70. }
  71. public static String formatTitle(String title) {
  72. return Colors.Gold + "_____[ " + Colors.Yellow + title + Colors.Gold + " ]_____";
  73. }
  74. public static void main(String[] args) {
  75. String[] players = {"dude", "bowie", "blarg", "sonbitch", "songoku", "pacman", "link", "stacker", "hacker", "newb" };
  76. for (String line : ChatTools.list(players))
  77. System.out.println(line);
  78. String testLine = "Loren Ipsum blarg voila tssssssh, boom wakka wakka §apacman on a boat bitch. From the boat union. Beata lingiushtically §1nootchie lolk erness.";
  79. for (String line : ChatTools.color(testLine))
  80. System.out.println(line);
  81. }
  82. }