Handle WRITTEN_BOOK_CONTENT component in rewriters as well (#4175)

Closes https://github.com/ViaVersion/ViaVersion/issues/4144
This commit is contained in:
EnZaXD 2024-10-10 22:42:21 +02:00 committed by GitHub
parent 6ad9a71907
commit 1a9cda0579
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 1 deletions

View File

@ -39,8 +39,8 @@ import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil; import com.viaversion.viaversion.util.TagUtil;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.BitSet; import java.util.BitSet;
import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
* Handles json and tag components, containing methods to override certain parts of the handling. * Handles json and tag components, containing methods to override certain parts of the handling.
@ -360,6 +360,8 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
handleShowItem(connection, contentsTag, componentsTag); handleShowItem(connection, contentsTag, componentsTag);
if (componentsTag != null) { if (componentsTag != null) {
handleContainerContents(connection, componentsTag); handleContainerContents(connection, componentsTag);
handleWrittenBookContents(connection, componentsTag);
handleItemArrayContents(connection, componentsTag, "bundle_contents"); handleItemArrayContents(connection, componentsTag, "bundle_contents");
handleItemArrayContents(connection, componentsTag, "charged_projectiles"); handleItemArrayContents(connection, componentsTag, "charged_projectiles");
} }
@ -386,6 +388,40 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
} }
} }
protected void handleWrittenBookContents(final UserConnection connection, final CompoundTag tag) {
final CompoundTag book = TagUtil.getNamespacedCompoundTag(tag, "minecraft:written_book_content");
if (book == null) {
return;
}
final ListTag<CompoundTag> pagesTag = book.getListTag("pages", CompoundTag.class);
if (pagesTag == null) {
return;
}
for (final CompoundTag compoundTag : pagesTag) {
final StringTag raw = compoundTag.getStringTag("raw");
processJsonString(connection, raw);
final StringTag filtered = compoundTag.getStringTag("filtered");
processJsonString(connection, filtered);
}
}
private void processJsonString(final UserConnection connection, final StringTag tag) {
if (tag == null) {
return;
}
final var input = inputSerializerVersion();
final var output = outputSerializerVersion();
final Tag asTag = input.toTag(input.toComponent(tag.getValue()));
processTag(connection, asTag);
tag.setValue(output.toString(output.toComponent(asTag)));
}
protected void handleItemArrayContents(final UserConnection connection, final CompoundTag tag, final String key) { protected void handleItemArrayContents(final UserConnection connection, final CompoundTag tag, final String key) {
final ListTag<CompoundTag> container = TagUtil.getNamespacedCompoundTagList(tag, key); final ListTag<CompoundTag> container = TagUtil.getNamespacedCompoundTagList(tag, key);
if (container == null) { if (container == null) {

View File

@ -28,6 +28,8 @@ import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.item.data.FilterableComponent;
import com.viaversion.viaversion.api.minecraft.item.data.WrittenBook;
import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
@ -138,6 +140,16 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
protocol.getComponentRewriter().processTag(connection, tag); protocol.getComponentRewriter().processTag(connection, tag);
} }
} }
final WrittenBook book = container.get(StructuredDataKey.WRITTEN_BOOK_CONTENT);
if (book != null) {
for (final FilterableComponent page : book.pages()) {
protocol.getComponentRewriter().processTag(connection, page.raw());
if (page.isFiltered()) {
protocol.getComponentRewriter().processTag(connection, page.filtered());
}
}
}
} }
// Look for item types // Look for item types