From 636efc2e4880bce87098a23e869fb212b7c155f4 Mon Sep 17 00:00:00 2001 From: Mysti Date: Wed, 27 Mar 2024 13:24:32 -0700 Subject: [PATCH 1/3] Add tellraw custom color support --- tellraw.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tellraw.py b/tellraw.py index 04464ee..63cb755 100644 --- a/tellraw.py +++ b/tellraw.py @@ -1,9 +1,11 @@ import copy from scratch_tracker import scratch_tracker from CompileError import CompileError +import re ''' {C for color +{#FFFFFF for custom hex color {U for underline, {u to exit underline, same for {D bold, {S strikethrough and {I italic {- to clear formatting [text](command) for a command click event @@ -114,6 +116,7 @@ def parseTextFormatting(text): COMMAND = 4 SCOREBOARD = 5 PROPERTY = 6 + CUSTOM_COLOR = 7 escaped = False @@ -124,6 +127,9 @@ def parseTextFormatting(text): segments = [] properties = {"color": None, "bold": False, "underlined": False, "italic": False, "strikethrough": False} + hex_char_regex = re.compile("[A-Fa-f0-9]", re.IGNORECASE) + color_string = "" + for ch in text: if ch == "\\" and not escaped: escaped = True @@ -178,9 +184,23 @@ def parseTextFormatting(text): mode = NONE else: seg = seg + ch + elif mode == CUSTOM_COLOR: + if len(color_string) < 7: + if hex_char_regex.match(ch) is None: + raise CompileError(f'Unexpected hex color character {{{ch} in tell command') + + color_string += ch + else: + properties["color"] = color_string + color_string = "" + mode = NONE + seg += ch elif mode == PROPERTY: if ch in COLORS: properties["color"] = COLORS[ch] + elif ch == "#": + mode = CUSTOM_COLOR + color_string += "#" elif ch == "-": properties["color"] = None properties["bold"] = False @@ -208,7 +228,8 @@ def parseTextFormatting(text): else: raise CompileError(f'Unexpected formatting character {{{ch} in tell command') - mode = NONE + if mode != CUSTOM_COLOR: + mode = NONE if len(seg) > 0: segments.append((seg, copy.copy(properties))) From dde464530ee023ed368279857b2a18177925d9f0 Mon Sep 17 00:00:00 2001 From: Mysti Date: Wed, 27 Mar 2024 21:07:56 -0700 Subject: [PATCH 2/3] fix when formatting immediately after color --- tellraw.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tellraw.py b/tellraw.py index 63cb755..b547f1d 100644 --- a/tellraw.py +++ b/tellraw.py @@ -185,16 +185,16 @@ def parseTextFormatting(text): else: seg = seg + ch elif mode == CUSTOM_COLOR: - if len(color_string) < 7: + if len(color_string) < 6: if hex_char_regex.match(ch) is None: raise CompileError(f'Unexpected hex color character {{{ch} in tell command') color_string += ch else: + color_string += ch properties["color"] = color_string color_string = "" mode = NONE - seg += ch elif mode == PROPERTY: if ch in COLORS: properties["color"] = COLORS[ch] From 62a194dee191d710eb3e8f94ce0693f49bbc6361 Mon Sep 17 00:00:00 2001 From: Mysti Date: Thu, 6 Jun 2024 16:33:20 -0400 Subject: [PATCH 3/3] Assemble color using `seg` --- tellraw.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tellraw.py b/tellraw.py index b547f1d..a7fba7a 100644 --- a/tellraw.py +++ b/tellraw.py @@ -5,7 +5,7 @@ ''' {C for color -{#FFFFFF for custom hex color +{#FFFFFF for a custom hex color {U for underline, {u to exit underline, same for {D bold, {S strikethrough and {I italic {- to clear formatting [text](command) for a command click event @@ -128,7 +128,6 @@ def parseTextFormatting(text): properties = {"color": None, "bold": False, "underlined": False, "italic": False, "strikethrough": False} hex_char_regex = re.compile("[A-Fa-f0-9]", re.IGNORECASE) - color_string = "" for ch in text: if ch == "\\" and not escaped: @@ -185,22 +184,22 @@ def parseTextFormatting(text): else: seg = seg + ch elif mode == CUSTOM_COLOR: - if len(color_string) < 6: + if len(seg) < 6: if hex_char_regex.match(ch) is None: raise CompileError(f'Unexpected hex color character {{{ch} in tell command') - color_string += ch + seg += ch else: - color_string += ch - properties["color"] = color_string - color_string = "" + seg += ch + properties["color"] = seg + seg = "" mode = NONE elif mode == PROPERTY: if ch in COLORS: properties["color"] = COLORS[ch] elif ch == "#": mode = CUSTOM_COLOR - color_string += "#" + seg += "#" elif ch == "-": properties["color"] = None properties["bold"] = False