mirror of
https://github.com/AtlasMediaGroup/Scissors.git
synced 2024-11-27 07:05:39 +00:00
195 lines
13 KiB
Diff
195 lines
13 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Telesphoreo <me@telesphoreo.me>
|
|
Date: Sun, 30 Oct 2022 18:40:45 -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 e7d9e2d8c87ddf3658b1c2e0f2a3e98ef8080cec..16192d2af2bd2c3f0bd4376e5fd7d6e46dac897c 100644
|
|
--- a/src/main/java/net/minecraft/network/chat/Component.java
|
|
+++ b/src/main/java/net/minecraft/network/chat/Component.java
|
|
@@ -26,6 +26,7 @@ import java.util.List;
|
|
import java.util.Map.Entry;
|
|
import java.util.Optional;
|
|
import javax.annotation.Nullable;
|
|
+import net.minecraft.ChatFormatting;
|
|
import net.minecraft.Util;
|
|
import net.minecraft.network.chat.contents.BlockDataSource;
|
|
import net.minecraft.network.chat.contents.DataSource;
|
|
@@ -506,6 +507,26 @@ public interface Component extends Message, FormattedText, Iterable<Component> {
|
|
return GsonHelper.toStableString(Serializer.toJsonTree(text));
|
|
}
|
|
|
|
+ // 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
|
|
+
|
|
public static JsonElement toJsonTree(Component text) {
|
|
return Component.Serializer.GSON.toJsonTree(text);
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/network/chat/HoverEvent.java b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
|
index d38562b518b28ba4617d39e4587c7fa72859ab11..454e73bbcf449561e18bb8565932872c5a62a6e1 100644
|
|
--- a/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
|
+++ b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
|
@@ -79,7 +79,7 @@ public class HoverEvent {
|
|
if (jsonElement != null) {
|
|
return action.deserialize(jsonElement);
|
|
} else {
|
|
- Component component = Component.Serializer.fromJson(json.get("value"));
|
|
+ Component component = Component.Serializer.fromJsonSafe(json.get("value")); // Scissors - Use safer method for getting Components from JSON
|
|
return component != null ? action.deserializeFromLegacy(component) : null;
|
|
}
|
|
}
|
|
@@ -94,7 +94,7 @@ public class HoverEvent {
|
|
}
|
|
|
|
public static class Action<T> {
|
|
- public static final HoverEvent.Action<Component> SHOW_TEXT = new HoverEvent.Action<>("show_text", true, Component.Serializer::fromJson, Component.Serializer::toJsonTree, Function.identity());
|
|
+ public static final HoverEvent.Action<Component> SHOW_TEXT = new HoverEvent.Action<>("show_text", true, Component.Serializer::fromJsonSafe, Component.Serializer::toJsonTree, Function.identity()); // Scissors - Use safer method for getting Components from JSON
|
|
public static final HoverEvent.Action<HoverEvent.ItemStackInfo> SHOW_ITEM = new HoverEvent.Action<>("show_item", true, HoverEvent.ItemStackInfo::create, HoverEvent.ItemStackInfo::serialize, HoverEvent.ItemStackInfo::create);
|
|
public static final HoverEvent.Action<HoverEvent.EntityTooltipInfo> SHOW_ENTITY = new HoverEvent.Action<>("show_entity", true, HoverEvent.EntityTooltipInfo::create, HoverEvent.EntityTooltipInfo::serialize, HoverEvent.EntityTooltipInfo::create);
|
|
private static final Map<String, HoverEvent.Action<?>> LOOKUP = Stream.of(SHOW_TEXT, SHOW_ITEM, SHOW_ENTITY).collect(ImmutableMap.toImmutableMap(HoverEvent.Action::getName, (action) -> {
|
|
@@ -182,7 +182,7 @@ public class HoverEvent {
|
|
return null;
|
|
}
|
|
// Scissors end
|
|
- Component component = Component.Serializer.fromJson(jsonObject.get("name"));
|
|
+ Component component = Component.Serializer.fromJsonSafe(jsonObject.get("name")); // Scissors - Use safer method for getting Components from JSON
|
|
return new HoverEvent.EntityTooltipInfo(entityType, uUID, component);
|
|
}
|
|
}
|
|
@@ -191,7 +191,7 @@ public class HoverEvent {
|
|
public static HoverEvent.EntityTooltipInfo create(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 = Registry.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 97a2657bc98d41c3c1e376b266d2c85f685acc88..7b6476455e095eed15c92797ce3a3e1146dbdc3f 100644
|
|
--- a/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
|
|
+++ b/src/main/java/net/minecraft/network/chat/contents/NbtContents.java
|
|
@@ -8,6 +8,7 @@ import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
import javax.annotation.Nullable;
|
|
+import net.kyori.adventure.text.TextComponent;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.arguments.NbtPathArgument;
|
|
import net.minecraft.nbt.Tag;
|
|
@@ -107,10 +108,11 @@ 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); // Scissors
|
|
return Stream.of(ComponentUtils.updateForEntity(source, mutableComponent, sender, depth));
|
|
} catch (Exception var5) {
|
|
- LOGGER.warn("Failed to parse component: {}", text, var5);
|
|
+ // Scissors - don't log
|
|
+ // LOGGER.warn("Failed to parse component: {}", text, var5);
|
|
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 6fa47becd0f83ac4273ef3a10c314aa27b08184b..a931aaa05f3e8c499e7e968f6fa1e908d0f2ea24 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
|
@@ -2315,12 +2315,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
|
this.setRot(this.getYRot(), this.getXRot());
|
|
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/level/block/entity/BeaconBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
|
index bb4ab75ec0e7a5d71dafeeb55e26d2204455e7a1..5ae750a2f12917ecddb0c14c23e6b02735385599 100644
|
|
--- a/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
|
@@ -408,7 +408,7 @@ public class BeaconBlockEntity extends BlockEntity implements MenuProvider {
|
|
this.levels = nbt.getInt("Levels"); // SPIGOT-5053, use where available
|
|
// CraftBukkit end
|
|
if (nbt.contains("CustomName", 8)) {
|
|
- this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt); // Paper - Catch ParseException
|
|
+ this.name = Component.Serializer.fromJsonSafe(nbt.getString("CustomName")); // Scissors - Use safer method for getting Components from JSON
|
|
}
|
|
|
|
this.lockKey = LockCode.fromTag(nbt);
|
|
diff --git a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
|
index 65e1381bb2d10bd212463feb602c60f8fdb9ade1..3e23451066894ebd88cad7022970cb2c0e9b75db 100644
|
|
--- a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
|
@@ -42,7 +42,7 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
|
|
public void load(CompoundTag nbt) {
|
|
super.load(nbt);
|
|
if (nbt.contains("CustomName", 8)) {
|
|
- this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt); // Paper - Catch ParseException
|
|
+ this.name = Component.Serializer.fromJsonSafe(nbt.getString("CustomName")); // Scissors - Use safer method for getting Components from JSON
|
|
}
|
|
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
|
index 2be7a697f08045b974579e6942b38571e744efac..84ec21c38bb44db1e9ff26c01d5c8af1a2417616 100644
|
|
--- a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
|
+++ b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
|
@@ -35,7 +35,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);
|
|
}
|
|
@@ -53,14 +53,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);
|
|
}
|
|
@@ -115,7 +115,7 @@ public class ScoreboardSaveData extends SavedData {
|
|
CompoundTag compoundTag = nbt.getCompound(i);
|
|
ObjectiveCriteria.byName(compoundTag.getString("CriteriaName")).ifPresent((criterion) -> {
|
|
String string = 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"));
|
|
this.scoreboard.addObjective(string, criterion, component, renderType);
|
|
});
|