From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 10 Dec 2023 17:26:11 -0600 Subject: [PATCH] Better handling of invalid JSON components diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java index 200ed770b57e1a9240abf0473968d4b85cbefe3c..1a2a3b506e2f9bce77294c3788451bfd47394548 100644 --- a/src/main/java/io/papermc/paper/util/MCUtil.java +++ b/src/main/java/io/papermc/paper/util/MCUtil.java @@ -654,12 +654,6 @@ public final class MCUtil { return null; } String string = compound.getString(key); - try { - return net.minecraft.network.chat.Component.Serializer.fromJson(string); - } catch (com.google.gson.JsonParseException e) { - org.bukkit.Bukkit.getLogger().warning("Unable to parse " + key + " from " + compound +": " + e.getMessage()); - } - - return null; + return net.minecraft.network.chat.Component.Serializer.fromJsonSafe(string); } } diff --git a/src/main/java/net/minecraft/network/chat/Component.java b/src/main/java/net/minecraft/network/chat/Component.java index d3a80d0a23be762c05931ae8001d98e43cab2b4a..ba13650b52e39c9cc5cfa421f7720c7d4ba75678 100644 --- a/src/main/java/net/minecraft/network/chat/Component.java +++ b/src/main/java/net/minecraft/network/chat/Component.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Optional; import java.util.UUID; import javax.annotation.Nullable; + +import net.minecraft.ChatFormatting; import net.minecraft.Util; import net.minecraft.network.chat.contents.DataSource; import net.minecraft.network.chat.contents.KeybindContents; @@ -315,6 +317,26 @@ public interface Component extends Message, FormattedText, Iterable { return json == null ? null : Serializer.deserialize(json); } + // Scissors start + @Nullable + public static MutableComponent fromJsonSafe(String json) { + try { + return fromJson(json); + } catch (Exception ex) { + return Component.empty().append("** Invalid JSON Component **").withStyle(ChatFormatting.RED); + } + } + + @Nullable + public static MutableComponent fromJsonSafe(JsonElement json) { + try { + return fromJson(json); + } catch (Exception ex) { + return Component.empty().append("** Invalid JSON Component **").withStyle(ChatFormatting.RED); + } + } + // Scissors end + @Nullable public static MutableComponent fromJsonLenient(String json) { JsonReader jsonreader = new JsonReader(new StringReader(json)); diff --git a/src/main/java/net/minecraft/network/chat/HoverEvent.java b/src/main/java/net/minecraft/network/chat/HoverEvent.java index dbe9f81e298a931c3e0e5b879dc13b493b3fdb52..6708290b6d096e8081c56d2e0da3c9ae2a112176 100644 --- a/src/main/java/net/minecraft/network/chat/HoverEvent.java +++ b/src/main/java/net/minecraft/network/chat/HoverEvent.java @@ -160,7 +160,7 @@ public class HoverEvent { public static DataResult legacyCreate(Component text) { try { CompoundTag compoundTag = TagParser.parseTag(text.getString()); - Component component = Component.Serializer.fromJson(compoundTag.getString("name")); + Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("name")); // Scissors - Use safer method for getting Components from JSON EntityType entityType = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation(compoundTag.getString("type"))); // Scissors start UUID uUID; diff --git a/src/main/java/net/minecraft/network/chat/contents/NbtContents.java b/src/main/java/net/minecraft/network/chat/contents/NbtContents.java index 186547674894fd084bceb478bb6017b747df4173..74c880425964da042ca57c097eb93273da59ce7e 100644 --- a/src/main/java/net/minecraft/network/chat/contents/NbtContents.java +++ b/src/main/java/net/minecraft/network/chat/contents/NbtContents.java @@ -115,10 +115,10 @@ public class NbtContents implements ComponentContents { Component component = DataFixUtils.orElse(ComponentUtils.updateForEntity(source, this.separator, sender, depth), ComponentUtils.DEFAULT_NO_STYLE_SEPARATOR); return stream.flatMap((text) -> { try { - MutableComponent mutableComponent = Component.Serializer.fromJson(text); + MutableComponent mutableComponent = Component.Serializer.fromJsonSafe(text); return Stream.of(ComponentUtils.updateForEntity(source, mutableComponent, sender, depth)); } catch (Exception var5) { - LOGGER.warn("Failed to parse component: {}", text, var5); + // Scissors - don't log return Stream.of(); } }).reduce((accumulator, current) -> { diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java index 92f9d55c224cd4e25060d8751ccb28a978979241..00144a02089522e24fd0b3ee847c35c954c4af2b 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -2528,11 +2528,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S if (nbt.contains("CustomName", 8)) { String s = nbt.getString("CustomName"); - try { - this.setCustomName(Component.Serializer.fromJson(s)); - } catch (Exception exception) { - Entity.LOGGER.warn("Failed to parse entity custom name {}", s, exception); - } + this.setCustomName(Component.Serializer.fromJsonSafe(s)); // Scissors - Use safer method for getting Components from JSON } this.setCustomNameVisible(nbt.getBoolean("CustomNameVisible")); diff --git a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java index b49d60d20d170d16a7daf2bc874e5d8d04552b3e..56ae1fa22bb722f91a06d7344bf879210f4ef8f2 100644 --- a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java +++ b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java @@ -41,7 +41,7 @@ public class ScoreboardSaveData extends SavedData { CompoundTag compoundTag = nbt.getCompound(i); String string = compoundTag.getString("Name"); PlayerTeam playerTeam = this.scoreboard.addPlayerTeam(string); - Component component = Component.Serializer.fromJson(compoundTag.getString("DisplayName")); + Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName")); // Scissors - Use safer method for getting Components from JSON if (component != null) { playerTeam.setDisplayName(component); } @@ -59,14 +59,14 @@ public class ScoreboardSaveData extends SavedData { } if (compoundTag.contains("MemberNamePrefix", 8)) { - Component component2 = Component.Serializer.fromJson(compoundTag.getString("MemberNamePrefix")); + Component component2 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNamePrefix")); // Scissors - Use safer method for getting Components from JSON if (component2 != null) { playerTeam.setPlayerPrefix(component2); } } if (compoundTag.contains("MemberNameSuffix", 8)) { - Component component3 = Component.Serializer.fromJson(compoundTag.getString("MemberNameSuffix")); + Component component3 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNameSuffix")); // Scissors - Use safer method for getting Components from JSON if (component3 != null) { playerTeam.setPlayerSuffix(component3); } @@ -126,7 +126,7 @@ public class ScoreboardSaveData extends SavedData { return ObjectiveCriteria.DUMMY; }); String string2 = compoundTag.getString("Name"); - Component component = Component.Serializer.fromJson(compoundTag.getString("DisplayName")); + Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName")); // Scissors - Use safer method for getting Components from JSON ObjectiveCriteria.RenderType renderType = ObjectiveCriteria.RenderType.byId(compoundTag.getString("RenderType")); boolean bl = compoundTag.getBoolean("display_auto_update"); NumberFormat numberFormat = NumberFormatTypes.CODEC.parse(NbtOps.INSTANCE, compoundTag.get("format")).result().orElse((NumberFormat)null);