Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion tellraw.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -114,6 +116,7 @@ def parseTextFormatting(text):
COMMAND = 4
SCOREBOARD = 5
PROPERTY = 6
CUSTOM_COLOR = 7

escaped = False

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down