use of net.md_5.bungee.api.ChatColor in project Denizen-For-Bukkit by DenizenScript.
the class FormattedTextHelper method parse.
public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
if (str == null) {
return null;
}
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
if (str.contains("://")) {
firstChar = 0;
} else {
TextComponent base = new TextComponent();
// This is for compat with how Spigot does parsing of plaintext.
base.addExtra(new TextComponent(str));
return new BaseComponent[] { base };
}
}
str = cleanRedundantCodes(str);
if (cleanBase && str.length() < 512 && !str.contains(ChatColor.COLOR_CHAR + "[") && !str.contains("://")) {
return parseSimpleColorsOnly(str);
}
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(baseColor);
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
}
} else {
base.setText(str.substring(0, firstChar));
}
root.addExtra(base);
str = str.substring(firstChar);
char[] chars = str.toCharArray();
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ChatColor.COLOR_CHAR && i + 1 < chars.length) {
char code = chars[i + 1];
if (!allowedCharCodes.isMatch(code)) {
continue;
}
if (code == '[') {
int endBracket = str.indexOf(']', i + 2);
if (endBracket == -1) {
continue;
}
String innards = str.substring(i + 2, endBracket);
List<String> innardParts = CoreUtilities.split(innards, ';');
List<String> innardBase = CoreUtilities.split(innardParts.get(0), '=', 2);
innardParts.remove(0);
String innardType = CoreUtilities.toLowerCase(innardBase.get(0));
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText.setText("");
if (innardType.equals("score") && innardParts.size() == 2) {
ScoreComponent component = new ScoreComponent(unescape(innardBase.get(1)), unescape(innardParts.get(0)), unescape(innardParts.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("keybind")) {
KeybindComponent component = new KeybindComponent();
component.setKeybind(unescape(innardBase.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("selector")) {
SelectorComponent component = new SelectorComponent(unescape(innardBase.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("translate")) {
TranslatableComponent component = new TranslatableComponent();
component.setTranslate(unescape(innardBase.get(1)));
for (String extra : innardParts) {
for (BaseComponent subComponent : parse(unescape(extra), baseColor, false)) {
component.addWith(subComponent);
}
}
lastText.addExtra(component);
} else if (innardType.equals("click") && innardParts.size() == 1) {
int endIndex = findEndIndexFor(str, "click", i + 5);
if (endIndex == -1) {
continue;
}
TextComponent clickableText = new TextComponent();
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(innardBase.get(1).toUpperCase()), unescape(innardParts.get(0))));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
clickableText.addExtra(subComponent);
}
lastText.addExtra(clickableText);
endBracket = endIndex + "&[/click".length();
} else if (innardType.equals("hover")) {
int endIndex = findEndIndexFor(str, "hover", i + 5);
if (endIndex == -1) {
continue;
}
TextComponent hoverableText = new TextComponent();
HoverEvent.Action action = HoverEvent.Action.valueOf(innardBase.get(1).toUpperCase());
if (HoverFormatHelper.processHoverInput(action, hoverableText, innardParts.get(0))) {
continue;
}
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
hoverableText.addExtra(subComponent);
}
lastText.addExtra(hoverableText);
endBracket = endIndex + "&[/hover".length();
} else if (innardType.equals("insertion")) {
int endIndex = str.indexOf(ChatColor.COLOR_CHAR + "[/insertion]", i);
int backupEndIndex = str.indexOf(ChatColor.COLOR_CHAR + "[insertion=", i + 5);
if (backupEndIndex > 0 && backupEndIndex < endIndex) {
endIndex = backupEndIndex;
}
if (endIndex == -1) {
continue;
}
TextComponent insertableText = new TextComponent();
insertableText.setInsertion(unescape(innardBase.get(1)));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
insertableText.addExtra(subComponent);
}
lastText.addExtra(insertableText);
endBracket = endIndex + "&[/insertion".length();
} else if (innardType.equals("reset")) {
if (innardBase.get(1).length() == 1) {
char subCode = innardBase.get(1).charAt(0);
if (subCode == 'k' || subCode == 'K') {
nextText.setObfuscated(false);
} else if (subCode == 'l' || subCode == 'L') {
nextText.setBold(false);
} else if (subCode == 'm' || subCode == 'M') {
nextText.setStrikethrough(false);
} else if (subCode == 'n' || subCode == 'N') {
nextText.setUnderlined(false);
} else if (subCode == 'o' || subCode == 'O') {
nextText.setItalic(false);
}
} else if (innardBase.get(1).equals("font")) {
nextText.setFont(base.getFont());
} else {
nextText.setColor(base.getColor());
}
} else if (innardType.equals("color")) {
String colorChar = innardBase.get(1);
ChatColor color = null;
if (colorChar.length() == 1) {
color = ChatColor.getByChar(colorChar.charAt(0));
} else if (colorChar.length() == 7) {
color = ChatColor.of(colorChar);
} else if (Debug.verbose) {
Debug.echoError("Text parse issue: cannot interpret color '" + innardBase.get(1) + "'.");
}
if (color != null) {
int endIndex = findEndIndexFor(str, "[color=", "[reset=color]", i + 1);
if (endIndex == -1) {
nextText.setColor(color);
} else {
TextComponent colorText = new TextComponent();
colorText.setColor(color);
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), color, false)) {
colorText.addExtra(subComponent);
}
lastText.addExtra(colorText);
endBracket = endIndex + "&[reset=color".length();
}
}
} else if (innardType.equals("font")) {
int endIndex = findEndIndexFor(str, "[font=", "[reset=font]", i + 1);
if (endIndex == -1) {
nextText.setFont(innardBase.get(1));
} else {
TextComponent fontText = new TextComponent();
fontText.setFont(innardBase.get(1));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
fontText.addExtra(subComponent);
}
lastText.addExtra(fontText);
endBracket = endIndex + "&[reset=font".length();
}
} else {
if (Debug.verbose) {
Debug.echoError("Text parse issue: cannot interpret type '" + innardType + "' with " + innardParts.size() + " parts.");
}
}
}
i = endBracket;
started = endBracket + 1;
continue;
} else if (code == 'r' || code == 'R') {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(baseColor);
} else if (colorCodesOrReset.isMatch(code)) {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.getByChar(code));
} else if (code == 'x') {
if (i + 13 >= chars.length) {
continue;
}
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int c = 1; c <= 6; c++) {
if (chars[i + c * 2] != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexPart = chars[i + 1 + c * 2];
if (!hexMatcher.isMatch(hexPart)) {
color = null;
break;
}
color.append(hexPart);
}
if (color == null) {
continue;
}
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.of(color.toString()));
i += 13;
started = i + 1;
continue;
} else {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
} else if (code == 'l' || code == 'L') {
nextText.setBold(true);
} else if (code == 'm' || code == 'M') {
nextText.setStrikethrough(true);
} else if (code == 'n' || code == 'N') {
nextText.setUnderlined(true);
} else if (code == 'o' || code == 'O') {
nextText.setItalic(true);
}
}
i++;
started = i + 1;
} else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i + 1] == 't' && chars[i + 2] == 't' && chars[i + 3] == 'p') {
String subStr = str.substring(i, i + "https://a.".length());
if (subStr.startsWith("https://") || subStr.startsWith("http://")) {
int nextSpace = CoreUtilities.indexOfAny(str, i, ' ', '\t', '\n');
if (nextSpace == -1) {
nextSpace = str.length();
}
String url = str.substring(i, nextSpace);
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = new TextComponent(lastText);
nextText.setText("");
TextComponent clickableText = new TextComponent(url);
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
lastText.addExtra(clickableText);
i = nextSpace;
started = nextSpace;
continue;
}
}
}
nextText.setText(nextText.getText() + str.substring(started));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
return new BaseComponent[] { cleanBase ? root : base };
}
use of net.md_5.bungee.api.ChatColor in project Denizen-For-Bukkit by DenizenScript.
the class FormattedTextHelper method stringify.
public static String stringify(BaseComponent component) {
if (component == null) {
return null;
}
StringBuilder builder = new StringBuilder(128);
ChatColor color = component.getColorRaw();
if (color != null) {
builder.append(color);
}
if (boolNotNull(component.isBoldRaw())) {
builder.append(ChatColor.BOLD);
}
if (boolNotNull(component.isItalicRaw())) {
builder.append(ChatColor.ITALIC);
}
if (boolNotNull(component.isStrikethroughRaw())) {
builder.append(ChatColor.STRIKETHROUGH);
}
if (boolNotNull(component.isUnderlinedRaw())) {
builder.append(ChatColor.UNDERLINE);
}
if (boolNotNull(component.isObfuscatedRaw())) {
builder.append(ChatColor.MAGIC);
}
if (component.getFontRaw() != null) {
builder.append(ChatColor.COLOR_CHAR).append("[font=").append(component.getFontRaw()).append("]");
}
boolean hasInsertion = component.getInsertion() != null;
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR).append("[insertion=").append(escape(component.getInsertion())).append("]");
}
boolean hasHover = component.getHoverEvent() != null;
if (hasHover) {
HoverEvent hover = component.getHoverEvent();
builder.append(ChatColor.COLOR_CHAR).append("[hover=").append(hover.getAction().name()).append(";").append(escape(NMSHandler.getInstance().stringForHover(hover))).append("]");
}
boolean hasClick = component.getClickEvent() != null;
if (hasClick) {
ClickEvent click = component.getClickEvent();
builder.append(ChatColor.COLOR_CHAR).append("[click=").append(click.getAction().name()).append(";").append(escape(click.getValue())).append("]");
}
if (component instanceof TextComponent) {
builder.append(((TextComponent) component).getText());
} else if (component instanceof TranslatableComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[translate=").append(escape(((TranslatableComponent) component).getTranslate()));
List<BaseComponent> with = ((TranslatableComponent) component).getWith();
if (with != null) {
for (BaseComponent withComponent : with) {
builder.append(";").append(escape(stringify(withComponent)));
}
}
builder.append("]");
} else if (component instanceof SelectorComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[selector=").append(escape(((SelectorComponent) component).getSelector())).append("]");
} else if (component instanceof KeybindComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[keybind=").append(escape(((KeybindComponent) component).getKeybind())).append("]");
} else if (component instanceof ScoreComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[score=").append(escape(((ScoreComponent) component).getName())).append(";").append(escape(((ScoreComponent) component).getObjective())).append(";").append(escape(((ScoreComponent) component).getValue())).append("]");
}
List<BaseComponent> after = component.getExtra();
if (after != null) {
for (BaseComponent afterComponent : after) {
builder.append(stringify(afterComponent));
}
}
if (hasClick) {
builder.append(ChatColor.COLOR_CHAR + "[/click]");
}
if (hasHover) {
builder.append(ChatColor.COLOR_CHAR + "[/hover]");
}
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR + "[/insertion]");
}
builder.append(RESET);
String output = builder.toString();
return cleanRedundantCodes(output);
}
Aggregations