From 6b616d8b07edb5818f6f4fb0ffc601f5b607a10c Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 9 May 2024 19:44:03 +0200 Subject: [PATCH] Fixup banner handling --- .../Protocol1_20_3To1_20_5.java | 2 + .../BlockItemPacketRewriter1_20_5.java | 73 +++++++++++++++++-- .../rewriter/EntityPacketRewriter1_20_5.java | 9 ++- gradle/libs.versions.toml | 2 +- 4 files changed, 75 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java index 5d0a16b7..a5d82bb5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java @@ -54,6 +54,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.Clientb import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ServerboundConfigurationPackets1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ServerboundPacket1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ServerboundPackets1_20_5; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.storage.BannerPatternStorage; import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -218,6 +219,7 @@ public final class Protocol1_20_3To1_20_5 extends BackwardsProtocol updateBlockEntityTag(blockEntity)); - blockRewriter.registerBlockEntityData(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA, this::updateBlockEntityTag); + blockRewriter.registerChunkData1_19(ClientboundPackets1_20_5.CHUNK_DATA, ChunkType1_20_2::new, (user, blockEntity) -> updateBlockEntityTag(user, blockEntity.tag())); + protocol.registerClientbound(ClientboundPackets1_20_5.BLOCK_ENTITY_DATA, wrapper -> { + wrapper.passthrough(Type.POSITION1_14); // Position + wrapper.passthrough(Type.VAR_INT); // Block entity type + final CompoundTag tag = wrapper.passthrough(Type.COMPOUND_TAG); + updateBlockEntityTag(wrapper.user(), tag); + }); registerSetCooldown(ClientboundPackets1_20_5.COOLDOWN); registerWindowItems1_17_1(ClientboundPackets1_20_5.WINDOW_ITEMS); @@ -182,8 +187,7 @@ public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItem }); } - private void updateBlockEntityTag(final BlockEntity blockEntity) { - final CompoundTag tag = blockEntity.tag(); + private void updateBlockEntityTag(final UserConnection connection, final CompoundTag tag) { if (tag == null) { return; } @@ -194,6 +198,26 @@ public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItem } else if (profileTag instanceof CompoundTag) { updateProfileTag(tag, (CompoundTag) profileTag); } + + final ListTag patternsTag = tag.getListTag("patterns", CompoundTag.class); + if (patternsTag != null) { + for (final CompoundTag patternTag : patternsTag) { + final String pattern = patternTag.getString("pattern", ""); + final String color = patternTag.getString("color"); + final String compactIdentifier = BannerPatterns1_20_5.fullIdToCompact(Key.stripMinecraftNamespace(pattern)); + if (compactIdentifier == null || color == null) { + continue; + } + + patternTag.remove("pattern"); + patternTag.remove("color"); + patternTag.putString("Pattern", compactIdentifier); + patternTag.putInt("Color", colorId(color)); + } + + tag.remove("patterns"); + tag.put("Patterns", patternsTag); + } } private void updateProfileTag(final CompoundTag tag, final CompoundTag profileTag) { @@ -269,12 +293,49 @@ public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItem } } + private static int colorId(final String color) { + switch (color) { + case "orange": + return 1; + case "magenta": + return 2; + case "light_blue": + return 3; + case "yellow": + return 4; + case "lime": + return 5; + case "pink": + return 6; + case "gray": + return 7; + case "light_gray": + return 8; + case "cyan": + return 9; + case "purple": + return 10; + case "blue": + return 11; + case "brown": + return 12; + case "green": + return 13; + case "red": + return 14; + case "black": + return 15; + default: + return 0; + } + } + @Override public @Nullable Item handleItemToClient(final UserConnection connection, @Nullable final Item item) { if (item == null) return null; super.handleItemToClient(connection, item); - return vvProtocol.getItemRewriter().toOldItem(item, DATA_CONVERTER); + return vvProtocol.getItemRewriter().toOldItem(connection, item, DATA_CONVERTER); } @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java index 3c2c14dc..5e26ab85 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java @@ -47,6 +47,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.Clientb import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPackets1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.storage.BannerPatternStorage; import com.viaversion.viaversion.util.Key; +import com.viaversion.viaversion.util.KeyMappings; import com.viaversion.viaversion.util.MathUtil; import java.util.HashMap; import java.util.Map; @@ -93,13 +94,13 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter