Catch component parsing exceptions separately instead of failing the entire item

This commit is contained in:
Nassim Jahnke 2024-04-24 15:47:47 +02:00
parent d2ca6a82be
commit 8df0c0ae2e
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
2 changed files with 26 additions and 12 deletions

View File

@ -1089,11 +1089,23 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
if (filteredPagesTag != null) {
final StringTag filteredPage = filteredPagesTag.getStringTag(String.valueOf(i));
if (filteredPage != null) {
filtered = jsonToTag(connection, filteredPage);
try {
filtered = jsonToTag(connection, filteredPage);
} catch (final Exception e) {
// A 1.20.4 client would display the broken json raw, but a 1.20.5 client would die
continue;
}
}
}
final Tag parsedPage = jsonToTag(connection, page);
final Tag parsedPage;
try {
parsedPage = jsonToTag(connection, page);
} catch (final Exception e) {
// Same as above
continue;
}
pages.add(new FilterableComponent(parsedPage, filtered));
}
@ -1291,13 +1303,22 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final StringTag nameTag = displayTag.getStringTag("Name");
if (nameTag != null) {
data.set(StructuredDataKey.CUSTOM_NAME, jsonToTag(connection, nameTag));
try {
final Tag convertedName = jsonToTag(connection, nameTag);
data.set(StructuredDataKey.CUSTOM_NAME, convertedName);
} catch (final Exception ignored) {
// No display name if it fails to parse
}
}
final ListTag<StringTag> loreTag = displayTag.getListTag("Lore", StringTag.class);
if (loreTag != null) {
// Apply limit as per new network codec. Some servers send these lores to do trickery with shaders
data.set(StructuredDataKey.LORE, loreTag.stream().limit(256).map(t -> jsonToTag(connection, t)).toArray(Tag[]::new));
try {
data.set(StructuredDataKey.LORE, loreTag.stream().limit(256).map(t -> jsonToTag(connection, t)).toArray(Tag[]::new));
} catch (final Exception ignored) {
// No lore if any one of them fail to parse
}
}
final NumberTag colorTag = displayTag.getNumberTag("color");

View File

@ -110,14 +110,7 @@ public final class ComponentUtil {
if (json == null) {
return null;
}
try {
final ATextComponent component = from.jsonSerializer.deserialize(json);
return to.toTag(component);
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + json, e);
return new StringTag("<error>");
}
return to.toTag(from.jsonSerializer.deserialize(json));
}
public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) {