diff --git a/tellraw.py b/tellraw.py index 04464ee..a7fba7a 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 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 @@ -114,6 +116,7 @@ def parseTextFormatting(text): COMMAND = 4 SCOREBOARD = 5 PROPERTY = 6 + CUSTOM_COLOR = 7 escaped = False @@ -124,6 +127,8 @@ 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) + for ch in text: if ch == "\\" and not escaped: escaped = True @@ -178,9 +183,23 @@ def parseTextFormatting(text): mode = NONE else: seg = seg + ch + elif mode == CUSTOM_COLOR: + if len(seg) < 6: + if hex_char_regex.match(ch) is None: + raise CompileError(f'Unexpected hex color character {{{ch} in tell command') + + seg += ch + else: + seg += ch + properties["color"] = seg + seg = "" + mode = NONE elif mode == PROPERTY: if ch in COLORS: properties["color"] = COLORS[ch] + elif ch == "#": + mode = CUSTOM_COLOR + seg += "#" elif ch == "-": properties["color"] = None properties["bold"] = False @@ -208,7 +227,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)))