From 10f2cad8fdf3f9cbcc1d8c3e80e49860565107ea Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:21:00 +0100 Subject: [PATCH] Change the consume animation of swords to block (#4223) --- .../rewriter/BlockItemPacketRewriter1_21_2.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java index c029a0946..ae70fccc8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java @@ -45,6 +45,7 @@ import com.viaversion.viaversion.api.minecraft.item.data.Instrument1_20_5; import com.viaversion.viaversion.api.minecraft.item.data.Instrument1_21_2; import com.viaversion.viaversion.api.minecraft.item.data.PotionEffect; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.version.Types1_21; @@ -417,6 +418,9 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< super.handleItemToClient(connection, item); updateItemData(item); + // Add data components to fix issues in older protocols + appendItemDataFixComponents(connection, item); + // Item name is now overridden by custom implemented display names (compass, player head, potion, shield, tipped arrow) final int identifier = item.identifier(); if (identifier == 952 || identifier == 1147 || identifier == 1039 || identifier == 1203 || identifier == 1200 || identifier == 1204 || identifier == 1202) { @@ -469,6 +473,17 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< wrapper.write(Types.UNSIGNED_BYTE, (short) containerId); } + private void appendItemDataFixComponents(final UserConnection connection, final Item item) { + final ProtocolVersion serverVersion = connection.getProtocolInfo().serverProtocolVersion(); + if (serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_8) && item.dataContainer().hasValue(StructuredDataKey.CONSUMABLE1_21_2)) { + if (item.identifier() == 840 || item.identifier() == 845 || item.identifier() == 850 || item.identifier() == 855 || item.identifier() == 860) { // swords + // Change the consume animation of swords to block + final Consumable1_21_2 consumable = item.dataContainer().get(StructuredDataKey.CONSUMABLE1_21_2); + item.dataContainer().set(StructuredDataKey.CONSUMABLE1_21_2, new Consumable1_21_2(consumable.consumeSeconds(), 3, consumable.sound(), consumable.hasConsumeParticles(), consumable.consumeEffects())); + } + } + } + public static void updateItemData(final Item item) { final StructuredDataContainer dataContainer = item.dataContainer(); dataContainer.replace(StructuredDataKey.INSTRUMENT1_20_5, StructuredDataKey.INSTRUMENT1_21_2, instrument -> { @@ -486,6 +501,7 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< final Consumable1_21_2.ApplyStatusEffects applyStatusEffects = new Consumable1_21_2.ApplyStatusEffects(new PotionEffect[]{effect.effect()}, effect.probability()); consumeEffects[i] = new Consumable1_21_2.ConsumeEffect<>(0 /* add status effect */, Consumable1_21_2.ApplyStatusEffects.TYPE, applyStatusEffects); } + dataContainer.set(StructuredDataKey.CONSUMABLE1_21_2, new Consumable1_21_2(food.eatSeconds(), 1 /* eat */, sound, true, consumeEffects)); if (food.usingConvertsTo() != null) { dataContainer.set(StructuredDataKey.USE_REMAINDER, food.usingConvertsTo());