Last active
August 29, 2015 14:05
-
-
Save mooman219/59e9a6a0ea2434f50cd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.mooman219.api.text; | |
import gnu.trove.map.hash.TCharIntHashMap; | |
import java.util.List; | |
import org.bukkit.ChatColor; | |
public class TextRenderHelper { | |
private static final TCharIntHashMap charWidths = new TCharIntHashMap(120, 0.5f, '0', -1); | |
public static void assureRenderedWidth(List<String> strings, char filler) { | |
assureRenderedWidth(strings, filler, ChatColor.RESET.toString()); | |
} | |
public static void assureRenderedWidth(List<String> strings, char filler, String append) { | |
int[] widths = new int[strings.size()]; | |
int max = 0; | |
for (int i = 0; i < strings.size(); i++) { | |
if (strings.get(i) != null) { | |
widths[i] = getRenderedWidth(strings.get(i)); | |
max = Math.max(max, widths[i]); | |
} | |
} | |
for (int i = 0; i < strings.size(); i++) { | |
if (strings.get(i) != null) { | |
strings.set(i, strings.get(i).concat(generateDesiredWidth(filler, max - widths[i])).concat(append)); | |
} | |
} | |
} | |
public static int getMaxRenderedWidth(List<String> strings) { | |
int max = 0; | |
for (String string : strings) { | |
if (string != null) { | |
max = Math.max(max, getRenderedWidth(string)); | |
} | |
} | |
return max; | |
} | |
public static int getRenderedWidth(String string) { | |
int length = 0; | |
int characterWidth = 0; | |
boolean bold = false; | |
for (int i = 0; i < string.length(); i++) { | |
if (string.charAt(i) == '§') { | |
if (++i < string.length()) { | |
bold = (Character.toLowerCase(string.charAt(i)) == 'l') ? true : !doesResetBold(string.charAt(i)); | |
} | |
} else { | |
characterWidth = charWidths.get(string.charAt(i)); | |
length += (characterWidth == -1 ? 5 : characterWidth); | |
length += (bold ? 1 : 0); | |
} | |
} | |
return length; | |
} | |
public static String generateDesiredWidth(char character, int desiredWidth) { | |
int characterWidth = charWidths.get(character); | |
StringBuilder builder = new StringBuilder((desiredWidth / characterWidth) + 2); | |
builder.append(ChatColor.RESET); | |
for (int i = 0; i < desiredWidth; i += characterWidth) { | |
builder.append(character); | |
} | |
return builder.toString(); | |
} | |
private static boolean doesResetBold(char character) { | |
switch (Character.toLowerCase(character)) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': | |
case 'a': | |
case 'b': | |
case 'c': | |
case 'd': | |
case 'e': | |
case 'f': | |
case 'r': | |
return true; | |
default: | |
return false; | |
} | |
} | |
static { | |
charWidths.put('A', 6); | |
charWidths.put('B', 6); | |
charWidths.put('C', 6); | |
charWidths.put('D', 6); | |
charWidths.put('E', 6); | |
charWidths.put('F', 6); | |
charWidths.put('G', 6); | |
charWidths.put('H', 6); | |
charWidths.put('I', 4); | |
charWidths.put('J', 6); | |
charWidths.put('K', 6); | |
charWidths.put('L', 6); | |
charWidths.put('M', 6); | |
charWidths.put('N', 6); | |
charWidths.put('O', 6); | |
charWidths.put('P', 6); | |
charWidths.put('Q', 6); | |
charWidths.put('R', 6); | |
charWidths.put('S', 6); | |
charWidths.put('T', 6); | |
charWidths.put('U', 6); | |
charWidths.put('V', 6); | |
charWidths.put('W', 6); | |
charWidths.put('X', 6); | |
charWidths.put('Y', 6); | |
charWidths.put('Z', 6); | |
charWidths.put('a', 6); | |
charWidths.put('b', 6); | |
charWidths.put('c', 6); | |
charWidths.put('d', 6); | |
charWidths.put('e', 6); | |
charWidths.put('f', 5); | |
charWidths.put('g', 6); | |
charWidths.put('h', 6); | |
charWidths.put('i', 2); | |
charWidths.put('j', 6); | |
charWidths.put('k', 5); | |
charWidths.put('l', 3); | |
charWidths.put('m', 6); | |
charWidths.put('n', 6); | |
charWidths.put('o', 6); | |
charWidths.put('p', 6); | |
charWidths.put('q', 6); | |
charWidths.put('r', 6); | |
charWidths.put('s', 6); | |
charWidths.put('t', 4); | |
charWidths.put('u', 6); | |
charWidths.put('v', 6); | |
charWidths.put('w', 6); | |
charWidths.put('x', 6); | |
charWidths.put('y', 6); | |
charWidths.put('z', 6); | |
charWidths.put('0', 6); | |
charWidths.put('1', 6); | |
charWidths.put('2', 6); | |
charWidths.put('3', 6); | |
charWidths.put('4', 6); | |
charWidths.put('5', 6); | |
charWidths.put('6', 6); | |
charWidths.put('7', 6); | |
charWidths.put('8', 6); | |
charWidths.put('9', 6); | |
charWidths.put(' ', 4); | |
charWidths.put('~', 7); | |
charWidths.put('!', 2); | |
charWidths.put('@', 7); | |
charWidths.put('#', 6); | |
charWidths.put('$', 6); | |
charWidths.put('%', 6); | |
charWidths.put('^', 6); | |
charWidths.put('&', 6); | |
charWidths.put('*', 5); | |
charWidths.put('(', 5); | |
charWidths.put(')', 5); | |
charWidths.put('_', 6); | |
charWidths.put('+', 6); | |
charWidths.put('`', 3); | |
charWidths.put('-', 6); | |
charWidths.put('=', 6); | |
charWidths.put('{', 5); | |
charWidths.put('}', 5); | |
charWidths.put('|', 2); | |
charWidths.put(':', 2); | |
charWidths.put('\"', 5); | |
charWidths.put('<', 5); | |
charWidths.put('>', 5); | |
charWidths.put('?', 6); | |
charWidths.put('[', 4); | |
charWidths.put(']', 4); | |
charWidths.put('\\', 6); | |
charWidths.put(';', 2); | |
charWidths.put('\'', 3); | |
charWidths.put(',', 2); | |
charWidths.put('.', 2); | |
charWidths.put('/', 6); | |
charWidths.put('»', 6); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> lines = new ArrayList<>(); | |
lines.add(ChatColor.GREEN + "" + ChatColor.BOLD + "Quest Complete!"); | |
lines.add(null); // Null will just skip the line. | |
lines.add("Gold: " + ChatColor.GOLD + "" + ChatColor.BOLD + "32"); | |
lines.add("Exp: " + ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "194"); | |
TextRenderHelper.assureRenderedWidth(lines, ' '); | |
HologramManager.get().spawnHologram(lines, player.getEyeLocation(), player, 200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment