From 95e165156ecb59a2489f38f511db0bb7535a3d65 Mon Sep 17 00:00:00 2001 From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> Date: Sun, 22 Jun 2025 15:24:16 -0400 Subject: [PATCH] Attempt to patch edge cases in component serialization roundtrip. This is a bandaid fix, but vanilla shouldn't really be creating illegal components like this. --- .../net/minecraft/commands/Commands.java.patch | 2 +- .../io/papermc/paper/adventure/PaperAdventure.java | 13 +++++++++++++ .../org/bukkit/craftbukkit/entity/CraftEntity.java | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch b/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch index 1910b261ab..0f834fb5c2 100644 --- a/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch +++ b/paper-server/patches/sources/net/minecraft/commands/Commands.java.patch @@ -118,7 +118,7 @@ + // source.sendFailure(mutableComponent); + builder + .append(net.kyori.adventure.text.Component.newline()) -+ .append(io.papermc.paper.adventure.PaperAdventure.asAdventure(mutableComponent)); ++ .append(io.papermc.paper.adventure.PaperAdventure.asAdventureSafe(mutableComponent)); // Console output can cause section characters + } + org.bukkit.event.command.UnknownCommandEvent event = new org.bukkit.event.command.UnknownCommandEvent(source.getBukkitSender(), command, org.spigotmc.SpigotConfig.unknownCommandMessage.isEmpty() ? null : builder.build()); + org.bukkit.Bukkit.getServer().getPluginManager().callEvent(event); diff --git a/paper-server/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/paper-server/src/main/java/io/papermc/paper/adventure/PaperAdventure.java index 32b378681f..c144131800 100644 --- a/paper-server/src/main/java/io/papermc/paper/adventure/PaperAdventure.java +++ b/paper-server/src/main/java/io/papermc/paper/adventure/PaperAdventure.java @@ -27,6 +27,7 @@ import net.kyori.adventure.text.TranslationArgument; import net.kyori.adventure.text.event.DataComponentValue; import net.kyori.adventure.text.event.DataComponentValueConverterRegistry; import net.kyori.adventure.text.flattener.ComponentFlattener; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.serializer.ComponentSerializer; @@ -187,6 +188,18 @@ public final class PaperAdventure { // Component + // There are edge cases where vanilla can create illegal components that fail the adventure codec roundtrips. + // Handle those "safely" + // Currently this is needed for command related click events that may have section characters in NMS. + public static @NotNull Component asAdventureSafe(@Nullable final net.minecraft.network.chat.Component component) { + try { + return asAdventure(component); + } catch (RuntimeException runtimeException) { + runtimeException.printStackTrace(); + return Component.translatable("multiplayer.message_not_delivered", "Adventure roundtrip").color(NamedTextColor.RED); + } + } + public static @NotNull Component asAdventure(@Nullable final net.minecraft.network.chat.Component component) { return component == null ? Component.empty() : WRAPPER_AWARE_SERIALIZER.deserialize(component); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java index a41f481a1e..7a088a61f4 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -797,7 +797,9 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { @Override public net.kyori.adventure.text.@org.jetbrains.annotations.NotNull Component teamDisplayName() { - return io.papermc.paper.adventure.PaperAdventure.asAdventure(this.getHandle().getDisplayName()); + // Player entities could possibly create a section character in the click event + // due to their game profile having an illegal name + return io.papermc.paper.adventure.PaperAdventure.asAdventureSafe(this.getHandle().getDisplayName()); } @Override