KyoriPowered / adventure

A user-interface library, formerly known as text, for Minecraft: Java Edition
https://docs.advntr.dev/
MIT License
682 stars 106 forks source link

MiniMessage losing style when converting tree to component on input containing only style tags #849

Closed BlockyTheDev closed 1 year ago

BlockyTheDev commented 1 year ago

Adventure: 4.12.0

Creating a component with following code

Component.text("_").mergeStyle(MiniMessage.miniMessage().deserialize("<bold><red>"));

ends in (<bold>_)

TextComponentImpl{content="_", style=StyleImpl{obfuscated=not_set, bold=true, strikethrough=not_set, underlined=not_set, italic=not_set, color=null, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}

instead of a red colored and bold decorated underscore and a Component in a smiliar way to this (This is )

TextComponentImpl{content="_", style=StyleImpl{obfuscated=not_set, bold=true, strikethrough=not_set, underlined=not_set, italic=not_set, color=NamedTextColor{name="red", value="#ff5555"}, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}

like in

Component.text("_", NamedTextColor.RED, TextDecoration.BOLD);

but why I think that this is a bug is because when adding any non-space char to the String in the mergeStyle call it is working without problems but I think that should only be a workaround. I included the Parse-Debug-Output of the deserialisation in the mergeStyle part at the end of this issue.

What I wanted to do: I am writing on a plugin for a private server (PaperMC), where a player can input a MiniMessage-Formated String (Like <bold><red>) as a command argument and then the style should be applied to the part of his next messages after the prefix, name and suffix (PREFIX NAME SUFFIX >> FORMAT MESSAGE). The first part until the format (format excluded) is present as String and the message (in the code example represented as _) is present as Component (deseralized MiniMessage) because it is deseralized with an other TagResolver bound to a permission the player has. All things worked except for the thing with the MiniMessage-Color-Tag because it is not included in the Component inside of the mergeStyle call.

[02:23:20 INFO]: Beginning parsing message <bold><red>
Attempting to match node 'bold' at column 0
Successfully matched node 'bold' to tag StylingTagImpl
Attempting to match node 'red' at column 0
Successfully matched node 'red' to tag StylingTagImpl
Attempting to match node 'bold' at column 0
Successfully matched node 'bold' to tag StylingTagImpl
Attempting to match node 'red' at column 6
Successfully matched node 'red' to tag StylingTagImpl
Text parsed into element tree:
Node {
  TagNode('bold') {
    TagNode('red') {
    }
  }
}
==========
treeToComponent
TagNode('red') {
}
    [02:23:20 INFO]:
TextComponentImpl{
    "content" = "",
    "style" = StyleImpl{
        "obfuscated" = not_set,
        "bold" = not_set,
        "strikethrough" = not_set,
        "underlined" = not_set,
        "italic" = not_set,
        "color" = NamedTextColor{
            "name" = "red",
            "value" = "#ff5555"
        },
        "clickEvent" = null,
        "hoverEvent" = null,
        "insertion" = null,
        "font" = null
    },
    "children" = []
}
==========
==========
treeToComponent
TagNode('bold') {
  TagNode('red') {
  }
}
    [02:23:20 INFO]:
TextComponentImpl{
    "content" = "",
    "style" = StyleImpl{
        "obfuscated" = not_set,
        "bold" = true,
        "strikethrough" = not_set,
        "underlined" = not_set,
        "italic" = not_set,
        "color" = null,
        "clickEvent" = null,
        "hoverEvent" = null,
        "insertion" = null,
        "font" = null
    },
    "children" = [
        TextComponentImpl{
            "content" = "",
            "style" = StyleImpl{
                "obfuscated" = not_set,
                "bold" = not_set,
                "strikethrough" = not_set,
                "underlined" = not_set,
                "italic" = not_set,
                "color" = NamedTextColor{
                    "name" = "red",
                    "value" = "#ff5555"
                },
                "clickEvent" = null,
                "hoverEvent" = null,
                "insertion" = null,
                "font" = null
            },
            "children" = []
        }
    ]
}
==========
==========
treeToComponent
Node {
  TagNode('bold') {
    TagNode('red') {
    }
  }
}
    [02:23:20 INFO]:
TextComponentImpl{
    "content" = "",
    "style" = StyleImpl{
        "obfuscated" = not_set,
        "bold" = not_set,
        "strikethrough" = not_set,
        "underlined" = not_set,
        "italic" = not_set,
        "color" = null,
        "clickEvent" = null,
        "hoverEvent" = null,
        "insertion" = null,
        "font" = null
    },
    "children" = [
        TextComponentImpl{
            "content" = "",
            "style" = StyleImpl{
                "obfuscated" = not_set,
                "bold" = true,
                "strikethrough" = not_set,
                "underlined" = not_set,
                "italic" = not_set,
                "color" = null,
                "clickEvent" = null,
                "hoverEvent" = null,
                "insertion" = null,
                "font" = null
            },
            "children" = [
                TextComponentImpl{
                    "content" = "",
                    "style" = StyleImpl{
                        "obfuscated" = not_set,
                        "bold" = not_set,
                        "strikethrough" = not_set,
                        "underlined" = not_set,
                        "italic" = not_set,
                        "color" = NamedTextColor{
                            "name" = "red",
                            "value" = "#ff5555"
                        },
                        "clickEvent" = null,
                        "hoverEvent" = null,
                        "insertion" = null,
                        "font" = null
                    },
                    "children" = []
                }
            ]
        }
    ]
}
==========
kashike commented 1 year ago

The cause: https://github.com/KyoriPowered/adventure/blob/529d407d55ca50e6dee10f5696063f97e34ae9b5/api/src/main/java/net/kyori/adventure/text/ComponentCompaction.java#L176-L203