2022-07-28 03:57:50 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2024-04-16 05:12:25 +00:00
|
|
|
From: Telesphoreo <me@telesphoreo.me>
|
|
|
|
Date: Mon, 15 Apr 2024 22:36:13 -0500
|
2022-07-28 03:57:50 +00:00
|
|
|
Subject: [PATCH] Better handling of invalid JSON components
|
|
|
|
|
|
|
|
|
2023-10-15 22:45:13 +00:00
|
|
|
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
|
2024-04-16 05:12:25 +00:00
|
|
|
index e028353e0261310afc42ca0454b723d9f1ffc131..ed8810319db2ce754245193a62ea7a9a32ac13ec 100644
|
2023-10-15 22:45:13 +00:00
|
|
|
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
|
|
|
|
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
|
2024-03-08 02:03:07 +00:00
|
|
|
@@ -675,12 +675,6 @@ public final class MCUtil {
|
2023-10-15 22:45:13 +00:00
|
|
|
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;
|
2024-04-16 05:12:25 +00:00
|
|
|
+ return net.minecraft.network.chat.Component.Serializer.fromJsonSafe(string); // Scissors
|
2023-10-15 22:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 03:57:50 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/network/chat/Component.java b/src/main/java/net/minecraft/network/chat/Component.java
|
2024-04-16 05:12:25 +00:00
|
|
|
index d3a80d0a23be762c05931ae8001d98e43cab2b4a..ae84d06564b2560e13404f6d89f88ad71dbc2cc6 100644
|
2022-07-28 03:57:50 +00:00
|
|
|
--- a/src/main/java/net/minecraft/network/chat/Component.java
|
|
|
|
+++ b/src/main/java/net/minecraft/network/chat/Component.java
|
2024-04-16 05:12:25 +00:00
|
|
|
@@ -315,6 +315,26 @@ public interface Component extends Message, FormattedText, Iterable<Component> {
|
2023-12-10 23:26:35 +00:00
|
|
|
return json == null ? null : Serializer.deserialize(json);
|
2022-07-28 03:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ // 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
|
|
|
|
+
|
2023-12-10 23:26:35 +00:00
|
|
|
@Nullable
|
|
|
|
public static MutableComponent fromJsonLenient(String json) {
|
|
|
|
JsonReader jsonreader = new JsonReader(new StringReader(json));
|
2022-07-28 03:57:50 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/network/chat/HoverEvent.java b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
2024-04-16 05:12:25 +00:00
|
|
|
index a82b4e39c3160114aded3f33c85fad2e95065b72..e1f15ac481ab598e420ad79bcf289251d9f1c99a 100644
|
2022-07-28 03:57:50 +00:00
|
|
|
--- a/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
|
|
|
+++ b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
2024-04-16 05:12:25 +00:00
|
|
|
@@ -147,7 +147,7 @@ public class HoverEvent {
|
2023-12-10 23:26:35 +00:00
|
|
|
public static DataResult<HoverEvent.EntityTooltipInfo> legacyCreate(Component text) {
|
2022-07-28 03:57:50 +00:00
|
|
|
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
|
2022-12-11 06:04:11 +00:00
|
|
|
EntityType<?> entityType = BuiltInRegistries.ENTITY_TYPE.get(new ResourceLocation(compoundTag.getString("type")));
|
2022-07-28 03:57:50 +00:00
|
|
|
// 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
|
2024-04-16 05:12:25 +00:00
|
|
|
index c5673657ca6332147a5cbd2f4107fee13b9b7f47..3fec6f3e0adef6e2094157ef9040c7c6dc41a6c2 100644
|
2022-07-28 03:57:50 +00:00
|
|
|
--- a/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
|
|
|
|
+++ b/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
|
2024-04-16 05:12:25 +00:00
|
|
|
@@ -124,10 +124,10 @@ public class NbtContents implements ComponentContents {
|
|
|
|
);
|
|
|
|
return stream.flatMap(text -> {
|
2022-07-28 03:57:50 +00:00
|
|
|
try {
|
|
|
|
- MutableComponent mutableComponent = Component.Serializer.fromJson(text);
|
2024-04-16 05:12:25 +00:00
|
|
|
+ MutableComponent mutableComponent = Component.Serializer.fromJsonSafe(text); // Scissors
|
2022-07-28 03:57:50 +00:00
|
|
|
return Stream.of(ComponentUtils.updateForEntity(source, mutableComponent, sender, depth));
|
2024-04-16 05:12:25 +00:00
|
|
|
} catch (Exception var5x) {
|
|
|
|
- LOGGER.warn("Failed to parse component: {}", text, var5x);
|
2022-07-28 03:57:50 +00:00
|
|
|
+ // Scissors - don't log
|
|
|
|
return Stream.of();
|
|
|
|
}
|
2024-04-16 05:12:25 +00:00
|
|
|
}).reduce((accumulator, current) -> accumulator.append(component).append(current)).orElseGet(Component::empty);
|
2022-07-28 03:57:50 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
|
2024-04-16 05:12:25 +00:00
|
|
|
index 637478fd8a284e6833cf8f5fa17ccf9d73d1dd3f..2ecf2d8ff546ed097cb3b547db22d28a31d29ec4 100644
|
2022-07-28 03:57:50 +00:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
2024-02-24 22:32:31 +00:00
|
|
|
@@ -2545,11 +2545,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource, S
|
2022-07-28 03:57:50 +00:00
|
|
|
if (nbt.contains("CustomName", 8)) {
|
|
|
|
String s = nbt.getString("CustomName");
|
2023-12-10 23:26:35 +00:00
|
|
|
|
2022-07-28 03:57:50 +00:00
|
|
|
- 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
|
2024-04-16 05:12:25 +00:00
|
|
|
index 7eb1c28886741bdfe7985d16b6824f3e85ffae50..a6b7ff2e50c9700589428cc7d61b5ef9f9082ea8 100644
|
2022-07-28 03:57:50 +00:00
|
|
|
--- a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
2023-12-10 23:26:35 +00:00
|
|
|
@@ -41,7 +41,7 @@ public class ScoreboardSaveData extends SavedData {
|
2022-07-28 03:57:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-12-10 23:26:35 +00:00
|
|
|
@@ -59,14 +59,14 @@ public class ScoreboardSaveData extends SavedData {
|
2022-07-28 03:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-04-16 05:12:25 +00:00
|
|
|
@@ -123,7 +123,7 @@ public class ScoreboardSaveData extends SavedData {
|
2023-09-24 21:03:31 +00:00
|
|
|
return ObjectiveCriteria.DUMMY;
|
2022-07-28 03:57:50 +00:00
|
|
|
});
|
2023-09-24 21:03:31 +00:00
|
|
|
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"));
|
2023-12-10 23:26:35 +00:00
|
|
|
boolean bl = compoundTag.getBoolean("display_auto_update");
|
2024-04-16 05:12:25 +00:00
|
|
|
NumberFormat numberFormat = NumberFormatTypes.CODEC.parse(NbtOps.INSTANCE, compoundTag.get("format")).result().orElse(null);
|