From 11300254f0b4bc760e8e8e8a3c09f0141f666917 Mon Sep 17 00:00:00 2001 From: bundabrg Date: Fri, 31 Jul 2020 04:10:43 +0800 Subject: [PATCH] Fix Anvil renames by trying a component first then fallback to plain text (#1052) Closes #1039 --- .../inventory/AnvilInventoryTranslator.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/AnvilInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/AnvilInventoryTranslator.java index 07aaaa19d..0a1d2f272 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/AnvilInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/AnvilInventoryTranslator.java @@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators.inventory; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientRenameItemPacket; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.google.gson.JsonSyntaxException; import com.nukkitx.nbt.NbtMap; import com.nukkitx.protocol.bedrock.data.inventory.*; import net.kyori.adventure.text.Component; @@ -108,8 +109,12 @@ public class AnvilInventoryTranslator extends BlockInventoryTranslator { NbtMap tag = itemName.getTag(); if (tag != null) { String name = tag.getCompound("display").getString("Name"); - Component component = GsonComponentSerializer.gson().deserialize(name); - rename = LegacyComponentSerializer.legacySection().serialize(component); + try { + Component component = GsonComponentSerializer.gson().deserialize(name); + rename = LegacyComponentSerializer.legacySection().serialize(component); + } catch (JsonSyntaxException e) { + rename = name; + } } else { rename = ""; } @@ -141,8 +146,12 @@ public class AnvilInventoryTranslator extends BlockInventoryTranslator { CompoundTag displayTag = tag.get("display"); if (displayTag != null && displayTag.contains("Name")) { String itemName = displayTag.get("Name").getValue().toString(); - Component component = GsonComponentSerializer.gson().deserialize(itemName); - rename = LegacyComponentSerializer.legacySection().serialize(component); + try { + Component component = GsonComponentSerializer.gson().deserialize(itemName); + rename = LegacyComponentSerializer.legacySection().serialize(component); + } catch (JsonSyntaxException e) { + rename = itemName; + } } else { rename = ""; }