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.
This commit is contained in:
Owen1212055
2025-06-22 15:24:16 -04:00
parent 803baf0ba6
commit 95e165156e
3 changed files with 17 additions and 2 deletions

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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