Skip to content

Instantly share code, notes, and snippets.

@RingOfStorms
Last active November 3, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RingOfStorms/dc8a34adf3b03eebe95ed91720782909 to your computer and use it in GitHub Desktop.
Save RingOfStorms/dc8a34adf3b03eebe95ed91720782909 to your computer and use it in GitHub Desktop.
public static String strJoin (String delim, Collection list) {
return strJoin(delim, false, list);
}
public static String strJoin (String delim, boolean endSpace, Collection list) {
if(list == null)
return null;
else if(list.size() == 0)
return "";
else {
StringBuilder sb = new StringBuilder();
for(Object o : list) {
sb.append(o.toString());
sb.append(delim);
}
sb.delete(sb.length()-delim.length(), sb.length());
if(endSpace)
sb.append(' ');
return sb.toString();
}
}
public static void sendCenteredMessage (CommandSender sender, String... messages) {
for(String message : messages) {
if (message == null || message.trim().isEmpty()) {
sender.sendMessage("");
return;
}
sender.sendMessage(getCenteringCompensation(message, 154) + message);
}
}
public static String repeat (String str, int times) {
return new String(new char[times]).replace("\0", str);
}
public static UUID uuidFromString (String str) {
if(str.length() == 36)
try {
return UUID.fromString(str);
}catch (Exception badUuidProvided) {}
else
try {
StringBuffer buffer = new StringBuffer(str.trim());
char delim = '-';
for(int pos : new int[] {20, 16, 12, 8})
buffer.insert(pos, delim);
return UUID.fromString(buffer.toString());
}catch (Exception badUuidProvided) {}
return null;
}
public static int getPixelSize (char c) {
if(c == 'i' || c == 'l' || c == '!' || c == ':' || c == ';' || c == '\'' || c == '|' ||
c == '.' || c == ',')
return 1;
else if(c == 'I' || c == '[' || c == ']' || c == '"' || c == '`' || c == ' ')
return 3;
else if(c == 'f' || c == 'k' || c == 't' || c == '(' || c == ')' || c == '{' || c == '}' ||
c == '<' || c == '>')
return 4;
else if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'B') || (c >= '0' && c <= '9') ||
(c >= '#' && c <= '&') || c == '*' || c == '+' || c == '-' || c == '=' ||
c == '?' || c == '/' || c == '\\')
return 5;
else if(c == '@')
return 6;
else
return 4;
}
public static int getPixelSize (String str) {
if(str == null)
return 0;
int messagePxSize = 0;
boolean previousCode = false;
boolean isBold = false;
for(char c : str.toCharArray()) {
if(c == ChatColor.COLOR_CHAR)
previousCode = true;
else if(previousCode) {
previousCode = false;
char nc = Character.toLowerCase(c);
if(!isBold) {
if (nc == ChatColor.BOLD.getChar())
isBold = true;
}else if(nc == ChatColor.RESET.getChar())
isBold = false;
}else{
messagePxSize += getPixelSize(c);
messagePxSize += isBold ? 2 : 1;
}
}
return messagePxSize;
}
public static String getCenteringCompensation (String str, int width) {
return repeat(" ", (int) Math.ceil((width - (getPixelSize(str)/2))/4d));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment