diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java index bbb3479e..c799b270 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java @@ -32,8 +32,10 @@ import com.viaversion.viaversion.api.minecraft.HolderSet; import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.minecraft.item.data.CustomModelData1_21_4; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.rewriter.StructuredItemRewriter; import java.util.ArrayList; @@ -88,8 +90,19 @@ public class BackwardsStructuredItemRewriter { public static final BackwardsMappingData MAPPINGS = new BackwardsMappingData("1.21.4", "1.21.2", Protocol1_21_2To1_21_4.class); - private final EntityPacketRewriter1_99 entityRewriter = new EntityPacketRewriter1_99(this); + private final EntityPacketRewriter1_21_4 entityRewriter = new EntityPacketRewriter1_21_4(this); private final BlockItemPacketRewriter1_21_4 itemRewriter = new BlockItemPacketRewriter1_21_4(this); private final ParticleRewriter particleRewriter = new ParticleRewriter1_21_4(this); private final TranslatableRewriter translatableRewriter = new ComponentRewriter1_21_4(this); @@ -148,7 +148,6 @@ public final class Protocol1_21_4To1_21_2 extends BackwardsProtocol { public BlockItemPacketRewriter1_21_4(final Protocol1_21_4To1_21_2 protocol) { @@ -72,4 +85,83 @@ public final class BlockItemPacketRewriter1_21_4 extends BackwardsStructuredItem recipeRewriter.registerRecipeBookAdd(ClientboundPackets1_21_2.RECIPE_BOOK_ADD); recipeRewriter.registerPlaceGhostRecipe(ClientboundPackets1_21_2.PLACE_GHOST_RECIPE); } + + + @Override + public Item handleItemToClient(final UserConnection connection, final Item item) { + super.handleItemToClient(connection, item); + + final StructuredDataContainer dataContainer = item.dataContainer(); + final CustomModelData1_21_4 modelData = dataContainer.get(StructuredDataKey.CUSTOM_MODEL_DATA1_21_4); + if (modelData != null) { + saveTag(createCustomTag(item), customModelDataToTag(modelData), "custom_model_data"); + } + + updateItemData(item); + return item; + } + + @Override + public Item handleItemToServer(final UserConnection connection, final Item item) { + super.handleItemToServer(connection, item); + + final StructuredDataContainer dataContainer = item.dataContainer(); + final CompoundTag customData = dataContainer.get(StructuredDataKey.CUSTOM_DATA); + if (customData != null) { + if (customData.remove(nbtTagName("custom_model_data")) instanceof final CompoundTag customModelData) { + dataContainer.set(StructuredDataKey.CUSTOM_MODEL_DATA1_21_4, customModelDataFromTag(customModelData)); + removeCustomTag(dataContainer, customData); + } + } + + downgradeItemData(item); + return item; + } + + private CustomModelData1_21_4 customModelDataFromTag(final CompoundTag tag) { + final IntArrayTag floatsTag = tag.getIntArrayTag("floats"); + final float[] floats = new float[floatsTag.getValue().length]; + for (int i = 0; i < floats.length; i++) { + floats[i] = Float.intBitsToFloat(floatsTag.get(i)); + } + + final ByteArrayTag booleansTag = tag.getByteArrayTag("booleans"); + final boolean[] booleans = new boolean[booleansTag.getValue().length]; + for (int i = 0; i < booleans.length; i++) { + booleans[i] = booleansTag.get(i) != 0; + } + + final ListTag stringsTag = tag.getListTag("strings", StringTag.class); + final String[] strings = new String[stringsTag.size()]; + for (int i = 0; i < strings.length; i++) { + strings[i] = stringsTag.get(i).getValue(); + } + + final IntArrayTag colorsTag = tag.getIntArrayTag("colors"); + return new CustomModelData1_21_4(floats, booleans, strings, colorsTag.getValue()); + } + + private CompoundTag customModelDataToTag(final CustomModelData1_21_4 customModelData) { + final CompoundTag tag = new CompoundTag(); + final int[] floats = new int[customModelData.floats().length]; + for (int i = 0; i < floats.length; i++) { + floats[i] = Float.floatToIntBits(customModelData.floats()[i]); + } + tag.put("floats", new IntArrayTag(floats)); + + final byte[] booleans = new byte[customModelData.booleans().length]; + for (int i = 0; i < booleans.length; i++) { + booleans[i] = (byte) (customModelData.booleans()[i] ? 1 : 0); + } + tag.put("booleans", new ByteArrayTag(booleans)); + + final ListTag strings = new ListTag<>(StringTag.class); + for (final String string : customModelData.strings()) { + strings.add(new StringTag(string)); + } + tag.put("strings", strings); + + tag.put("colors", new IntArrayTag(customModelData.colors())); + return tag; + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_4to1_21_2/rewriter/ComponentRewriter1_21_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_4to1_21_2/rewriter/ComponentRewriter1_21_4.java index 7df3333b..2b4f6523 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_4to1_21_2/rewriter/ComponentRewriter1_21_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_4to1_21_2/rewriter/ComponentRewriter1_21_4.java @@ -23,6 +23,7 @@ import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPacket1_21_2; import com.viaversion.viaversion.util.SerializerVersion; +import com.viaversion.viaversion.util.TagUtil; public final class ComponentRewriter1_21_4 extends TranslatableRewriter { @@ -37,8 +38,7 @@ public final class ComponentRewriter1_21_4 extends TranslatableRewriter { +public final class EntityPacketRewriter1_21_4 extends EntityRewriter { - public EntityPacketRewriter1_99(final Protocol1_21_4To1_21_2 protocol) { + public EntityPacketRewriter1_21_4(final Protocol1_21_4To1_21_2 protocol) { super(protocol, Types1_21_4.ENTITY_DATA_TYPES.optionalComponentType, Types1_21_4.ENTITY_DATA_TYPES.booleanType); } @@ -49,7 +52,8 @@ public final class EntityPacketRewriter1_99 extends EntityRewriter { final int dimensionId = wrapper.passthrough(Types.VAR_INT); final String world = wrapper.passthrough(Types.STRING); trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); + + final PacketWrapper playerLoadedPacket = wrapper.create(ServerboundPackets1_21_4.PLAYER_LOADED); + playerLoadedPacket.scheduleSendToServer(Protocol1_21_4To1_21_2.class); }); + + protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_VEHICLE, wrapper -> { + wrapper.passthrough(Types.DOUBLE); // X + wrapper.passthrough(Types.DOUBLE); // Y + wrapper.passthrough(Types.DOUBLE); // Z + wrapper.passthrough(Types.FLOAT); // Yaw + wrapper.passthrough(Types.FLOAT); // Pitch + wrapper.write(Types.BOOLEAN, true); // On ground // TODO ... + }); + } + + private float itemModelIndex(final String trim) { + return switch (Key.stripNamespace(trim)) { + case "amethyst" -> 1.0F; + case "copper" -> 0.5F; + case "diamond" -> 0.8F; + case "emerald" -> 0.7F; + case "gold" -> 0.6F; + case "iron" -> 0.2F; + case "lapis" -> 0.9F; + case "netherite" -> 0.3F; + case "quartz" -> 0.1F; + case "redstone" -> 0.4F; + default -> 1.0f; + }; } @Override