Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BlockyTheDev opened this issue Dec 27, 2022 · 1 comment

Comments

@BlockyTheDev
Copy link

BlockyTheDev commented Dec 27, 2022

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" = []
                }
            ]
        }
    ]
}
==========
@kezz kezz changed the title MergeStyle only partially applied MiniMessage losing style when converting tree to component on input containing only style tags Jan 5, 2023
@kashike
Copy link
Member

kashike commented Jan 11, 2023

The cause:

/**
* Simplify the provided style to remove any information that is redundant,
* given that the content is blank.
*
* @param style style to simplify
* @param parentStyle style from component's parents, for context
* @return a new, simplified style
*/
private static @NotNull Style simplifyStyleForBlank(final @NotNull Style style, final @Nullable Style parentStyle) {
final Style.Builder builder = style.toBuilder();
// TextColor doesn't affect spaces, unless there is other decoration present
if (!(style.hasDecoration(TextDecoration.UNDERLINED) || style.hasDecoration(TextDecoration.STRIKETHROUGH))
&& (parentStyle == null || !(parentStyle.hasDecoration(TextDecoration.UNDERLINED) || parentStyle.hasDecoration(TextDecoration.STRIKETHROUGH)))) {
builder.color(null);
}
// ITALIC/OBFUSCATED don't affect spaces (in modern versions), as these styles only affect glyph rendering
builder.decoration(TextDecoration.ITALIC, TextDecoration.State.NOT_SET);
builder.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.NOT_SET);
// UNDERLINE/STRIKETHROUGH affects spaces because the line renders on top
// BOLD affects spaces because it increments the character advance by 1
// font affects spaces in 1.19+ (since 22w11a), due to the font glyph provider for spaces
return builder.build();
}

@kashike kashike self-assigned this Jan 15, 2023
@kezz kezz closed this as completed in fa14e85 Jan 17, 2023
kezz added a commit that referenced this issue Jan 17, 2023
bug(api): workaround which fixes #849
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants