From 993608b4b4f39b84a8b3effe14092189c525b072 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 6 Jun 2024 10:56:43 +0200 Subject: [PATCH] Also apply MC-68487 workaround to items --- .../BackwardsBlockEntityProvider.java | 17 ++--- .../BlockItemPacketRewriter1_16_2.java | 66 +++++++++++-------- .../rewriter/BlockItemPacketRewriter1_16.java | 6 +- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/provider/BackwardsBlockEntityProvider.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/provider/BackwardsBlockEntityProvider.java index fcfe1a3c..0c16b8d8 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/provider/BackwardsBlockEntityProvider.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/provider/BackwardsBlockEntityProvider.java @@ -30,6 +30,7 @@ import com.viaversion.viaversion.api.minecraft.BlockPosition; import com.viaversion.viaversion.api.platform.providers.Provider; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.StringTag; +import com.viaversion.viaversion.util.Key; import java.util.HashMap; import java.util.Map; @@ -37,12 +38,12 @@ public class BackwardsBlockEntityProvider implements Provider { private final Map handlers = new HashMap<>(); public BackwardsBlockEntityProvider() { - handlers.put("minecraft:flower_pot", new FlowerPotHandler()); // TODO requires special treatment, manually send - handlers.put("minecraft:bed", new BedHandler()); - handlers.put("minecraft:banner", new BannerHandler()); - handlers.put("minecraft:skull", new SkullHandler()); - handlers.put("minecraft:mob_spawner", new SpawnerHandler()); - handlers.put("minecraft:piston", new PistonHandler()); + handlers.put("flower_pot", new FlowerPotHandler()); // TODO requires special treatment, manually send + handlers.put("bed", new BedHandler()); + handlers.put("banner", new BannerHandler()); + handlers.put("skull", new SkullHandler()); + handlers.put("mob_spawner", new SpawnerHandler()); + handlers.put("piston", new PistonHandler()); } /** @@ -52,7 +53,7 @@ public class BackwardsBlockEntityProvider implements Provider { * @return true if present */ public boolean isHandled(String key) { - return handlers.containsKey(key); + return handlers.containsKey(Key.stripMinecraftNamespace(key)); } /** @@ -69,7 +70,7 @@ public class BackwardsBlockEntityProvider implements Provider { } String id = idTag.getValue(); - BackwardsBlockEntityHandler handler = handlers.get(id); + BackwardsBlockEntityHandler handler = handlers.get(Key.stripMinecraftNamespace(id)); if (handler == null) { return tag; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/rewriter/BlockItemPacketRewriter1_16_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/rewriter/BlockItemPacketRewriter1_16_2.java index f9836ec6..bd371d0f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/rewriter/BlockItemPacketRewriter1_16_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/rewriter/BlockItemPacketRewriter1_16_2.java @@ -17,23 +17,26 @@ */ package com.viaversion.viabackwards.protocol.v1_16_2to1_16_1.rewriter; +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.nbt.tag.IntArrayTag; +import com.viaversion.nbt.tag.ListTag; import com.viaversion.viabackwards.api.rewriters.BackwardsItemRewriter; import com.viaversion.viabackwards.protocol.v1_16_2to1_16_1.Protocol1_16_2To1_16_1; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_8; +import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_16; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_16_2; import com.viaversion.viaversion.protocols.v1_15_2to1_16.packet.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.v1_15_2to1_16.packet.ServerboundPackets1_16; -import com.viaversion.nbt.tag.CompoundTag; -import com.viaversion.nbt.tag.IntArrayTag; -import com.viaversion.nbt.tag.ListTag; -import com.viaversion.nbt.tag.StringTag; import com.viaversion.viaversion.protocols.v1_16_1to1_16_2.packet.ClientboundPackets1_16_2; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; +import com.viaversion.viaversion.util.Key; +import org.checkerframework.checker.nullness.qual.Nullable; public class BlockItemPacketRewriter1_16_2 extends BackwardsItemRewriter { @@ -118,29 +121,40 @@ public class BlockItemPacketRewriter1_16_2 extends BackwardsItemRewriter handleItemToServer(wrapper.user(), wrapper.passthrough(Types.ITEM1_13_2))); } + @Override + public @Nullable Item handleItemToClient(final UserConnection connection, @Nullable final Item item) { + if (item != null && item.tag() != null) { + addValueHashAsId(item.tag()); + } + return super.handleItemToClient(connection, item); + } + private void handleBlockEntity(CompoundTag tag) { - StringTag idTag = tag.getStringTag("id"); - if (idTag == null) return; - if (idTag.getValue().equals("minecraft:skull")) { - // Workaround an old client bug: MC-68487 - CompoundTag skullOwnerTag = tag.getCompoundTag("SkullOwner"); - if (skullOwnerTag == null) return; - - if (!skullOwnerTag.contains("Id")) return; - - CompoundTag properties = skullOwnerTag.getCompoundTag("Properties"); - if (properties == null) return; - - ListTag textures = properties.getListTag("textures", CompoundTag.class); - if (textures == null) return; - - CompoundTag first = !textures.isEmpty() ? textures.get(0) : null; - if (first == null) return; - - // Make the client cache the skinprofile over this uuid - int hashCode = first.get("Value").getValue().hashCode(); - int[] uuidIntArray = {hashCode, 0, 0, 0}; //TODO split texture in 4 for a lower collision chance - skullOwnerTag.put("Id", new IntArrayTag(uuidIntArray)); + String id = tag.getString("id"); + if (id != null && Key.stripMinecraftNamespace(id).equals("skull")) { + addValueHashAsId(tag); } } + + private void addValueHashAsId(CompoundTag tag) { + // Workaround an old client bug: MC-68487 + CompoundTag skullOwnerTag = tag.getCompoundTag("SkullOwner"); + if (skullOwnerTag == null) return; + + if (!skullOwnerTag.contains("Id")) return; + + CompoundTag properties = skullOwnerTag.getCompoundTag("Properties"); + if (properties == null) return; + + ListTag textures = properties.getListTag("textures", CompoundTag.class); + if (textures == null) return; + + CompoundTag first = !textures.isEmpty() ? textures.get(0) : null; + if (first == null) return; + + // Make the client cache the skinprofile over this uuid + int hashCode = first.get("Value").getValue().hashCode(); + int[] uuidIntArray = {hashCode, 0, 0, 0}; + skullOwnerTag.put("Id", new IntArrayTag(uuidIntArray)); + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/BlockItemPacketRewriter1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/BlockItemPacketRewriter1_16.java index a3d8dea8..9eae5d48 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/BlockItemPacketRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/BlockItemPacketRewriter1_16.java @@ -226,10 +226,10 @@ public class BlockItemPacketRewriter1_16 extends BackwardsItemRewriter