Skip to content

Commit

Permalink
cleaner/shorter JSON output result for complex colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 2, 2020
1 parent 8f6cc2d commit dc9a7b5
Showing 1 changed file with 32 additions and 25 deletions.
Expand Up @@ -8,7 +8,6 @@
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.*;

import java.util.ArrayList;
import java.util.List;

public class FormattedTextHelper {
Expand Down Expand Up @@ -172,8 +171,23 @@ public static TextComponent copyFormatToNewText(TextComponent last) {

public static BaseComponent[] parse(String str) {
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
TextComponent base = new TextComponent();
base.addExtra(new TextComponent(str)); // This is for compat with how Spigot does parsing of plaintext.
return new BaseComponent[] { base };
}
TextComponent root = new TextComponent(str.substring(0, firstChar));
TextComponent base = new TextComponent();
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(ChatColor.WHITE);
root.addExtra(base);
str = str.substring(firstChar);
char[] chars = str.toCharArray();
List<BaseComponent> outputList = new ArrayList<>(2);
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText = new TextComponent();
Expand All @@ -192,7 +206,7 @@ public static BaseComponent[] parse(String str) {
String innardType = CoreUtilities.toLowerCase(innardBase.get(0));
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
base.addExtra(nextText);
TextComponent doublelasttext = lastText;
lastText = nextText;
nextText = copyFormatToNewText(lastText);
Expand Down Expand Up @@ -342,18 +356,17 @@ else if (innardType.equals("font")) {
}
else if ((code >= '0' && code <= '9') || (code >= 'a' && code <= 'f') || (code >= 'A' && code <= 'F')) {
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setBold(false);
nextText.setItalic(false);
nextText.setStrikethrough(false);
nextText.setUnderlined(false);
nextText.setObfuscated(false);
nextText.setColor(ChatColor.getByChar(code));
}
else if ((code >= 'k' && code <= 'o') || (code >= 'K' && code <= 'O')) {
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
Expand All @@ -373,13 +386,10 @@ else if (code == 'o' || code == 'O') {
}
else if (code == 'r' || code == 'R') {
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setBold(false);
nextText.setItalic(false);
nextText.setStrikethrough(false);
nextText.setUnderlined(false);
nextText.setObfuscated(false);
nextText.setColor(ChatColor.WHITE);
}
else if (code == 'x') {
Expand All @@ -399,13 +409,10 @@ else if (code == 'x') {
continue;
}
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setBold(false);
nextText.setItalic(false);
nextText.setStrikethrough(false);
nextText.setUnderlined(false);
nextText.setObfuscated(false);
nextText.setColor(ChatColor.of(color.toString()));
i += 13;
started = i + 1;
Expand All @@ -426,7 +433,7 @@ else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i
}
String url = str.substring(i, nextSpace);
nextText.setText(nextText.getText() + str.substring(started, i));
outputList.add(nextText);
base.addExtra(nextText);
lastText = nextText;
nextText = new TextComponent(lastText);
nextText.setText("");
Expand All @@ -441,8 +448,8 @@ else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i
}
nextText.setText(nextText.getText() + str.substring(started));
if (!nextText.getText().isEmpty()) {
outputList.add(nextText);
base.addExtra(nextText);
}
return outputList.toArray(new BaseComponent[0]);
return new BaseComponent[] { root };
}
}

0 comments on commit dc9a7b5

Please sign in to comment.