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

134 lines
8.3 KiB
Diff
Raw Normal View History

2024-05-08 17:17:21 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Telesphoreo <me@telesphoreo.me>
Date: Wed, 8 May 2024 12:15:48 -0500
Subject: [PATCH] Better handling of invalid JSON components
diff --git a/src/main/java/net/minecraft/network/chat/Component.java b/src/main/java/net/minecraft/network/chat/Component.java
index 6dcade427f19771b08e04cfa036dedcfac30b5cd..3b075dcb6bda40b278296fc8750f2af90c13acc4 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.core.HolderLookup;
import net.minecraft.network.chat.contents.DataSource;
import net.minecraft.network.chat.contents.KeybindContents;
@@ -311,6 +313,26 @@ public interface Component extends Message, FormattedText, Iterable<Component> {
return json == null ? null : Serializer.deserialize(json, registries);
}
+ // Scissors start
+ @Nullable
+ public static MutableComponent fromJsonSafe(String json, HolderLookup.Provider registries) {
+ try {
+ return fromJson(json, registries);
+ } catch (Exception ex) {
+ return Component.empty().append("** Invalid JSON Component **").withStyle(ChatFormatting.RED);
+ }
+ }
+
+ @Nullable
+ public static MutableComponent fromJsonSafe(JsonElement json, HolderLookup.Provider registries) {
+ try {
+ return fromJson(json, registries);
+ } catch (Exception ex) {
+ return Component.empty().append("** Invalid JSON Component **").withStyle(ChatFormatting.RED);
+ }
+ }
+ // Scissors end
+
@Nullable
public static MutableComponent fromJsonLenient(String json, HolderLookup.Provider registries) {
JsonReader jsonreader = new JsonReader(new StringReader(json));
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 df26c39a2bb20e2021b50211dce905483a77f4e6..0b85d5aef65fdb08c1c11f0ebb973078b57e5773 100644
--- a/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
+++ b/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
@@ -124,10 +124,10 @@ public class NbtContents implements ComponentContents {
);
return stream.flatMap(text -> {
try {
- MutableComponent mutableComponent = Component.Serializer.fromJson(text, source.registryAccess());
+ MutableComponent mutableComponent = Component.Serializer.fromJsonSafe(text, source.registryAccess()); // Scissors
return Stream.of(ComponentUtils.updateForEntity(source, mutableComponent, sender, depth));
} catch (Exception var5x) {
- LOGGER.warn("Failed to parse component: {}", text, var5x);
+ // Scissors - don't log
return Stream.of();
}
}).reduce((accumulator, current) -> accumulator.append(component).append(current)).orElseGet(Component::empty);
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
2024-06-14 23:02:52 +00:00
index 7ef9f67d27cc240191dd5d07e8dcf5fbdebe1049..6ee2d11e4f3fbc8424a2ffbe6a7ebd1832a25503 100644
2024-05-08 17:17:21 +00:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2024-06-14 23:02:52 +00:00
@@ -2602,12 +2602,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2024-05-08 17:17:21 +00:00
this.setRot(this.getYRot(), this.getXRot());
if (nbt.contains("CustomName", 8)) {
String s = nbt.getString("CustomName");
-
- try {
- this.setCustomName(Component.Serializer.fromJson(s, this.registryAccess()));
- } catch (Exception exception) {
- Entity.LOGGER.warn("Failed to parse entity custom name {}", s, exception);
- }
+ this.setCustomName(Component.Serializer.fromJsonSafe(s, this.registryAccess())); // Scissors - Use safer method for getting Components from JSON
}
this.setCustomNameVisible(nbt.getBoolean("CustomNameVisible"));
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
2024-06-14 23:02:52 +00:00
index c0563260277f9f4bd9ff08993b2efb4bca9a0c60..f3c296f2464620083e44c14cc99eca1add8f8d9b 100644
2024-05-08 17:17:21 +00:00
--- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
@@ -362,7 +362,7 @@ public abstract class BlockEntity {
@Nullable
public static Component parseCustomNameSafe(String json, HolderLookup.Provider registryLookup) {
try {
- return Component.Serializer.fromJson(json, registryLookup);
+ return Component.Serializer.fromJsonSafe(json, registryLookup); // Scissors
} catch (Exception exception) {
BlockEntity.LOGGER.warn("Failed to parse custom name from string '{}', discarding", json, exception);
return null;
diff --git a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
index b679bbdfa307d3aa9f2f5e4c70c559695b2733de..4c195b56b956b31028c4f51453d6e9bf31cada0c 100644
--- a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
+++ b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
@@ -42,7 +42,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"), registries);
+ Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName"), registries); // Scissors - Use safer method for getting Components from JSON
if (component != null) {
playerTeam.setDisplayName(component);
}
@@ -60,14 +60,14 @@ public class ScoreboardSaveData extends SavedData {
}
if (compoundTag.contains("MemberNamePrefix", 8)) {
- Component component2 = Component.Serializer.fromJson(compoundTag.getString("MemberNamePrefix"), registries);
+ Component component2 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNamePrefix"), registries); // 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"), registries);
+ Component component3 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNameSuffix"), registries); // Scissors - Use safer method for getting Components from JSON
if (component3 != null) {
playerTeam.setPlayerSuffix(component3);
}
@@ -124,7 +124,7 @@ public class ScoreboardSaveData extends SavedData {
return ObjectiveCriteria.DUMMY;
});
String string2 = compoundTag.getString("Name");
- Component component = Component.Serializer.fromJson(compoundTag.getString("DisplayName"), registries);
+ Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName"), registries); // 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