From b6dec9d5ee59575143f4578b44a65b4ea75d021a Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Wed, 18 Jan 2023 18:28:37 -0800 Subject: [PATCH] json optimization parser and tags --- .../bukkit/BukkitElementExtensions.java | 19 ++++++++++ .../denizen/tags/core/TextTagBase.java | 14 ++++++++ .../utilities/FormattedTextHelper.java | 36 +++++++++++++------ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementExtensions.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementExtensions.java index 3db20ebeee..f2defd2c1a 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementExtensions.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementExtensions.java @@ -445,6 +445,25 @@ public static void register() { return new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(object.asString()))); }); + // <--[tag] + // @attribute + // @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 ]> // @returns ElementTag diff --git a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java index 95137e7e0c..98c102f4fe 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java +++ b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java @@ -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 diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java index 1b550e7de7..962297cd34 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java @@ -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; } @@ -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); } @@ -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); @@ -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))); @@ -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."); @@ -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); } @@ -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) {