Skip to content

Commit

Permalink
json optimization parser and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 19, 2023
1 parent c0906b2 commit b6dec9d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
Expand Up @@ -445,6 +445,25 @@ public static void register() {
return new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(object.asString())));
});

// <--[tag]
// @attribute <ElementTag.optimize_json>
// @returns ElementTag
// @group conversion
// @description
// Tells the formatted text parser to try to produce mininalist JSON text.
// This is useful in particular for very long text or where text is being sent rapidly/repeatedly.
// It is not needed in most normal messages.
// It will produce incompatibility issues if used in items or other locations where raw JSON matching is required.
// Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>.
// -->
ElementTag.tagProcessor.registerStaticTag(ElementTag.class, "optimize_json", (attribute, object) -> {
String opti = ChatColor.COLOR_CHAR + "[optimize=true]";
if (object.asString().contains(opti)) {
return object;
}
return new ElementTag(opti + object.asString(), true);
});

// <--[tag]
// @attribute <ElementTag.hover_item[<item>]>
// @returns ElementTag
Expand Down
Expand Up @@ -324,6 +324,20 @@ else if (colorName.startsWith("co@") || colorName.lastIndexOf(',') > colorName.i
return new ElementTag(ChatColor.COLOR_CHAR + "[font=" + attribute.getParam() + "]");
});

// <--[tag]
// @attribute <&optimize>
// @returns ElementTag
// @description
// Returns a chat code that tells the formatted text parser to try to produce mininalist JSON text.
// This is useful in particular for very long text or where text is being sent rapidly/repeatedly.
// It is not needed in most normal messages.
// It will produce incompatibility issues if used in items or other locations where raw JSON matching is required.
// Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>.
// -->
TagManager.registerStaticTagBaseHandler(ElementTag.class, "&optimize", (attribute) -> {
return new ElementTag(ChatColor.COLOR_CHAR + "[optimize=true]", true);
});

// <--[tag]
// @attribute <&0>
// @returns ElementTag
Expand Down
Expand Up @@ -208,13 +208,23 @@ else if (component instanceof ScoreComponent) {

public static final String RESET = ChatColor.RESET.toString(), POSSIBLE_RESET_PREFIX = RESET + ChatColor.COLOR_CHAR;

public static TextComponent copyFormatToNewText(TextComponent last) {
private static Boolean procBool(Boolean input, boolean optimize) {
if (input == null) {
return null;
}
if (optimize) {
return input ? true : null;
}
return input;
}

public static TextComponent copyFormatToNewText(TextComponent last, boolean optimize) {
TextComponent toRet = new TextComponent();
toRet.setObfuscated(last.isObfuscatedRaw());
toRet.setBold(last.isBoldRaw());
toRet.setStrikethrough(last.isStrikethroughRaw());
toRet.setUnderlined(last.isUnderlinedRaw());
toRet.setItalic(last.isItalicRaw());
toRet.setObfuscated(procBool(last.isObfuscatedRaw(), optimize));
toRet.setBold(procBool(last.isBoldRaw(), optimize));
toRet.setStrikethrough(procBool(last.isStrikethroughRaw(), optimize));
toRet.setUnderlined(procBool(last.isUnderlinedRaw(), optimize));
toRet.setItalic(procBool(last.isItalicRaw(), optimize));
toRet.setColor(last.getColorRaw());
return toRet;
}
Expand Down Expand Up @@ -382,7 +392,7 @@ else if (colorCodesOrReset.isMatch(c)) {
if (!nextText.getText().isEmpty()) {
root.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
nextText = copyFormatToNewText(nextText, false);
if (c == 'k' || c == 'K') {
nextText.setObfuscated(true);
}
Expand Down Expand Up @@ -455,9 +465,10 @@ public static BaseComponent[] parseInternal(String str, ChatColor baseColor, boo
return new BaseComponent[] { component };
}
}
boolean optimize = str.contains(ChatColor.COLOR_CHAR + "[optimize=true]");
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
if (cleanBase && !optimize) {
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
Expand Down Expand Up @@ -497,7 +508,7 @@ public static BaseComponent[] parseInternal(String str, ChatColor baseColor, boo
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText = copyFormatToNewText(lastText, optimize);
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)));
Expand Down Expand Up @@ -660,6 +671,9 @@ else if (innardType.equals("font") && Utilities.matchesNamespacedKey(innardBase.
endBracket = endIndex + "&[reset=font".length();
}
}
else if (innardType.equals("optimize")) {
// Ignore
}
else {
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Text parse issue: cannot interpret type '" + innardType + "' with " + innardParts.size() + " parts.");
Expand Down Expand Up @@ -722,7 +736,7 @@ else if (code == 'x') {
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
nextText = copyFormatToNewText(nextText, optimize);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
}
Expand Down Expand Up @@ -768,7 +782,7 @@ else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
return new BaseComponent[] { cleanBase ? root : base };
return new BaseComponent[] { cleanBase && !optimize ? root : base };
}

public static int indexOfLastColorBlockStart(String text) {
Expand Down

0 comments on commit b6dec9d

Please sign in to comment.