Skip to content

Commit

Permalink
make basic text generation more spigot-compatibleish
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 31, 2021
1 parent 88153c9 commit fdd8f06
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 30 deletions.
Expand Up @@ -676,7 +676,8 @@ else if (attribute.startsWith("list", 2)) {
// @attribute <NPCTag.name_hologram_npc>
// @returns NPCTag
// @description
// Returns the NPCTag of a hologram attached to this NPC as its nameplate.
// Returns the NPCTag of a hologram attached to this NPC as its nameplate (if any).
// Note that this can regenerate at any time.
// -->
registerTag("name_hologram_npc", (attribute, object) -> {
if (!object.getCitizen().hasTrait(HologramTrait.class)) {
Expand All @@ -694,7 +695,8 @@ else if (attribute.startsWith("list", 2)) {
// @attribute <NPCTag.hologram_npcs>
// @returns ListTag(NPCTag)
// @description
// Returns the list of hologram NPCs attached to an NPC.
// Returns the list of hologram NPCs attached to an NPC (if any).
// Note that these can regenerate at any time.
// -->
registerTag("hologram_npcs", (attribute, object) -> {
if (!object.getCitizen().hasTrait(HologramTrait.class)) {
Expand Down
Expand Up @@ -192,6 +192,8 @@ else if (base.startsWith("[/" + type + "]", next + 1)){

public static AsciiMatcher allowedCharCodes = new AsciiMatcher("0123456789abcdefABCDEFklmnorxKLMNORX[");

public static AsciiMatcher hexMatcher = new AsciiMatcher("0123456789abcdefABCDEF");

public static AsciiMatcher colorCodesOrReset = new AsciiMatcher("0123456789abcdefABCDEFrR"); // Any color code that can be invalidated

public static AsciiMatcher colorCodeInvalidator = new AsciiMatcher("0123456789abcdefABCDEFrRxX"); // Any code that can invalidate the colors above
Expand Down Expand Up @@ -231,6 +233,104 @@ public static String cleanRedundantCodes(String str) {
return output.toString();
}

public static TextComponent getCleanRef() {
TextComponent reference = new TextComponent();
reference.setBold(false);
reference.setItalic(false);
reference.setStrikethrough(false);
reference.setUnderlined(false);
reference.setObfuscated(false);
return reference;
}

public static BaseComponent[] parseSimpleColorsOnly(String str) {
TextComponent root = new TextComponent();
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
int lastStart = 0;
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
lastStart = firstChar;
}
TextComponent nextText = new TextComponent();
while (firstChar != -1 && firstChar + 1 < str.length()) {
char c = str.charAt(firstChar + 1);
if (allowedCharCodes.isMatch(c)) {
if (c == 'r' || c == 'R') {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
lastStart = firstChar + 2;
}
else if (c == 'X' || c == 'x' && firstChar + 13 < str.length()) {
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int i = 1; i <= 6; i++) {
if (str.charAt(firstChar + i * 2) != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexChar = str.charAt(firstChar + 1 + i * 2);
if (!hexMatcher.isMatch(hexChar)) {
color = null;
break;
}
color.append(hexChar);
}
if (color != null) {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
nextText.setColor(ChatColor.of(color.toString()));
firstChar += 12;
lastStart = firstChar + 2;
}
}
else if (colorCodesOrReset.isMatch(c)) {
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = getCleanRef();
nextText.setColor(ChatColor.getByChar(c));
lastStart = firstChar + 2;
}
else { // format code
nextText.setText(str.substring(lastStart, firstChar));
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (c == 'k' || c == 'K') {
nextText.setObfuscated(true);
}
else if (c == 'l' || c == 'L') {
nextText.setBold(true);
}
else if (c == 'm' || c == 'M') {
nextText.setStrikethrough(true);
}
else if (c == 'n' || c == 'N') {
nextText.setUnderlined(true);
}
else if (c == 'o' || c == 'O') {
nextText.setItalic(true);
}
lastStart = firstChar + 2;
}
}
firstChar = str.indexOf(ChatColor.COLOR_CHAR, firstChar + 1);
}
if (lastStart < str.length()) {
nextText.setText(str.substring(lastStart));
root.addExtra(nextText);
}
return new BaseComponent[] { root };
}

public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
Expand All @@ -240,6 +340,9 @@ public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cle
return new BaseComponent[] { base };
}
str = cleanRedundantCodes(str);
if (cleanBase && str.length() < 256 && !str.contains(ChatColor.COLOR_CHAR + "[")) {
return parseSimpleColorsOnly(str);
}
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
Expand All @@ -265,6 +368,9 @@ public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cle
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) {
Expand Down Expand Up @@ -422,43 +528,21 @@ else if (innardType.equals("font")) {
started = endBracket + 1;
continue;
}
else if ((code >= '0' && code <= '9') || (code >= 'a' && code <= 'f') || (code >= 'A' && code <= 'F')) {
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(ChatColor.getByChar(code));
}
else if ((code >= 'k' && code <= 'o') || (code >= 'K' && code <= 'O')) {
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);
}
nextText.setColor(baseColor);
}
else if (code == 'r' || code == 'R') {
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(baseColor);
nextText.setColor(ChatColor.getByChar(code));
}
else if (code == 'x') {
if (i + 13 >= chars.length) {
Expand All @@ -471,7 +555,12 @@ else if (code == 'x') {
color = null;
break;
}
color.append(chars[i + 1 + c * 2]);
char hexPart = chars[i + 1 + c * 2];
if (!hexMatcher.isMatch(hexPart)) {
color = null;
break;
}
color.append(hexPart);
}
if (color == null) {
continue;
Expand All @@ -487,7 +576,26 @@ else if (code == 'x') {
continue;
}
else {
continue;
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;
Expand Down

0 comments on commit fdd8f06

Please sign in to comment.