PaperMC / Paper

The most widely used, high performance Minecraft server that aims to fix gameplay and mechanics inconsistencies
https://papermc.io/
Other
9.39k stars 2.2k forks source link

GsonComponentSerializer cannot serialize LeatherArmor hover event with armor trim #10842

Closed StarTux closed 1 month ago

StarTux commented 1 month ago

Expected behavior

When we run this code:

        ItemStack item = new ItemStack(Material.LEATHER_CHESTPLATE);
        item.editMeta(ArmorMeta.class, m -> m.setTrim(new ArmorTrim(TrimMaterial.QUARTZ, TrimPattern.SILENCE)));
        Component component = Component.empty().hoverEvent(item.asHoverEvent());
        String string = GsonComponentSerializer.gson().serialize(component);

One would expect that the string variable now holds a nice representation of the chat component.

Observed/Actual behavior

Instead it throws and we get this lengthy error message

[14:39:19 WARN]: Unexpected exception while parsing console command "test"                                                                                                                                                         [80/1915]
org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin Test v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[paper-api-1.20.6-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:164) ~[paper-api-1.20.6-R0.1-SNAPSHOT.jar:?]                                                                                                                  
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:964) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                    
        at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:949) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                             
        at net.minecraft.server.dedicated.DedicatedServer.handleConsoleInputs(DedicatedServer.java:521) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                                                              
        at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:468) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                                                                     
        at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1571) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                                                                                
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1231) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                                                                                 
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:323) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]                                                                                                              
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]                                                    
Caused by: java.lang.IllegalStateException: Failed to convert data component value of type class io.papermc.paper.adventure.PaperAdventure$DataComponentValueImpl to type interface net.kyori.adventure.text.serializer.gson.GsonDataCompone
ntValue due to an error in a converter provided by adventure:platform/paper!
        at net.kyori.adventure.text.event.DataComponentValueConverterRegistry.convert(DataComponentValueConverterRegistry.java:96) ~[adventure-api-4.17.0.jar:?]
        at net.kyori.adventure.text.event.HoverEvent$ShowItem.dataComponentsAs(HoverEvent.java:623) ~[adventure-api-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ShowItemSerializer.write(ShowItemSerializer.java:139) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ShowItemSerializer.write(ShowItemSerializer.java:49) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.StyleSerializer.write(StyleSerializer.java:296) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.StyleSerializer.write(StyleSerializer.java:62) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJsonTree(Gson.java:712) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.ComponentSerializerImpl.write(ComponentSerializerImpl.java:252) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ComponentSerializerImpl.write(ComponentSerializerImpl.java:77) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:812) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:759) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:736) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.GsonComponentSerializerImpl.serialize(GsonComponentSerializerImpl.java:104) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.GsonComponentSerializerImpl.serialize(GsonComponentSerializerImpl.java:41) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at Test.jar/com.cavetale.test.TestPlugin.onCommand(TestPlugin.java:71) ~[Test.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper-api-1.20.6-R0.1-SNAPSHOT.jar:?]
        ... 9 more
Caused by: java.lang.IllegalStateException: Can't access registry ResourceKey[minecraft:root / minecraft:item]; Can't access registry ResourceKey[minecraft:root / minecraft:item]
        at com.mojang.serialization.DataResult$Error.getOrThrow(DataResult.java:275) ~[datafixerupper-7.0.14.jar:?]
        at com.mojang.serialization.DataResult.getOrThrow(DataResult.java:79) ~[datafixerupper-7.0.14.jar:?]
        at io.papermc.paper.adventure.providers.DataComponentValueConverterProviderImpl.lambda$conversions$0(DataComponentValueConverterProviderImpl.java:40) ~[paper-1.20.6.jar:1.20.6-130-5d8e53d]
        at net.kyori.adventure.text.event.DataComponentValueConversionImpl.convert(DataComponentValueConversionImpl.java:59) ~[adventure-api-4.17.0.jar:?]
        at net.kyori.adventure.text.event.DataComponentValueConverterRegistry.convert(DataComponentValueConverterRegistry.java:94) ~[adventure-api-4.17.0.jar:?]
        at net.kyori.adventure.text.event.HoverEvent$ShowItem.dataComponentsAs(HoverEvent.java:623) ~[adventure-api-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ShowItemSerializer.write(ShowItemSerializer.java:139) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ShowItemSerializer.write(ShowItemSerializer.java:49) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.StyleSerializer.write(StyleSerializer.java:296) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.StyleSerializer.write(StyleSerializer.java:62) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJsonTree(Gson.java:712) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.ComponentSerializerImpl.write(ComponentSerializerImpl.java:252) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.ComponentSerializerImpl.write(ComponentSerializerImpl.java:77) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:196) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:842) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:812) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:759) ~[gson-2.10.1.jar:?]
        at com.google.gson.Gson.toJson(Gson.java:736) ~[gson-2.10.1.jar:?]
        at net.kyori.adventure.text.serializer.gson.GsonComponentSerializerImpl.serialize(GsonComponentSerializerImpl.java:104) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at net.kyori.adventure.text.serializer.gson.GsonComponentSerializerImpl.serialize(GsonComponentSerializerImpl.java:41) ~[adventure-text-serializer-gson-4.17.0.jar:?]
        at Test.jar/com.cavetale.test.TestPlugin.onCommand(TestPlugin.java:71) ~[Test.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper-api-1.20.6-R0.1-SNAPSHOT.jar:?]
        ... 9 more

I hope this helps.

Steps/models to reproduce

Plugin and Datapack List

Bukkit Plugins:

There are 3 data pack(s) enabled: [vanilla (built-in)], [file/bukkit (world)], [paper (built-in)]

Paper version

This server is running Paper version 1.20.6-130-master@5d8e53d (2024-06-01T14:32:53Z) (Implementing API version 1.20.6-R0.1-SNAPSHOT) You are running the latest version

Other

Machine-Maker commented 1 month ago

Ah yeah, I think I noticed this as a potential issue about a week ago but forgot about it. We need to be using a RegistryOps w/Context for encoding/decoding the data component stuff in the adventure converter registry.