From 121f107ff3f8d2ec5c7c94a60ff299cdee4ba040 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 9 Mar 2024 11:26:52 +0100 Subject: [PATCH] More handled structures --- .../data/Attributes1_20_3.java | 4 +- .../BlockItemPacketRewriter1_20_5.java | 132 ++++++++++++++++-- .../rewriter/EntityPacketRewriter1_20_5.java | 2 +- 3 files changed, 126 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Attributes1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Attributes1_20_3.java index 7e20b3516..d80fd99a0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Attributes1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/data/Attributes1_20_3.java @@ -39,11 +39,11 @@ public final class Attributes1_20_3 { "zombie.spawn_reinforcements" ); - public static @Nullable String attribute(final int id) { + public static @Nullable String idToKey(final int id) { return ATTRIBUTES.idToKey(id); } - public static int id(final String attribute) { + public static int keyToId(final String attribute) { return ATTRIBUTES.keyToId(attribute); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java index 6ca32476a..9ea0e9cd8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java @@ -37,6 +37,8 @@ import com.viaversion.viaversion.api.minecraft.item.StructuredItem; import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrim; import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrimMaterial; import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrimPattern; +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifier; +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifiers; import com.viaversion.viaversion.api.minecraft.item.data.BlockStateProperties; import com.viaversion.viaversion.api.minecraft.item.data.DyedColor; import com.viaversion.viaversion.api.minecraft.item.data.Enchantments; @@ -45,6 +47,7 @@ import com.viaversion.viaversion.api.minecraft.item.data.FilterableString; import com.viaversion.viaversion.api.minecraft.item.data.FireworkExplosion; import com.viaversion.viaversion.api.minecraft.item.data.Fireworks; import com.viaversion.viaversion.api.minecraft.item.data.LodestoneTracker; +import com.viaversion.viaversion.api.minecraft.item.data.ModifierData; import com.viaversion.viaversion.api.minecraft.item.data.PotionContents; import com.viaversion.viaversion.api.minecraft.item.data.PotionEffect; import com.viaversion.viaversion.api.minecraft.item.data.PotionEffectData; @@ -60,6 +63,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.Clientb import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.RecipeRewriter1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Attributes1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Enchantments1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Instruments1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.MapDecorations1_20_3; @@ -98,14 +102,14 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter updateBlockEntityTag(blockEntity.tag())); + blockRewriter.registerChunkData1_19(ClientboundPackets1_20_3.CHUNK_DATA, ChunkType1_20_2::new, blockEntity -> updateBlockEntityTag(null, blockEntity.tag())); protocol.registerClientbound(ClientboundPackets1_20_3.BLOCK_ENTITY_DATA, wrapper -> { wrapper.passthrough(Type.POSITION1_14); // Position wrapper.passthrough(Type.VAR_INT); // Block entity type CompoundTag tag = wrapper.read(Type.COMPOUND_TAG); if (tag != null) { - updateBlockEntityTag(tag); + updateBlockEntityTag(null, tag); } else { // No longer nullable tag = new CompoundTag(); @@ -289,18 +293,19 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter attributeModifiersTag = tag.getListTag("AttributeModifiers", CompoundTag.class); + if (attributeModifiersTag != null) { + updateAttributes(data, attributeModifiersTag, (hideFlagsValue & 0x02) == 0); + } + final CompoundTag fireworksTag = tag.getCompoundTag("Fireworks"); if (fireworksTag != null) { final ListTag explosionsTag = fireworksTag.getListTag("Explosions", CompoundTag.class); @@ -383,10 +393,8 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter attributeModifiersTag, final boolean showInTooltip) { + final AttributeModifier[] modifiers = attributeModifiersTag.stream().map(modifierTag -> { + final StringTag attributeNameTag = modifierTag.getStringTag("AttributeName"); + final StringTag nameTag = modifierTag.getStringTag("Name"); + final NumberTag operationTag = modifierTag.getNumberTag("Operation"); + final NumberTag amountTag = modifierTag.getNumberTag("Amount"); + final IntArrayTag uuidTag = modifierTag.getIntArrayTag("UUID"); + final NumberTag slotTag = modifierTag.getNumberTag("Slot"); + if (nameTag == null || attributeNameTag == null || operationTag == null || amountTag == null || uuidTag == null || slotTag == null) { + return null; + } + + final int operationId = operationTag.asInt(); + if (operationId < 0 || operationId > 2) { + return null; + } + + final int attributeId = Attributes1_20_3.keyToId(attributeNameTag.getValue()); + if (attributeId == -1) { + return null; + } + + return new AttributeModifier( + attributeId, + new ModifierData( + UUIDUtil.fromIntArray(uuidTag.getValue()), + nameTag.getValue(), + amountTag.asDouble(), + operationId + ), + slotTag.asInt() + ); + }).filter(Objects::nonNull).toArray(AttributeModifier[]::new); + data.set(StructuredDataKey.ATTRIBUTE_MODIFIERS, new AttributeModifiers(modifiers, showInTooltip)); + } + private void updatePotionTags(final StructuredDataContainer data, final CompoundTag tag) { final StringTag potionTag = tag.getStringTag("Potion"); Integer potionId = null; @@ -770,10 +814,80 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter propertiesListTag = new ListTag<>(CompoundTag.class); + for (final Map.Entry entry : ((CompoundTag) propertiesTag).entrySet()) { + if (!(entry.getValue() instanceof ListTag)) { + continue; + } + + final ListTag value = (ListTag) entry.getValue(); + for (final Tag propertyTag : value) { + if (!(propertyTag instanceof CompoundTag)) { + continue; + } + + final CompoundTag updatedPropertyTag = new CompoundTag(); + final CompoundTag propertyCompoundTag = (CompoundTag) propertyTag; + final StringTag valueTag = propertyCompoundTag.getStringTag("Value"); + final StringTag signatureTag = propertyCompoundTag.getStringTag("Signature"); + updatedPropertyTag.putString("name", entry.getKey()); + updatedPropertyTag.putString("value", valueTag != null ? valueTag.getValue() : ""); + if (signatureTag != null) { + updatedPropertyTag.putString("signature", signatureTag.getValue()); + } + propertiesListTag.add(updatedPropertyTag); + } + } + + profileTag.put("properties", propertiesListTag); + } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java index e79329322..5f52d6d52 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java @@ -157,7 +157,7 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter