Added line stuff

This commit is contained in:
libraryaddict 2015-12-11 15:21:05 +13:00
parent 1ee1e7a04b
commit 1661c056c1

View File

@ -1,52 +1,469 @@
package mineplex.core.common.util; package mineplex.core.common.util;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import javax.imageio.ImageIO;
import mineplex.core.common.CurrencyType;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.bukkit.Material; import org.bukkit.ChatColor;
public class UtilText { public class UtilText
public static <T> String listToString(Collection<T> inputList, boolean comma) { {
String out = ""; private static HashMap<Character, Integer> _characters = new HashMap<Character, Integer>();
for (T cur : inputList) { public static enum LineFormat
out += cur.toString() + (comma ? ", " : " "); {
LORE(200), CHAT(319);
private int _length;
private LineFormat(int length)
{
_length = length;
} }
if (out.length() > 0) { public int getLength()
out = out.substring(0, out.length() - (comma ? 2 : 1)); {
return _length;
}
} }
return out; static
{
try
{
InputStream inputStream = CurrencyType.class.getResourceAsStream("ascii.png");
BufferedImage image = ImageIO.read(inputStream);
char[] text = new char[]
{
' ',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
':',
';',
'<',
'=',
'>',
'?',
'@',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'[',
'\\',
']',
'^',
'_',
'`',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'{',
'|',
'}',
'~'
};
int x = 0;
int y = 16;
for (char c : text)
{
grab(c, image, x, y);
if (x < 15 * 8)
{
x += 8;
}
else
{
x = 0;
y += 8;
}
} }
public static int upperCaseCount(String input) { inputStream.close();
int count = 0; }
catch (IOException e)
for (int k = 0; k < input.length(); k++) { {
e.printStackTrace();
}
char ch = input.charAt(k);
if (Character.isUpperCase(ch))
count++;
} }
return count; public static String center(String string, LineFormat lineFormat)
} {
public static int lowerCaseCount(String input) { int length = getLength(string);
int count = 0;
for (int k = 0; k < input.length(); k++) {
char ch = input.charAt(k);
if (Character.isLowerCase(ch))
count++;
if (length > lineFormat.getLength())
{
return string;
} }
return count; // Get the number of empty pixels on both sides of the string
int div = (int) Math.floor((lineFormat.getLength() - length) / 2D);
div -= 2; // For the gap between the strings
return fillLine(" ", div) + string + fillLine(" ", div);
}
public static String alignRight(String string, LineFormat lineFormat)
{
int length = getLength(string);
if (length > lineFormat.getLength())
{
return string;
}
// Get the number of empty pixels on both sides of the string
int div = lineFormat.getLength() - length;
div -= 1; // For the gap between the strings
return fillLine(" ", div) + string;
}
public static String centerChat(String string, LineFormat lineFormat)
{
int length = getLength(string);
if (length > lineFormat.getLength())
{
return string;
}
// Get the number of empty pixels on both sides of the string
int div = (int) Math.floor(((lineFormat.getLength() + 10) - length) / 2D);
div -= 2; // For the gap between the strings
return fillLine(" ", div) + string;
}
public static String substringPixels(String string, int cutoff)
{
int len = 0;
char[] array = string.toCharArray();
boolean bold = false;
for (int i = 0; i < array.length; i++)
{
char c = array[i];
if (c == '<27>')
{
if (++i < array.length)
{
ChatColor color = ChatColor.getByChar(array[i]);
if (color != null)
{
if (color.equals(ChatColor.BOLD))
{
bold = true;
}
else if (color.equals(ChatColor.RESET) || color.isColor())
{
bold = false;
}
}
}
continue;
}
if (!_characters.containsKey(c))
{
continue;
}
int toAdd = _characters.get(c);
if (bold)
{
toAdd++;
}
if (len + toAdd > cutoff)
{
return string.substring(0, Math.max(0, i - 1));
}
if (i + 1 < array.length)
{
len++;
}
}
return string;
}
public static ArrayList<String> splitLines(String[] strings, LineFormat lineFormat)
{
ArrayList<String> lines = new ArrayList<String>();
for (String s : strings)
{
lines.addAll(splitLine(s, lineFormat));
}
return lines;
}
public static ArrayList<String> splitLine(String string, LineFormat lineFormat)
{
ArrayList<String> strings = new ArrayList<String>();
String current = "";
int currentLength = 0;
String[] split = string.split(" ");
String colors = "";
for (int i = 0; i < split.length; i++)
{
String word = split[i];
int wordLength = getLength(colors + word);
if (currentLength + wordLength + 4 > lineFormat.getLength() && !current.isEmpty())
{
strings.add(current);
current = colors + word;
currentLength = wordLength + 1;
continue;
}
if (i != 0)
{
current += " ";
currentLength += 4;
}
current += word;
currentLength += wordLength;
colors = ChatColor.getLastColors(current);
}
if (!current.isEmpty())
{
strings.add(current);
}
return strings;
}
public static String fillLine(String filler, int maxPixels)
{
int pixels = getLength(filler);
if (pixels <= 0)
{
return "";
}
String toReturn = "";
int currentLen = 0;
int offset = maxPixels % 4;
boolean isOffset = false;
if (offset > 0)
{
toReturn += C.Bold;
}
while (currentLen + pixels <= maxPixels)
{
currentLen += pixels + 1;
toReturn += filler;
if (offset-- > 0)
{
currentLen++;
if (offset == 0)
{
isOffset = false;
toReturn += ChatColor.RESET;
}
}
}
if (isOffset)
{
toReturn += ChatColor.RESET;
}
return toReturn;
}
public static boolean fitsOneLine(String string, LineFormat lineFormat)
{
return getLength(string) <= lineFormat.getLength();
}
public static int getLength(String string)
{
int len = 0;
char[] array = string.toCharArray();
boolean bold = false;
for (int i = 0; i < array.length; i++)
{
char c = array[i];
if (c == '<27>')
{
if (++i < array.length)
{
ChatColor color = ChatColor.getByChar(array[i]);
if (color != null)
{
if (color.equals(ChatColor.BOLD))
{
bold = true;
}
else if (color.equals(ChatColor.RESET) || color.isColor())
{
bold = false;
}
}
}
continue;
}
if (!_characters.containsKey(c))
{
continue;
}
len += _characters.get(c);
if (bold)
{
len++;
}
if (i + 1 < array.length)
{
len++;
}
}
return len;
}
private static void grab(Character character, BufferedImage image, int imageX, int imageY)
{
if (character == ' ')
{
_characters.put(character, 3);
return;
}
for (int x = 0; x < 8; x++)
{
boolean isTransparentLine = true;
for (int y = 0; y < 8; y++)
{
int pixel = image.getRGB(imageX + x, imageY + y);
if ((pixel >> 24) != 0x00)
{
isTransparentLine = false;
break;
}
}
if (isTransparentLine)
{
_characters.put(character, x);
return;
}
}
_characters.put(character, 8);
} }
public static boolean isStringSimilar(String newString, String oldString, float matchRequirement) public static boolean isStringSimilar(String newString, String oldString, float matchRequirement)
@ -56,28 +473,28 @@ public class UtilText {
return newString.toLowerCase().equals(oldString.toLowerCase()); return newString.toLowerCase().equals(oldString.toLowerCase());
} }
for (int i=0 ; i < newString.length() * matchRequirement ; i++) for (int i = 0; i < newString.length() * matchRequirement; i++)
{ {
int matchFromIndex = 0; int matchFromIndex = 0;
//Look for substrings starting at i // Look for substrings starting at i
for (int j=0 ; j < oldString.length() ; j++) for (int j = 0; j < oldString.length(); j++)
{ {
//End of newString // End of newString
if (i+j >= newString.length()) if (i + j >= newString.length())
{ {
break; break;
} }
//Matched // Matched
if (newString.charAt(i+j) == oldString.charAt(j)) if (newString.charAt(i + j) == oldString.charAt(j))
{ {
matchFromIndex++; matchFromIndex++;
if (matchFromIndex >= newString.length() * matchRequirement) if (matchFromIndex >= newString.length() * matchRequirement)
return true; return true;
} }
//No Match > Reset // No Match > Reset
else else
{ {
break; break;
@ -88,12 +505,62 @@ public class UtilText {
return false; return false;
} }
public static <T> String listToString(Collection<T> inputList, boolean comma)
{
String out = "";
for (T cur : inputList)
{
out += cur.toString() + (comma ? ", " : " ");
}
if (out.length() > 0)
{
out = out.substring(0, out.length() - (comma ? 2 : 1));
}
return out;
}
public static int lowerCaseCount(String input)
{
int count = 0;
for (int k = 0; k < input.length(); k++)
{
char ch = input.charAt(k);
if (Character.isLowerCase(ch))
count++;
}
return count;
}
public static int upperCaseCount(String input)
{
int count = 0;
for (int k = 0; k < input.length(); k++)
{
char ch = input.charAt(k);
if (Character.isUpperCase(ch))
count++;
}
return count;
}
public static String[] wrap(String text, int lineLength) public static String[] wrap(String text, int lineLength)
{ {
return wrap(text, lineLength, true); return wrap(text, lineLength, true);
} }
public static String[] wrap(String text, int lineLength, boolean wrapLongerWords) { public static String[] wrap(String text, int lineLength, boolean wrapLongerWords)
{
return WordUtils.wrap(text, lineLength, "\00D0", wrapLongerWords).split("\00D0"); return WordUtils.wrap(text, lineLength, "\00D0", wrapLongerWords).split("\00D0");
} }
@ -102,11 +569,13 @@ public class UtilText {
return new String(new byte[times]).replace("\0", txt); return new String(new byte[times]).replace("\0", txt);
} }
public static boolean plural(int x){ public static boolean plural(int x)
{
return x <= 0 ? true : x > 1; return x <= 0 ? true : x > 1;
} }
public static String trim(int maxLength, String s) { public static String trim(int maxLength, String s)
{
return s.length() <= maxLength ? s : s.substring(0, maxLength); return s.length() <= maxLength ? s : s.substring(0, maxLength);
} }