From 75c666266538cc88be2bf237f837b629fae8da89 Mon Sep 17 00:00:00 2001 From: Rocco Date: Mon, 9 Dec 2024 10:08:51 +0000 Subject: [PATCH] Sword blocking: consumables for 1.21.4+, back to shields for 1.20.5-1.21.3 (#4299) --- .../api/configuration/ViaVersionConfig.java | 8 -------- .../configuration/AbstractViaConfig.java | 7 ------- .../BlockItemPacketRewriter1_20_5.java | 6 ------ .../BlockItemPacketRewriter1_21_4.java | 19 +++++++++++++++++++ .../BlockItemPacketRewriter1_21_2.java | 15 --------------- .../rewriter/WorldPacketRewriter1_9.java | 6 ++++-- .../v1_8to1_9/storage/EntityTracker1_9.java | 10 +++++++++- .../resources/assets/viaversion/config.yml | 3 --- 8 files changed, 32 insertions(+), 42 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java index 1cb309c58..23b844129 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java +++ b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java @@ -478,12 +478,4 @@ public interface ViaVersionConfig extends Config { * @return true if enabled */ boolean fix1_21PlacementRotation(); - - /** - * If enabled, 1.20.5+ clients will have sword blocking mechanics on 1.8 servers using the consumable item component. - * Note that you won't be able to see the blocking in first person if the client is older than 1.21.4. - * - * @return true if enabled - */ - boolean swordBlockingViaConsumable(); } diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index 5ba3d448a..98684f865 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -96,7 +96,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf private boolean cancelBlockSounds; private boolean hideScoreboardNumbers; private boolean fix1_21PlacementRotation; - private boolean swordBlockingViaConsumable; protected AbstractViaConfig(final File configFile, final Logger logger) { super(configFile, logger); @@ -166,7 +165,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf cancelBlockSounds = getBoolean("cancel-block-sounds", true); hideScoreboardNumbers = getBoolean("hide-scoreboard-numbers", false); fix1_21PlacementRotation = getBoolean("fix-1_21-placement-rotation", true); - swordBlockingViaConsumable = getBoolean("sword-blocking-via-consumable", true); } private BlockedProtocolVersions loadBlockedProtocolVersions() { @@ -558,9 +556,4 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf public boolean fix1_21PlacementRotation() { return fix1_21PlacementRotation; } - - @Override - public boolean swordBlockingViaConsumable() { - return swordBlockingViaConsumable; - } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java index a85ee57a6..05570efb8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java @@ -619,12 +619,6 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java index 88395f357..ed24927b9 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java @@ -30,6 +30,7 @@ import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.BulkChunkType1_8; @@ -297,8 +298,9 @@ public class WorldPacketRewriter1_9 { wrapper.write(Types.UNSIGNED_BYTE, (short) 255); // Write item in hand Item item = Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(wrapper.user()); - // Blocking patch - if (Via.getConfig().isShieldBlocking()) { + // Blocking patch for 1.9-1.21.3 clients + if (Via.getConfig().isShieldBlocking() && + wrapper.user().getProtocolInfo().protocolVersion().olderThan(ProtocolVersion.v1_21_4)) { EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class); // Check if the shield is already there or if we have to give it here diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/EntityTracker1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/EntityTracker1_9.java index 9f59a0233..7da66cfc8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/EntityTracker1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/EntityTracker1_9.java @@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.minecraft.entitydata.types.EntityDataTypes1 import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; 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.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9; @@ -102,6 +103,11 @@ public class EntityTracker1_9 extends EntityTrackerBase { * The item in the offhand will be cleared if there is no sword in the main hand. */ public void syncShieldWithSword() { + if (user().getProtocolInfo().protocolVersion().newerThanOrEqualTo(ProtocolVersion.v1_21_4)) { + // If sword blocking is done through consumables, don't add a shield. + return; + } + boolean swordInHand = hasSwordInHand(); // Update if there is no sword in the main hand or if the player has no shield in the second hand but a sword in the main hand @@ -183,7 +189,9 @@ public class EntityTracker1_9 extends EntityTrackerBase { if (entityData.id() == 0) { // Byte byte data = (byte) entityData.getValue(); - if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { + // If sword blocking is done through consumables (1.21.4+), don't add a shield. + if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking() + && user().getProtocolInfo().protocolVersion().olderThan(ProtocolVersion.v1_21_4)) { if ((data & 0x10) == 0x10) { if (validBlocking.contains(entityId)) { Item shield = new DataItem(442, (byte) 1, (short) 0, null); diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index a7fdb4d57..e03f37307 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -215,6 +215,3 @@ chunk-border-fix: false left-handed-handling: true # Tries to cancel block break/place sounds sent by 1.8 servers to 1.9+ clients to prevent them from playing twice cancel-block-sounds: true -# If enabled, 1.20.5+ clients will have sword blocking mechanics on 1.8 servers using the consumable item component. -# Note that you won't be able to see the blocking in first person if the client is older than 1.21.4. -sword-blocking-via-consumable: true