Skip to content

Commit

Permalink
Fix new line MOTD parsing, fix edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
PassTheMayo committed Dec 23, 2023
1 parent 0193459 commit 6747a95
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions formatting/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Parse(input interface{}) (*Result, error) {
}
case map[string]interface{}:
{
tree, err = parseString(parseChatObject(v, nil))
tree, err = parseString(convertChatToString(v, nil))

break
}
Expand All @@ -52,7 +52,7 @@ func Parse(input interface{}) (*Result, error) {
}, nil
}

func parseChatObject(current map[string]interface{}, parent map[string]interface{}) (result string) {
func convertChatToString(current map[string]interface{}, parent map[string]interface{}) (result string) {
if v, ok := current["color"].(string); ok {
result += colors.Parse(v).ToRaw()
} else if v, ok := parent["color"].(string); ok {
Expand Down Expand Up @@ -107,23 +107,34 @@ func parseChatObject(current map[string]interface{}, parent map[string]interface

if extra, ok := current["extra"].([]interface{}); ok {
for _, v := range extra {
if v2, ok := v.(map[string]interface{}); ok {
result += parseChatObject(v2, current)
switch val := v.(type) {
case map[string]interface{}:
{
result += convertChatToString(val, current)

break
}
case string:
{
result += val

break
}
}
}
}

return
}

func parseString(s string) ([]Item, error) {
func parseString(input string) ([]Item, error) {
var (
tree []Item = make([]Item, 0)
item Item = Item{
Text: "",
Decorators: make([]decorators.Decorator, 0),
}
r *strings.Reader = strings.NewReader(s)
r *strings.Reader = strings.NewReader(input)
)

for r.Len() > 0 {
Expand All @@ -141,7 +152,7 @@ func parseString(s string) ([]Item, error) {
tree = append(tree, item)

item = Item{
Text: "",
Text: "\n",
Decorators: make([]decorators.Decorator, 0),
}

Expand Down

0 comments on commit 6747a95

Please sign in to comment.