From 20f7738f08790785a16394f4e8a32f36e3792e0d Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Fri, 12 Jul 2024 00:11:11 +0200 Subject: [PATCH] Add cancel-block-sounds option for 1.8->1.9 place/break fix (#4011) --- .../api/configuration/ViaVersionConfig.java | 7 +++ .../bukkit/platform/BukkitViaLoader.java | 4 +- .../configuration/AbstractViaConfig.java | 7 +++ .../rewriter/WorldPacketRewriter1_9.java | 45 ++++++++++--------- .../resources/assets/viaversion/config.yml | 2 + 5 files changed, 44 insertions(+), 21 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 c80f9729d..bd7491a68 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 @@ -457,4 +457,11 @@ public interface ViaVersionConfig extends Config { * @return true if enabled */ boolean handleInvalidItemCount(); + + /** + * Tries to cancel block break/place sounds sent by 1.8 servers to 1.9+ clients to prevent them from playing twice + * + * @return true if enabled + */ + boolean cancelBlockSounds(); } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java index 9f56fd9ab..fe549ae35 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java @@ -87,7 +87,9 @@ public class BukkitViaLoader implements ViaPlatformLoader { if (serverProtocolVersion.olderThan(ProtocolVersion.v1_9)) { new ArmorListener(plugin).register(); new DeathListener(plugin).register(); - new BlockListener(plugin).register(); + if (plugin.getConf().cancelBlockSounds()) { + new BlockListener(plugin).register(); + } if (plugin.getConf().isItemCache()) { handItemCache = new HandItemCache(); 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 03408928a..22545608f 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -93,6 +93,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf private boolean translateOcelotToCat; private boolean enforceSecureChat; private boolean handleInvalidItemCount; + private boolean cancelBlockSounds; protected AbstractViaConfig(final File configFile, final Logger logger) { super(configFile, logger); @@ -159,6 +160,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf translateOcelotToCat = getBoolean("translate-ocelot-to-cat", true); enforceSecureChat = getBoolean("enforce-secure-chat", false); handleInvalidItemCount = getBoolean("handle-invalid-item-count", false); + cancelBlockSounds = getBoolean("cancel-block-sounds", true); } private BlockedProtocolVersions loadBlockedProtocolVersions() { @@ -535,4 +537,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf public boolean handleInvalidItemCount() { return handleInvalidItemCount; } + + @Override + public boolean cancelBlockSounds() { + return cancelBlockSounds; + } } 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 04b58a6b8..ead2b6d1c 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 @@ -112,6 +112,10 @@ public class WorldPacketRewriter1_9 { } wrapper.set(Types.STRING, 0, newname); wrapper.write(Types.VAR_INT, catid); // Write Category ID + + if (!Via.getConfig().cancelBlockSounds()) { + return; + } if (effect != null && effect.isBreakSound()) { EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class); int x = wrapper.passthrough(Types.INT); //Position X @@ -358,7 +362,27 @@ public class WorldPacketRewriter1_9 { map(Types.UNSIGNED_BYTE); // 5 - Y map(Types.UNSIGNED_BYTE); // 6 - Z - //Register block place to fix sounds + // Handle CommandBlocks + handler(wrapper -> { + CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class); + + BlockPosition pos = wrapper.get(Types.BLOCK_POSITION1_8, 0); + Optional tag = provider.get(wrapper.user(), pos); + // Send the Update Block Entity packet if present + if (tag.isPresent()) { + PacketWrapper updateBlockEntity = PacketWrapper.create(ClientboundPackets1_9.BLOCK_ENTITY_DATA, null, wrapper.user()); + + updateBlockEntity.write(Types.BLOCK_POSITION1_8, pos); + updateBlockEntity.write(Types.UNSIGNED_BYTE, (short) 2); + updateBlockEntity.write(Types.NAMED_COMPOUND_TAG, tag.get()); + + updateBlockEntity.scheduleSend(Protocol1_8To1_9.class); + } + }); + + if (!Via.getConfig().cancelBlockSounds()) { + return; + } handler(wrapper -> { int face = wrapper.get(Types.UNSIGNED_BYTE, 0); if (face == 255) @@ -378,25 +402,6 @@ public class WorldPacketRewriter1_9 { EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class); tracker.addBlockInteraction(new BlockPosition(x, y, z)); }); - - // Handle CommandBlocks - handler(wrapper -> { - CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class); - - BlockPosition pos = wrapper.get(Types.BLOCK_POSITION1_8, 0); - Optional tag = provider.get(wrapper.user(), pos); - // Send the Update Block Entity packet if present - if (tag.isPresent()) { - PacketWrapper updateBlockEntity = PacketWrapper.create(ClientboundPackets1_9.BLOCK_ENTITY_DATA, null, wrapper.user()); - - updateBlockEntity.write(Types.BLOCK_POSITION1_8, pos); - updateBlockEntity.write(Types.UNSIGNED_BYTE, (short) 2); - updateBlockEntity.write(Types.NAMED_COMPOUND_TAG, tag.get()); - - updateBlockEntity.scheduleSend(Protocol1_8To1_9.class); - } - }); - } }); } diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index d0c1f476f..5508cddc6 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -207,3 +207,5 @@ replacement-piston-id: 0 chunk-border-fix: false # Allows 1.9+ left-handedness (main hand) on 1.8 servers 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 \ No newline at end of file