Scissors/patches/server/0010-Better-handling-of-invalid-JSON-components.patch

151 lines
9.0 KiB
Diff
Raw Normal View History

2022-07-28 03:57:50 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2024-02-13 21:52:00 +00:00
From: Video <videogamesm12@gmail.com>
2023-12-10 23:26:35 +00:00
Date: Sun, 10 Dec 2023 17:26:11 -0600
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
index 850f75172e9efa72cabb8e5bd124b96a0b1a945f..2c3ef2a0c745cf6b470555706389e0ce4a15cb67 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
@@ -649,12 +649,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;
+ return net.minecraft.network.chat.Component.Serializer.fromJsonSafe(string);
}
}
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
2023-12-14 00:42:08 +00:00
index d3a80d0a23be762c05931ae8001d98e43cab2b4a..ba13650b52e39c9cc5cfa421f7720c7d4ba75678 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
2023-12-14 00:42:08 +00:00
@@ -23,6 +23,8 @@ import java.util.List;
2022-07-28 03:57:50 +00:00
import java.util.Optional;
2023-12-10 23:26:35 +00:00
import java.util.UUID;
2022-07-28 03:57:50 +00:00
import javax.annotation.Nullable;
2023-12-10 23:26:35 +00:00
+
+import net.minecraft.ChatFormatting;
2022-07-28 03:57:50 +00:00
import net.minecraft.Util;
import net.minecraft.network.chat.contents.DataSource;
2023-12-10 23:26:35 +00:00
import net.minecraft.network.chat.contents.KeybindContents;
2023-12-14 00:42:08 +00:00
@@ -315,6 +317,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
2023-12-10 23:26:35 +00:00
index dbe9f81e298a931c3e0e5b879dc13b493b3fdb52..6708290b6d096e8081c56d2e0da3c9ae2a112176 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
2023-12-10 23:26:35 +00:00
@@ -160,7 +160,7 @@ public class HoverEvent {
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
2023-12-10 23:26:35 +00:00
index 186547674894fd084bceb478bb6017b747df4173..74c880425964da042ca57c097eb93273da59ce7e 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
2023-12-10 23:26:35 +00:00
@@ -115,10 +115,10 @@ public class NbtContents implements ComponentContents {
2022-07-28 03:57:50 +00:00
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);
2023-12-10 23:26:35 +00:00
+ MutableComponent mutableComponent = Component.Serializer.fromJsonSafe(text);
2022-07-28 03:57:50 +00:00
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
2024-02-13 21:52:00 +00:00
index 906eded9a2ab61737a30cfe89292a71237ce4eb7..ea36187ed37d59edd2481de79372599a85f04e97 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-13 21:52:00 +00:00
@@ -2529,11 +2529,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-01-24 22:30:21 +00:00
index 6c3ed29e13ea78ceb1c9bb11ce0d9938bb8b4c62..ab2f9b9a021e05ad66a3701577862fa2acf81f52 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);
}
2023-12-10 23:26:35 +00:00
@@ -126,7 +126,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");
NumberFormat numberFormat = NumberFormatTypes.CODEC.parse(NbtOps.INSTANCE, compoundTag.get("format")).result().orElse((NumberFormat)null);