From 3dd592030f1c2f7281bfdd39252fb74fb7f9ce29 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Sat, 22 Sep 2018 15:56:59 -0400 Subject: [PATCH] Catch JsonParseException in Entity and TE names 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. diff --git a/src/main/java/net/minecraft/server/CommandBlockListenerAbstract.java b/src/main/java/net/minecraft/server/CommandBlockListenerAbstract.java index 7f9030fa8..9d8fc3aa9 100644 --- a/src/main/java/net/minecraft/server/CommandBlockListenerAbstract.java +++ b/src/main/java/net/minecraft/server/CommandBlockListenerAbstract.java @@ -56,7 +56,7 @@ public abstract class CommandBlockListenerAbstract implements ICommandListener { this.command = nbttagcompound.getString("Command"); this.successCount = nbttagcompound.getInt("SuccessCount"); if (nbttagcompound.hasKeyOfType("CustomName", 8)) { - this.customName = IChatBaseComponent.ChatSerializer.a(nbttagcompound.getString("CustomName")); + this.customName = MCUtil.getBaseComponentFromNbt("CustomName", nbttagcompound); // Paper - Catch ParseException } if (nbttagcompound.hasKeyOfType("TrackOutput", 1)) { diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java index cbc0aea04..5e60baf23 100644 --- a/src/main/java/net/minecraft/server/Entity.java +++ b/src/main/java/net/minecraft/server/Entity.java @@ -1701,7 +1701,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke this.setPosition(this.locX, this.locY, this.locZ); this.setYawPitch(this.yaw, this.pitch); if (nbttagcompound.hasKeyOfType("CustomName", 8)) { - this.setCustomName(IChatBaseComponent.ChatSerializer.a(nbttagcompound.getString("CustomName"))); + this.setCustomName(MCUtil.getBaseComponentFromNbt("CustomName", nbttagcompound)); // Paper - Catch ParseException } this.setCustomNameVisible(nbttagcompound.getBoolean("CustomNameVisible")); diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java index 6d278a0da..ec3732193 100644 --- a/src/main/java/net/minecraft/server/MCUtil.java +++ b/src/main/java/net/minecraft/server/MCUtil.java @@ -339,4 +339,19 @@ public final class MCUtil { return null; } } + + @Nullable + public static IChatBaseComponent getBaseComponentFromNbt(String key, NBTTagCompound compound) { + if (!compound.hasKey(key)) { + return null; + } + String string = compound.getString(key); + try { + return IChatBaseComponent.ChatSerializer.jsonToComponent(string); + } catch (com.google.gson.JsonParseException e) { + org.bukkit.Bukkit.getLogger().warning("Unable to parse " + key + " from " + compound +": " + e.getMessage()); + } + + return null; + } } diff --git a/src/main/java/net/minecraft/server/TileEntityBanner.java b/src/main/java/net/minecraft/server/TileEntityBanner.java index 1f26d8a02..12264c4d0 100644 --- a/src/main/java/net/minecraft/server/TileEntityBanner.java +++ b/src/main/java/net/minecraft/server/TileEntityBanner.java @@ -57,7 +57,7 @@ public class TileEntityBanner extends TileEntity implements INamableTileEntity { public void load(NBTTagCompound nbttagcompound) { super.load(nbttagcompound); if (nbttagcompound.hasKeyOfType("CustomName", 8)) { - this.a = IChatBaseComponent.ChatSerializer.a(nbttagcompound.getString("CustomName")); + this.a = MCUtil.getBaseComponentFromNbt("CustomName", nbttagcompound); // Paper - Catch ParseException } if (this.hasWorld()) { diff --git a/src/main/java/net/minecraft/server/TileEntityContainer.java b/src/main/java/net/minecraft/server/TileEntityContainer.java index 473ec2cbd..ab6b86e4e 100644 --- a/src/main/java/net/minecraft/server/TileEntityContainer.java +++ b/src/main/java/net/minecraft/server/TileEntityContainer.java @@ -17,7 +17,7 @@ public abstract class TileEntityContainer extends TileEntity implements IInvento super.load(nbttagcompound); this.chestLock = ChestLock.b(nbttagcompound); if (nbttagcompound.hasKeyOfType("CustomName", 8)) { - this.customName = IChatBaseComponent.ChatSerializer.a(nbttagcompound.getString("CustomName")); + this.customName = MCUtil.getBaseComponentFromNbt("CustomName", nbttagcompound); // Paper - Catch ParseException } } -- 2.22.0