Fix invalid book view not visible in 1.20.3->.5 (#4139)

This commit is contained in:
EnZaXD 2024-09-07 10:45:10 +02:00 committed by GitHub
parent 95581a6fa5
commit ebc0e61be8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1163,47 +1163,63 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
} }
private void updateWrittenBookPages(final UserConnection connection, final StructuredDataContainer data, final CompoundTag tag) { private void updateWrittenBookPages(final UserConnection connection, final StructuredDataContainer data, final CompoundTag tag) {
final String title = tag.getString("title");
final String author = tag.getString("author");
final ListTag<StringTag> pagesTag = tag.getListTag("pages", StringTag.class); final ListTag<StringTag> pagesTag = tag.getListTag("pages", StringTag.class);
final CompoundTag filteredPagesTag = tag.getCompoundTag("filtered_pages");
if (pagesTag == null) { boolean valid = author != null && title != null && title.length() <= 32 && pagesTag != null;
return; if (valid) {
for (final StringTag page : pagesTag) {
if (page.getValue().length() > Short.MAX_VALUE) {
valid = false;
break;
}
}
} }
final List<FilterableComponent> pages = new ArrayList<>(); final List<FilterableComponent> pages = new ArrayList<>();
for (int i = 0; i < pagesTag.size(); i++) { if (valid) {
final StringTag page = pagesTag.get(i); final CompoundTag filteredPagesTag = tag.getCompoundTag("filtered_pages");
Tag filtered = null;
if (filteredPagesTag != null) { for (int i = 0; i < pagesTag.size(); i++) {
final StringTag filteredPage = filteredPagesTag.getStringTag(String.valueOf(i)); final StringTag page = pagesTag.get(i);
if (filteredPage != null) { Tag filtered = null;
try { if (filteredPagesTag != null) {
filtered = jsonToTag(connection, filteredPage); final StringTag filteredPage = filteredPagesTag.getStringTag(String.valueOf(i));
} catch (final Exception e) { if (filteredPage != null) {
// A 1.20.4 client would display the broken json raw, but a 1.20.5 client would die try {
continue; 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; final Tag parsedPage;
try { try {
parsedPage = jsonToTag(connection, page); parsedPage = jsonToTag(connection, page);
} catch (final Exception e) { } catch (final Exception e) {
// Same as above // Same as above
continue; continue;
} }
pages.add(new FilterableComponent(parsedPage, filtered)); pages.add(new FilterableComponent(parsedPage, filtered));
}
} else {
final CompoundTag invalidPage = new CompoundTag();
invalidPage.putString("text", "* Invalid book tag *");
invalidPage.putString("color", "#AA0000"); // dark red
pages.add(new FilterableComponent(invalidPage, null));
} }
final String title = tag.getString("title", "");
final String filteredTitle = tag.getString("filtered_title"); // Nullable final String filteredTitle = tag.getString("filtered_title"); // Nullable
final String author = tag.getString("author", "");
final int generation = tag.getInt("generation"); final int generation = tag.getInt("generation");
final boolean resolved = tag.getBoolean("resolved"); final boolean resolved = tag.getBoolean("resolved");
final WrittenBook writtenBook = new WrittenBook( final WrittenBook writtenBook = new WrittenBook(
new FilterableString(limit(title, 32), limit(filteredTitle, 32)), new FilterableString(limit(title == null ? "" : title, 32), limit(filteredTitle, 32)),
author, author == null ? "" : author,
clamp(generation, 0, 3), clamp(generation, 0, 3),
pages.toArray(new FilterableComponent[0]), pages.toArray(new FilterableComponent[0]),
resolved resolved