KyoriPowered / adventure

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

Serializes lose `hoverEvent` between Json -> Component conversions and vice versa #1025

Open LoneDev6 opened 6 months ago

LoneDev6 commented 6 months ago

For some reason serializes lose hoverEvent between Json -> Component conversions and vice versa.

Server version: 1.19.4. Current: git-Purpur-1985 (MC: 1.19.4)

        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>adventure-api</artifactId>
            <version>4.15.0</version>
        </dependency>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>adventure-text-serializer-gson</artifactId>
            <version>4.15.0</version>
        </dependency>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>adventure-text-serializer-gson-legacy-impl</artifactId>
            <version>4.15.0</version>
        </dependency>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>adventure-platform-bukkit</artifactId>
            <version>4.3.2</version>
        </dependency>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>adventure-text-minimessage</artifactId>
            <version>4.15.0</version>
        </dependency>

My code:

GsonComponentSerializer.gson().serialize(
        GsonComponentSerializer.gson().deserialize(jsonString)
);

Second attempt specifying legacy hover serializer (same result).

GsonComponentSerializer gson = GsonComponentSerializer.builder()
                .legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
                .options(
                        OptionState.optionState()
                                .value(JSONOptions.EMIT_RGB, Boolean.TRUE)
                                .value(JSONOptions.EMIT_HOVER_EVENT_TYPE, JSONOptions.HoverEventValueMode.BOTH)
                                .value(JSONOptions.EMIT_COMPACT_TEXT_COMPONENT, Boolean.TRUE)
                                .value(JSONOptions.EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, Boolean.FALSE)
                                .value(JSONOptions.VALIDATE_STRICT_EVENTS, Boolean.FALSE)
                                .build()
                )
                .build();;
        String bro = gson.serialize(
                gson.deserialize(buggedJsonStr)
        );

This component was obtained from another plugin called ChatControlRed.

Before

{
  "extra": [
    {
      "hoverEvent": {
        "action": "show_text",
        "value": [
          {
            "extra": [
              {
                "text": "flpm_9eeed723-306e-44c0-9bb9-0c138a9e2f9a flps_68bbf558-ed0e-37ab-bb3e-9c8672097642"
              }
            ],
            "text": ""
          }
        ]
      },
      "text": ""
    },
    {
      "extra": [
        { "color": "dark_gray", "text": "[" },
        { "color": "dark_red", "text": "admin" },
        { "color": "dark_gray", "text": "] " },
        { "color": "white", "text": "LoneDev" },
        { "color": "gray", "text": ": " },
        { "color": "white", "text": "" }
      ],
      "text": ""
    },
    {
      "extra": [
        {
          "color": "gray",
          "hoverEvent": {
            "action": "show_item",
            "value": [{ "text": "{Count:59b,id:\"minecraft:stone\"}" }]
          },
          "text": "["
        },
        {
          "color": "yellow",
          "hoverEvent": {
            "action": "show_item",
            "value": [{ "text": "{Count:59b,id:\"minecraft:stone\"}" }]
          },
          "text": "59x "
        },
        {
          "color": "white",
          "hoverEvent": {
            "action": "show_item",
            "value": [{ "text": "{Count:59b,id:\"minecraft:stone\"}" }]
          },
          "text": "Stone"
        },
        {
          "color": "gray",
          "hoverEvent": {
            "action": "show_item",
            "value": [{ "text": "{Count:59b,id:\"minecraft:stone\"}" }]
          },
          "text": "]"
        }
      ],
      "text": ""
    }
  ],
  "text": ""
}

After

{
  "extra": [
    {
      "hoverEvent": {
        "action": "show_text",
        "contents": {
          "extra": [
            "flpm_9eeed723-306e-44c0-9bb9-0c138a9e2f9a flps_68bbf558-ed0e-37ab-bb3e-9c8672097642"
          ],
          "text": ""
        }
      },
      "text": ""
    },
    {
      "extra": [
        { "color": "dark_gray", "text": "[" },
        { "color": "dark_red", "text": "admin" },
        { "color": "dark_gray", "text": "] " },
        { "color": "white", "text": "LoneDev" },
        { "color": "gray", "text": ": " },
        { "color": "white", "text": "" }
      ],
      "text": ""
    },
    {
      "extra": [
        { "color": "gray", "text": "[" },
        { "color": "yellow", "text": "59x " },
        { "color": "white", "text": "Stone" },
        { "color": "gray", "text": "]" }
      ],
      "text": ""
    }
  ],
  "text": ""
}
LoneDev6 commented 6 months ago

This doesn't lose any data, so the issue is probably caused by the "value" tag, I think. The issue might be caused by the fact that some plugins are sending legacy hover events data and for some reason Adventure is discarding them.

{
  "extra": [
    { "color": "gold", "text": "LoneDev's item is " },
    { "color": "gold", "text": "" },
    { "color": "gold", "text": "" },
    {
      "color": "white",
      "hoverEvent": {
        "action": "show_item",
        "contents": { "id": "minecraft:wooden_axe", "tag": "{Damage:10}" }
      },
      "text": "Wooden Axe x1"
    }
  ],
  "text": ""
}
zml2008 commented 6 months ago

What happens when you enable validating strict events, rather than telling the serializer to silently discard any useful information for this issue

(also you seem to have done part of your process incorrectly inserting legacy characters in your components, we'd recommend fixing that to avoid inconsistent styling)

LoneDev6 commented 6 months ago

Thanks! I'd expect the adventure API to allow legacy components as the client shows these messages correctly even on modern versions. I am not the author of the other plugin so I have no control over their malformed JSON, but the client accepts it so I guess adventure should also do that.

Let me know what you think, thanks.

MiniDigger commented 6 months ago

mojang says to not use it, as such adventure doesn't support (and in some cases even block) legacy formatting characters https://bugs.mojang.com/browse/MC-190605?focusedId=993040#comment-993040

LoneDev6 commented 5 months ago

It's not about § legacy colors, it's about the legacy way to create hover events. I edited the message to avoid confusion: https://github.com/KyoriPowered/adventure/issues/1025#issuecomment-1876881599