mirror of
https://github.com/AtlasMediaGroup/Scissors.git
synced 2025-06-28 16:26:41 +00:00
json components
This commit is contained in:
@ -0,0 +1,150 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Telesphoreo <me@telesphoreo.me>
|
||||
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 e0037b99838519eee5fb6f7e95ffaad252f08baf..0d3295f93895c314fcfd753f98173e85bcf8c078 100644
|
||||
--- a/src/main/java/net/minecraft/network/chat/Component.java
|
||||
+++ b/src/main/java/net/minecraft/network/chat/Component.java
|
||||
@@ -24,6 +24,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;
|
||||
@@ -324,6 +326,26 @@ public interface Component extends Message, FormattedText, Iterable<Component> {
|
||||
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<HoverEvent.EntityTooltipInfo> 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 9abe817ae202edaa2d88cd59ae5c7db0b1c634be..e3ac487e246875f5f9ee6f32746da33c0de8200e 100644
|
||||
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
||||
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
||||
@@ -2527,11 +2527,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);
|
Reference in New Issue
Block a user