2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Zach Brown <1254957+zachbr@users.noreply.github.com>
Date: Sat, 22 Sep 2018 15:56:59 -0400
2024-01-21 13:56:22 +01:00
Subject: [PATCH] Catch JsonParseException in entity and block entity names
2021-06-11 14:02:28 +02:00
As a result, data that no longer parses correctly will not crash the server
instead just logging the exception and continuing (and in most cases should
fix the data)
Player data is fixed pretty much immediately but some block data (like
Shulkers) may need to be changed in order for it to re-save properly
No more crashing though.
2022-10-24 21:43:46 +02:00
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
2021-06-11 14:02:28 +02:00
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
2022-10-24 21:43:46 +02:00
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
2021-06-11 14:02:28 +02:00
@@ -0,0 +0,0 @@ public final class MCUtil {
2024-03-03 22:40:18 +01:00
return builder.build();
2021-06-11 14:02:28 +02:00
}
2023-09-22 00:26:51 +02:00
+
2021-06-11 14:02:28 +02:00
+ @Nullable
2024-04-24 07:43:09 +02:00
+ public static net.minecraft.network.chat.Component getBaseComponentFromNbt(final String key, final net.minecraft.nbt.CompoundTag compound, final net.minecraft.core.HolderLookup.Provider lookupProvider) {
2021-06-11 14:02:28 +02:00
+ if (!compound.contains(key)) {
+ return null;
+ }
2024-04-24 07:43:09 +02:00
+ final String string = compound.getString(key);
2021-06-11 14:02:28 +02:00
+ try {
2024-04-24 07:43:09 +02:00
+ return net.minecraft.network.chat.Component.Serializer.fromJson(string, lookupProvider);
2021-06-11 14:02:28 +02:00
+ } catch (com.google.gson.JsonParseException e) {
+ org.bukkit.Bukkit.getLogger().warning("Unable to parse " + key + " from " + compound +": " + e.getMessage());
+ }
+
+ return null;
+ }
2023-09-22 00:26:51 +02:00
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/BaseCommandBlock.java b/src/main/java/net/minecraft/world/level/BaseCommandBlock.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/level/BaseCommandBlock.java
+++ b/src/main/java/net/minecraft/world/level/BaseCommandBlock.java
@@ -0,0 +0,0 @@ public abstract class BaseCommandBlock implements CommandSource {
2021-06-13 08:48:25 +02:00
this.command = nbt.getString("Command");
this.successCount = nbt.getInt("SuccessCount");
if (nbt.contains("CustomName", 8)) {
2024-04-24 07:43:09 +02:00
- this.setCustomName(Component.Serializer.fromJson(nbt.getString("CustomName"), registries));
+ this.setCustomName(io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt, registries)); // Paper - Catch ParseException
} else {
this.setCustomName((Component) null);
2021-06-11 14:02:28 +02:00
}
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BannerBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BannerBlockEntity.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/BannerBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BannerBlockEntity.java
@@ -0,0 +0,0 @@ public class BannerBlockEntity extends BlockEntity implements Nameable {
2024-04-24 07:43:09 +02:00
protected void loadAdditional(CompoundTag nbt, HolderLookup.Provider registryLookup) {
super.loadAdditional(nbt, registryLookup);
2021-06-13 08:48:25 +02:00
if (nbt.contains("CustomName", 8)) {
2024-04-24 07:43:09 +02:00
- this.name = Component.Serializer.fromJson(nbt.getString("CustomName"), registryLookup);
+ this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt, registryLookup); // Paper - Catch ParseException
2021-06-11 14:02:28 +02:00
}
2024-04-24 07:43:09 +02:00
if (nbt.contains("patterns")) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java
@@ -0,0 +0,0 @@ public abstract class BaseContainerBlockEntity extends BlockEntity implements Co
2024-04-24 07:43:09 +02:00
super.loadAdditional(nbt, registryLookup);
2021-06-13 08:48:25 +02:00
this.lockKey = LockCode.fromTag(nbt);
if (nbt.contains("CustomName", 8)) {
2024-04-24 07:43:09 +02:00
- this.name = Component.Serializer.fromJson(nbt.getString("CustomName"), registryLookup);
+ this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt, registryLookup); // Paper - Catch ParseException
2022-01-06 12:40:51 +01:00
}
}
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 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
2022-12-07 20:22:28 +01:00
@@ -0,0 +0,0 @@ public class BeaconBlockEntity extends BlockEntity implements MenuProvider, Name
2023-09-22 00:26:51 +02:00
this.secondaryPower = BeaconBlockEntity.loadEffect(nbt, "secondary_effect");
this.levels = nbt.getInt("Levels"); // CraftBukkit - SPIGOT-5053, use where available
2022-01-06 12:40:51 +01:00
if (nbt.contains("CustomName", 8)) {
2024-04-24 07:43:09 +02:00
- this.name = Component.Serializer.fromJson(nbt.getString("CustomName"), registryLookup);
+ this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt, registryLookup); // Paper - Catch ParseException
2022-01-06 12:40:51 +01:00
}
this.lockKey = LockCode.fromTag(nbt);
2024-04-24 07:43:09 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/EnchantingTableBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/EnchantingTableBlockEntity.java
2022-01-06 12:40:51 +01:00
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
2024-04-24 07:43:09 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/EnchantingTableBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/EnchantingTableBlockEntity.java
@@ -0,0 +0,0 @@ public class EnchantingTableBlockEntity extends BlockEntity implements Nameable
protected void loadAdditional(CompoundTag nbt, HolderLookup.Provider registryLookup) {
super.loadAdditional(nbt, registryLookup);
2022-01-06 12:40:51 +01:00
if (nbt.contains("CustomName", 8)) {
2024-04-24 07:43:09 +02:00
- this.name = Component.Serializer.fromJson(nbt.getString("CustomName"), registryLookup);
+ this.name = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("CustomName", nbt, registryLookup); // Paper - Catch ParseException
2021-06-11 14:02:28 +02:00
}
}
2024-04-12 21:14:06 +02:00
2024-04-24 07:43:09 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/SkullBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/SkullBlockEntity.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/SkullBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/SkullBlockEntity.java
@@ -0,0 +0,0 @@ public class SkullBlockEntity extends BlockEntity {
}
if (nbt.contains("custom_name", 8)) {
- this.customName = Component.Serializer.fromJson(nbt.getString("custom_name"), registryLookup);
+ this.customName = io.papermc.paper.util.MCUtil.getBaseComponentFromNbt("custom_name", nbt, registryLookup); // Paper - Catch ParseException
} else {
this.customName = null;
}