Advertisement
Guest User

Untitled

a guest
Nov 16th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.40 KB | None | 0 0
  1. /**
  2.  * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
  3.  * Copyright (C) 2004 Sam Hocevar <[email protected]>
  4.  *
  5.  * Everyone is permitted to copy and distribute verbatim or modified copies of
  6.  * this license document, and changing it is allowed as long as the name is
  7.  * changed.
  8.  *
  9.  * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING,
  10.  * DISTRIBUTION AND MODIFICATION
  11.  *
  12.  * 0. You just DO WHAT THE FUCK YOU WANT TO.
  13.  */
  14. /*
  15.  * Base Class taken from Help
  16.  * https://github.com/tkelly910/Help
  17.  *
  18.  */
  19. package be.Balor.Tools.Help.String;
  20.  
  21. import java.util.LinkedList;
  22.  
  23. public class ACMinecraftFontWidthCalculator {
  24.  
  25.     public final static int chatwidth = 318; // 325
  26.     public static String charWidthIndexIndex = " !\"#$%&'()*+,-./"
  27.             + "0123456789:;<=>?" + "@ABCDEFGHIJKLMNO" + "PQRSTUVWXYZ[\\]^_"
  28.             + "'abcdefghijklmno" + "pqrstuvwxyz{|}~⌂"
  29.             + "ÇüéâäàåçêëèïîìÄÅ"
  30.             + "ÉæÆôöòûùÿÖÜø£Ø×ƒ"
  31.             + "áíóúñѪº¿®¬½¼¡«»";
  32.     public static int[] charWidths = {4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6,
  33.             2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6, 7, 6, 6, 6,
  34.             6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  35.             6, 4, 6, 4, 6, 6, 3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6,
  36.             6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6, 6, 6, 6, 6, 6, 6,
  37.             6, 6, 6, 6, 6, 4, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  38.             6, 6, 4, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2,
  39.             6,
  40.             6,
  41.             // not sure what tkelly made these rows for..
  42.             8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6, 9, 9, 9, 9, 9, 9,
  43.             9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9,
  44.             9, 5, 9, 9, 8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7, 7, 7,
  45.             7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1};
  46.  
  47.     // chat limmitation: repetitions of characters is limmited to 119 per line
  48.     // so: repeating !'s will not fill a line
  49.  
  50.     public static int getStringWidth(final String s) {
  51.         int i = 0;
  52.         if (s != null) {
  53.             for (final char c : s.replaceAll("\\u00A7.", "").toCharArray()) {
  54.                 i += getCharWidth(c);
  55.             }
  56.         }
  57.         return i;
  58.     }
  59.  
  60.     public static int getCharWidth(final char c) {
  61.         // return getCharWidth(c, 0);
  62.         final int k = charWidthIndexIndex.indexOf(c);
  63.         if (c != '\247' && k >= 0) {
  64.             return charWidths[k];
  65.         }
  66.         return 0;
  67.     }
  68.  
  69.     public static int getCharWidth(final char c, final int defaultReturn) {
  70.         final int k = charWidthIndexIndex.indexOf(c);
  71.         if (c != '\247' && k >= 0) {
  72.             return charWidths[k];
  73.         }
  74.         return defaultReturn;
  75.     }
  76.  
  77.     public static String uncoloredStr(final String s) {
  78.         return s != null ? s.replaceAll("\\u00A7.", "") : s;
  79.     }
  80.  
  81.     /**
  82.      * pads str on the right with pad (left-align)
  83.      *
  84.      * @param str
  85.      *            string to format
  86.      * @param len
  87.      *            spaces to pad
  88.      * @param pad
  89.      *            character to use when padding
  90.      * @return str with padding appended
  91.      */
  92.     public static String strPadRight(final String str, int len, final char pad) {
  93.         // for purposes of this function, assuming a normal char to be 6
  94.         len *= 6;
  95.         len -= getStringWidth(str);
  96.         return str + unformattedStrRepeat(pad, len / getCharWidth(pad, 6));
  97.     }
  98.  
  99.     public static String strPadRightChat(final String str, int abslen,
  100.             final char pad) {
  101.         abslen -= getStringWidth(str);
  102.         return str + unformattedStrRepeat(pad, abslen / getCharWidth(pad, 6));
  103.     }
  104.  
  105.     public static String strPadRightChat(final String str, int abslen) {
  106.         abslen -= getStringWidth(str);
  107.         return str + unformattedStrRepeat(' ', abslen / getCharWidth(' ', 6));
  108.     }
  109.  
  110.     public static String strPadRightChat(final String str, final char pad) {
  111.         final int width = chatwidth - getStringWidth(str);
  112.         return str + unformattedStrRepeat(pad, width / getCharWidth(pad, 6));
  113.     }
  114.  
  115.     public static String strPadRightChat(final String str) {
  116.         final int width = chatwidth - getStringWidth(str);
  117.         return str + unformattedStrRepeat(' ', width / getCharWidth(' ', 6));
  118.     }
  119.  
  120.     /**
  121.      * pads str on the left with pad (right-align)
  122.      *
  123.      * @param str
  124.      *            string to format
  125.      * @param len
  126.      *            spaces to pad
  127.      * @param pad
  128.      *            character to use when padding
  129.      * @return str with padding prepended
  130.      */
  131.     public static String strPadLeft(final String str, int len, final char pad) {
  132.         // for purposes of this function, assuming a normal char to be 6
  133.         len *= 6;
  134.         len -= getStringWidth(str);
  135.         return unformattedStrRepeat(pad, len / getCharWidth(pad, 6)) + str;
  136.     }
  137.  
  138.     public static String strPadLeftChat(final String str, int abslen,
  139.             final char pad) {
  140.         abslen -= getStringWidth(str);
  141.         return unformattedStrRepeat(pad, abslen / getCharWidth(pad, 6)).concat(
  142.                 str);
  143.     }
  144.  
  145.     public static String strPadLeftChat(final String str, int abslen) {
  146.         abslen -= getStringWidth(str);
  147.         return unformattedStrRepeat(' ', abslen / getCharWidth(' ', 6)).concat(
  148.                 str);
  149.     }
  150.  
  151.     public static String strPadLeftChat(final String str, final char pad) {
  152.         final int width = chatwidth - getStringWidth(str);
  153.         return unformattedStrRepeat(pad, width / getCharWidth(pad, 6)).concat(
  154.                 str);
  155.     }
  156.  
  157.     public static String strPadLeftChat(final String str) {
  158.         final int width = chatwidth - getStringWidth(str);
  159.         return unformattedStrRepeat(' ', width / getCharWidth(' ', 6)).concat(
  160.                 str);
  161.     }
  162.  
  163.     /**
  164.      * pads str on the left & right with pad (center-align)
  165.      *
  166.      * @param str
  167.      *            string to format
  168.      * @param len
  169.      *            spaces to pad
  170.      * @param pad
  171.      *            character to use when padding
  172.      * @return str centered with pad
  173.      */
  174.     public static String strPadCenter(final String str, int len, final char pad) {
  175.         // for purposes of this function, assuming a normal char to be 6
  176.         len *= 6;
  177.         len -= getStringWidth(str);
  178.         final int padwid = getCharWidth(pad, 6);
  179.         final int prepad = (len / padwid) / 2;
  180.         len -= prepad * padwid;
  181.         return unformattedStrRepeat(pad, prepad) + str
  182.                 + unformattedStrRepeat(pad, len / padwid);
  183.     }
  184.  
  185.     public static String strPadCenterChat(final String str, int abslen,
  186.             final char pad) {
  187.         abslen -= getStringWidth(str);
  188.         final int padwid = getCharWidth(pad, 6);
  189.         final int prepad = (abslen / padwid) / 2;
  190.         abslen -= prepad * padwid;
  191.         return unformattedStrRepeat(pad, prepad) + str
  192.                 + unformattedStrRepeat(pad, abslen / padwid);
  193.     }
  194.  
  195.     public static String strPadCenterChat(final String str, final char pad) {
  196.         int width = chatwidth - getStringWidth(str);
  197.         final int padwid = getCharWidth(pad, 6);
  198.         final int prepad = (width / padwid) / 2;
  199.         width -= prepad * padwid;
  200.         return unformattedStrRepeat(pad, prepad) + str
  201.                 + unformattedStrRepeat(pad, width / padwid);
  202.     }
  203.  
  204.     public static int strLen(final String str) {
  205.         if (!str.contains("\u00A7")) {
  206.             return str.length();
  207.         }
  208.         // just searching for \u00A7.
  209.         return str.replaceAll("\\u00A7.", "").length();
  210.     }
  211.  
  212.     public static String unformattedPadRight(String str, final int len,
  213.             final char pad) {
  214.         for (int i = strLen(str); i < len; ++i) {
  215.             str += pad;
  216.         }
  217.         return str;
  218.     }
  219.  
  220.     public static String unformattedPadLeft(final String str, final int len,
  221.             final char pad) {
  222.         return unformattedStrRepeat(pad, len - strLen(str)) + str;
  223.     }
  224.  
  225.     public static String unformattedPadCenter(final String str, int len,
  226.             final char pad) {
  227.         len -= strLen(str);
  228.         final int prepad = len / 2;
  229.         return unformattedStrRepeat(pad, prepad) + str
  230.                 + unformattedStrRepeat(pad, len - prepad);
  231.     }
  232.  
  233.     public static String unformattedStrRepeat(final char ch, final int len) {
  234.         String str = "";
  235.         for (int i = 0; i < len; ++i) {
  236.             str += ch;
  237.         }
  238.         return str;
  239.     }
  240.  
  241.     public static String strTrim(final String str, final int length) {
  242.         if (uncoloredStr(str).length() > length) {
  243.             int width = length;
  244.             String ret = "";
  245.             boolean lastCol = false;
  246.             for (final char c : str.toCharArray()) {
  247.                 if (c == '\u00A7') {
  248.                     ret += c;
  249.                     lastCol = true;
  250.                 } else {
  251.                     if (!lastCol) {
  252.                         if (width - 1 >= 0) {
  253.                             width -= 1;
  254.                             ret += c;
  255.                         } else {
  256.                             return ret;
  257.                         }
  258.                     } else {
  259.                         ret += c;
  260.                         lastCol = false;
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.         return str;
  266.     }
  267.  
  268.     public static String strChatTrim(final String str) {
  269.         return strChatTrim(str, chatwidth);
  270.     }
  271.  
  272.     public static String strChatTrim(final String str, final int absLen) {
  273.         int width = getStringWidth(str);
  274.         if (width > absLen) {
  275.             width = absLen;
  276.             String ret = "";
  277.             boolean lastCol = false;
  278.             for (final char c : str.toCharArray()) {
  279.                 if (c == '\u00A7') {
  280.                     ret += c;
  281.                     lastCol = true;
  282.                 } else {
  283.                     if (!lastCol) {
  284.                         final int w = getCharWidth(c);
  285.                         if (width - w >= 0) {
  286.                             width -= w;
  287.                             ret += c;
  288.                         } else {
  289.                             return ret;
  290.                         }
  291.                     } else {
  292.                         ret += c;
  293.                         lastCol = false;
  294.                     }
  295.                 }
  296.             }
  297.         }
  298.         return str;
  299.     }
  300.  
  301.     public static String strChatWordWrap(final String str) {
  302.         return strChatWordWrap(str, 0, ' ');
  303.     }
  304.  
  305.     public static String strChatWordWrap(final String str, final int tab) {
  306.         return strChatWordWrap(str, tab, ' ');
  307.     }
  308.  
  309.     public static String strChatWordWrap(String str, final int tab,
  310.             final char tabChar) {
  311.         String ret = "";
  312.         while (str.length() > 0) {
  313.             // find last char of first line
  314.             if (getStringWidth(str) <= chatwidth) {
  315.                 return (ret.length() > 0 ? ret + "\n"
  316.                         + unformattedStrRepeat(tabChar, tab) : "").concat(str);
  317.             }
  318.             final String line1 = strChatTrim(str);
  319.             int lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  320.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  321.                 --lastPos;
  322.             }
  323.             if (lastPos == 0) {
  324.                 lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  325.             }
  326.             // ret += strPadRightChat((ret.length() > 0 ?
  327.             // unformattedStrRepeat(tabChar, tab) : "") + str.substring(0,
  328.             // lastPos));
  329.             ret += (ret.length() > 0 ? "\n"
  330.                     + unformattedStrRepeat(tabChar, tab) : "")
  331.                     + str.substring(0, lastPos);
  332.             str = str.substring(lastPos + 1);
  333.         }
  334.         return ret;
  335.     }
  336.  
  337.     public static String strChatWordWrapRight(final String str, final int tab) {
  338.         return strChatWordWrapRight(str, tab, ' ');
  339.     }
  340.  
  341.     /**
  342.      * right-aligns paragraphs
  343.      *
  344.      * @param str
  345.      * @param tab
  346.      * @param tabChar
  347.      * @return
  348.      */
  349.     public static String strChatWordWrapRight(String str, final int tab,
  350.             final char tabChar) {
  351.         String ret = "";
  352.         while (str.length() > 0) {
  353.             // find last char of first line
  354.             if (getStringWidth(str) <= chatwidth) {
  355.                 return (ret.length() > 0 ? ret + "\n" : "")
  356.                         .concat(strPadLeftChat(str, tabChar));
  357.             }
  358.             final String line1 = strChatTrim(str);
  359.             int lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  360.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  361.                 --lastPos;
  362.             }
  363.             if (lastPos == 0) {
  364.                 lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  365.             }
  366.             // ret += strPadLeftChat(str.substring(0, lastPos), tabChar);
  367.             ret += (ret.length() > 0 ? "\n" : "")
  368.                     + strPadLeftChat(str.substring(0, lastPos), tabChar);
  369.             str = str.substring(lastPos + 1);
  370.         }
  371.         return ret;
  372.     }
  373.  
  374.     /**
  375.      * will left-align the start of the string until sepChar, then right-align
  376.      * the remaining paragraph
  377.      *
  378.      * @param str
  379.      * @param tab
  380.      * @param tabChar
  381.      * @param sepChar
  382.      * @return
  383.      */
  384.     public static String strChatWordWrapRight(String str, final int tab,
  385.             final char tabChar, final char sepChar) {
  386.         String ret = "";
  387.         String line1 = strChatTrim(str);
  388.         // first run the first left & right align
  389.         if (line1.contains("" + sepChar)) {
  390.             int lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  391.             final int sepPos = line1.indexOf(sepChar) + 1;
  392.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  393.                 --lastPos;
  394.             }
  395.             if (lastPos == 0) {
  396.                 lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  397.             } else if (sepPos > lastPos) {
  398.                 lastPos = sepPos;
  399.             }
  400.             ret += str.substring(0, sepPos);
  401.             ret += strPadLeftChat(str.substring(sepPos, lastPos), chatwidth
  402.                     - getStringWidth(ret));
  403.             str = str.substring(lastPos + 1);
  404.         }
  405.         while (str.length() > 0) {
  406.             // find last char of first line
  407.             if (getStringWidth(str) <= chatwidth) {
  408.                 return (ret.length() > 0 ? ret + "\n" : "")
  409.                         .concat(strPadLeftChat(str, tabChar));
  410.             }
  411.             line1 = strChatTrim(str);
  412.             int lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  413.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  414.                 --lastPos;
  415.             }
  416.             if (lastPos == 0) {
  417.                 lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  418.             }
  419.             // ret += strPadLeftChat(str.substring(0, lastPos), tabChar);
  420.             ret += (ret.length() > 0 ? "\n" + lastStrColor(ret) : "")
  421.                     + strPadLeftChat(str.substring(0, lastPos), tabChar);
  422.             str = str.substring(lastPos + 1);
  423.         }
  424.         System.out.println(str + " changed to " + ret + "("
  425.                 + getStringWidth(ret) + " pixels long)");
  426.         return ret;
  427.     }
  428.  
  429.     public static String strWordWrap(final String str, final int width) {
  430.         return strWordWrap(str, width, 0, ' ');
  431.     }
  432.  
  433.     public static String strWordWrap(final String str, final int width,
  434.             final int tab) {
  435.         return strWordWrap(str, width, tab, ' ');
  436.     }
  437.  
  438.     public static String strWordWrap(String str, final int width,
  439.             final int tab, final char tabChar) {
  440.         String ret = "";
  441.         while (str.length() > 0) {
  442.             // find last char of first line
  443.             if (strLen(str) <= width) {
  444.                 return (ret.length() > 0 ? ret + "\n"
  445.                         + unformattedStrRepeat(tabChar, tab) : "").concat(str);
  446.             }
  447.             final String line1 = strTrim(str, width);
  448.             int lastPos = line1.length()
  449.                     - (ret.length() > 0 && line1.length() > tab + 1
  450.                             ? tab + 1
  451.                             : 1);
  452.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  453.                 --lastPos;
  454.             }
  455.             if (lastPos == 0) {
  456.                 lastPos = line1.length()
  457.                         - (ret.length() > 0 && line1.length() > tab + 1
  458.                                 ? tab + 1
  459.                                 : 1);
  460.             }
  461.             // ret += strPadRightChat((ret.length() > 0 ?
  462.             // unformattedStrRepeat(tabChar, tab) : "") + str.substring(0,
  463.             // lastPos));
  464.             ret += (ret.length() > 0 ? "\n"
  465.                     + unformattedStrRepeat(tabChar, tab) : "")
  466.                     + str.substring(0, lastPos);
  467.             str = str.substring(lastPos + 1);
  468.         }
  469.         return ret;
  470.     }
  471.  
  472.     public static String strWordWrapRight(final String str, final int width,
  473.             final int tab) {
  474.         return strWordWrapRight(str, width, tab, ' ');
  475.     }
  476.  
  477.     /**
  478.      * right-aligns paragraphs
  479.      *
  480.      * @param str
  481.      * @param width
  482.      * @param tab
  483.      * @param tabChar
  484.      * @return
  485.      */
  486.     public static String strWordWrapRight(String str, final int width,
  487.             final int tab, final char tabChar) {
  488.         String ret = "";
  489.         while (str.length() > 0) {
  490.             // find last char of first line
  491.             if (getStringWidth(str) <= width) {
  492.                 return (ret.length() > 0 ? ret + "\n" : "")
  493.                         .concat(unformattedPadLeft(str, width, tabChar));
  494.             }
  495.             final String line1 = strTrim(str, width);
  496.             int lastPos = line1.length()
  497.                     - (ret.length() > 0 && line1.length() > tab + 1
  498.                             ? tab + 1
  499.                             : 1);
  500.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  501.                 --lastPos;
  502.             }
  503.             if (lastPos <= 0) {
  504.                 lastPos = line1.length()
  505.                         - (ret.length() > 0 && line1.length() > tab + 1
  506.                                 ? tab + 1
  507.                                 : 1);
  508.             }
  509.             // ret += strPadLeftChat(str.substring(0, lastPos), tabChar);
  510.             ret += (ret.length() > 0 ? "\n" : "")
  511.                     + unformattedPadLeft(str.substring(0, lastPos), width,
  512.                             tabChar);
  513.             str = str.substring(lastPos + 1);
  514.         }
  515.         return ret;
  516.     }
  517.  
  518.     /**
  519.      * will left-align the start of the string until sepChar, then right-align
  520.      * the remaining paragraph
  521.      *
  522.      * @param str
  523.      * @param width
  524.      * @param tab
  525.      * @param tabChar
  526.      * @param sepChar
  527.      * @return
  528.      */
  529.     public static String strWordWrapRight(String str, final int width,
  530.             final int tab, final char tabChar, final char sepChar) {
  531.         String ret = "";
  532.         String line1 = strTrim(str, width);
  533.         // first run the first left & right align
  534.         if (line1.contains("" + sepChar)) {
  535.             int lastPos = line1.length() - (ret.length() > 0 ? tab + 1 : 1);
  536.             final int sepPos = line1.indexOf(sepChar) + 1;
  537.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  538.                 --lastPos;
  539.             }
  540.             if (lastPos == 0) {
  541.                 lastPos = line1.length()
  542.                         - (ret.length() > 0 && line1.length() > tab + 1
  543.                                 ? tab + 1
  544.                                 : 1);
  545.             } else if (sepPos > lastPos) {
  546.                 lastPos = sepPos;
  547.             }
  548.             ret += str.substring(0, sepPos);
  549.             ret += strPadLeftChat(str.substring(sepPos, lastPos), width
  550.                     - strLen(ret));
  551.             str = str.substring(lastPos + 1);
  552.         }
  553.         while (str.length() > 0) {
  554.             // find last char of first line
  555.             if (strLen(str) <= width) {
  556.                 return (ret.length() > 0 ? ret + "\n" : "")
  557.                         .concat(unformattedPadLeft(str, width, tabChar));
  558.             }
  559.             line1 = strChatTrim(str);
  560.             int lastPos = line1.length()
  561.                     - (ret.length() > 0 && line1.length() > tab + 1
  562.                             ? tab + 1
  563.                             : 1);
  564.             while (lastPos > 0 && line1.charAt(lastPos) != ' ') {
  565.                 --lastPos;
  566.             }
  567.             if (lastPos == 0) {
  568.                 lastPos = line1.length()
  569.                         - (ret.length() > 0 && line1.length() > tab + 1
  570.                                 ? tab + 1
  571.                                 : 1);
  572.             }
  573.             // ret += strPadLeftChat(str.substring(0, lastPos), tabChar);
  574.             ret += (ret.length() > 0 ? "\n" + lastStrColor(ret) : "")
  575.                     + unformattedPadLeft(str.substring(0, lastPos), width,
  576.                             tabChar);
  577.             str = str.substring(lastPos + 1);
  578.         }
  579.         return ret;
  580.     }
  581.  
  582.     public static String lastStrColor(final String str) {
  583.         final int i = str.lastIndexOf('\u00A7');
  584.         if (i >= 0 && i + 1 < str.length()) {
  585.             return str.substring(i, i + 2);
  586.         }
  587.         return "\u00A7F";// white
  588.     }
  589.  
  590.     private static boolean containsAlignTag(final String str, final String tag) {
  591.         final int pos = str.indexOf("<" + tag);
  592.         if (pos >= 0) {
  593.             return str.length() > pos + ("<" + tag).length()
  594.                     && (str.charAt(pos + ("<" + tag).length()) == '>' || str
  595.                             .charAt(pos + ("<" + tag).length() + 1) == '>');
  596.         }
  597.         return false;
  598.     }
  599.  
  600.     /**
  601.      * UNTESTED: DON'T USE YET
  602.      */
  603.     public static String alignTags(String input,
  604.             final boolean minecraftChatFormat) {
  605.         for (final String fm : new String[]{"l", "r", "c"}) {
  606.             while (containsAlignTag(input, fm)) {
  607.                 char repl = ' ';
  608.                 if (input.matches("^.*<" + fm + ".>.*$")) {
  609.                     repl = input.substring(input.indexOf("<" + fm) + 2).charAt(
  610.                             0);
  611.                     input = input.replaceFirst("<" + fm + ".>", "<" + fm + ">");
  612.                 }
  613.  
  614.                 if (fm.equals("l")) {
  615.                     if (minecraftChatFormat) {
  616.                         input = strPadRight(
  617.                                 input.substring(0,
  618.                                         input.indexOf("<" + fm + ">")),
  619.                                 input.indexOf("<" + fm + ">"), repl)
  620.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  621.                     } else {
  622.                         input = Str.padRight(
  623.                                 input.substring(0,
  624.                                         input.indexOf("<" + fm + ">")),
  625.                                 input.indexOf("<" + fm + ">"), repl)
  626.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  627.                     }
  628.                 } else if (fm.equals("c")) {
  629.                     if (minecraftChatFormat) {
  630.                         input = strPadCenter(
  631.                                 input.substring(0,
  632.                                         input.indexOf("<" + fm + ">")),
  633.                                 input.indexOf("<" + fm + ">"), repl)
  634.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  635.                     } else {
  636.                         input = Str.padCenter(
  637.                                 input.substring(0,
  638.                                         input.indexOf("<" + fm + ">")),
  639.                                 input.indexOf("<" + fm + ">"), repl)
  640.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  641.                     }
  642.                 } else {
  643.                     if (minecraftChatFormat) {
  644.                         input = strPadLeft(
  645.                                 input.substring(0,
  646.                                         input.indexOf("<" + fm + ">")),
  647.                                 input.indexOf("<" + fm + ">"), repl)
  648.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  649.                     } else {
  650.                         input = Str.padLeft(
  651.                                 input.substring(0,
  652.                                         input.indexOf("<" + fm + ">")),
  653.                                 input.indexOf("<" + fm + ">"), repl)
  654.                                 + input.substring(input.indexOf("<" + fm + ">") + 3);
  655.                     }
  656.                 }
  657.             }
  658.         }
  659.         return input;
  660.     }
  661.  
  662.     public static LinkedList<String> alignTags(LinkedList<String> input,
  663.             final boolean minecraftChatFormat) {
  664.         for (final String fm : new String[]{"l", "r", "c"}) {
  665.             while (containsAlignTag(input.get(1), fm)) {
  666.                 char repl = ' ';
  667.                 if (input.get(1).matches("^.*<" + fm + ".>.*$")) {// ||
  668.                                                                     // input.get(1).matches("^.*<r.>.*$"))
  669.                                                                     // {
  670.                     repl = input.get(1)
  671.                             .substring(input.get(1).indexOf("<" + fm) + 2)
  672.                             .charAt(0); // ,
  673.                                         // input.get(1).indexOf(">")
  674.                     for (int i = 0; i < input.size(); ++i) {
  675.                         input.set(
  676.                                 i,
  677.                                 input.get(i).replaceFirst("<" + fm + ".>",
  678.                                         "<" + fm + ">"));
  679.                     }
  680.                 }
  681.  
  682.                 int maxPos = 0;
  683.                 for (int i = 1; i < input.size(); ++i) {
  684.                     if (input.get(i).indexOf("<" + fm + ">") > maxPos) {
  685.                         maxPos = input.get(i).indexOf("<" + fm + ">");
  686.                     }
  687.                 }
  688.  
  689.                 final LinkedList<String> newinput = new LinkedList<String>();
  690.                 for (int i = 0; i < input.size(); ++i) {
  691.                     final String line = input.get(i);
  692.                     if (line.indexOf("<" + fm + ">") != -1) {
  693.                         if (fm.equals("l")) {
  694.                             if (minecraftChatFormat) {
  695.                                 newinput.add(strPadRight(
  696.                                         line.substring(0,
  697.                                                 line.indexOf("<" + fm + ">")),
  698.                                         maxPos, repl)
  699.                                         + line.substring(line.indexOf("<" + fm
  700.                                                 + ">") + 3));
  701.                             } else {
  702.                                 newinput.add(Str.padRight(
  703.                                         line.substring(0,
  704.                                                 line.indexOf("<" + fm + ">")),
  705.                                         maxPos, repl)
  706.                                         + line.substring(line.indexOf("<" + fm
  707.                                                 + ">") + 3));
  708.                             }
  709.                         } else if (fm.equals("c")) {
  710.                             if (minecraftChatFormat) {
  711.                                 newinput.add(strPadCenter(
  712.                                         line.substring(0,
  713.                                                 line.indexOf("<" + fm + ">")),
  714.                                         maxPos, repl)
  715.                                         + line.substring(line.indexOf("<" + fm
  716.                                                 + ">") + 3));
  717.                             } else {
  718.                                 newinput.add(Str.padCenter(
  719.                                         line.substring(0,
  720.                                                 line.indexOf("<" + fm + ">")),
  721.                                         maxPos, repl)
  722.                                         + line.substring(line.indexOf("<" + fm
  723.                                                 + ">") + 3));
  724.                             }
  725.                         } else {
  726.                             if (minecraftChatFormat) {
  727.                                 newinput.add(strPadLeft(
  728.                                         line.substring(0,
  729.                                                 line.indexOf("<" + fm + ">")),
  730.                                         maxPos, repl)
  731.                                         + line.substring(line.indexOf("<" + fm
  732.                                                 + ">") + 3));
  733.                             } else {
  734.                                 newinput.add(Str.padLeft(
  735.                                         line.substring(0,
  736.                                                 line.indexOf("<" + fm + ">")),
  737.                                         maxPos, repl)
  738.                                         + line.substring(line.indexOf("<" + fm
  739.                                                 + ">") + 3));
  740.                             }
  741.                         }
  742.                     } else {
  743.                         newinput.add(line);
  744.                     }
  745.                 }
  746.                 input = newinput;
  747.             }
  748.         }
  749.         return input;
  750.     }
  751. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement