From 3a988ddba1e636858ec2158de3e1e6e86dce9a58 Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 14 Dec 2020 06:06:28 +0100 Subject: [PATCH] Fixed block break particles and sound being played two times for the breaker --- .../server/instance/InstanceContainer.java | 10 +++++++- .../net/minestom/server/utils/NBTUtils.java | 1 + .../minestom/server/utils/PacketUtils.java | 25 ++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index ee12c91e4..0dbf42bf5 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -20,6 +20,7 @@ import net.minestom.server.network.packet.server.play.EffectPacket; import net.minestom.server.network.packet.server.play.UnloadChunkPacket; import net.minestom.server.storage.StorageLocation; import net.minestom.server.utils.BlockPosition; +import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.Position; import net.minestom.server.utils.block.CustomBlockUtils; import net.minestom.server.utils.callback.OptionalCallback; @@ -398,7 +399,14 @@ public class InstanceContainer extends Instance { effectPacket.data = blockStateId; effectPacket.disableRelativeVolume = false; - chunk.sendPacketToViewers(effectPacket); + PacketUtils.sendGroupedPacket(chunk.getViewers(), effectPacket, + (viewer) -> { + // Prevent the block breaker to play the particles and sound two times + if (customBlock == null && viewer.equals(player)) { + return false; + } + return true; + }); } } diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 3204fd21f..47435db44 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -416,6 +416,7 @@ public final class NBTUtils { * @return the value representation of a tag * @throws UnsupportedOperationException if the tag type is not supported */ + @NotNull public static Object fromNBT(@NotNull NBT nbt) { if (nbt instanceof NBTNumber) { return ((NBTNumber) nbt).getValue(); diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 739e8a13c..b4d3de067 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -11,7 +11,9 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.player.NettyPlayerConnection; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.utils.callback.validator.PlayerValidator; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.zip.Deflater; @@ -36,10 +38,12 @@ public final class PacketUtils { *

* Can drastically improve performance since the packet will not have to be processed as much. * - * @param players the players to send the packet to - * @param packet the packet to send to the players + * @param players the players to send the packet to + * @param packet the packet to send to the players + * @param playerValidator optional callback to check if a specify player of {@code players} should receive the packet */ - public static void sendGroupedPacket(@NotNull Collection players, @NotNull ServerPacket packet) { + public static void sendGroupedPacket(@NotNull Collection players, @NotNull ServerPacket packet, + @Nullable PlayerValidator playerValidator) { if (players.isEmpty()) return; @@ -50,6 +54,11 @@ public final class PacketUtils { // Send packet to all players for (Player player : players) { + + // Verify if the player should receive the packet + if (playerValidator != null && !playerValidator.isValid(player)) + continue; + final PlayerConnection playerConnection = player.getPlayerConnection(); if (playerConnection instanceof NettyPlayerConnection) { final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection; @@ -61,6 +70,16 @@ public final class PacketUtils { } } + /** + * Same as {@link #sendGroupedPacket(Collection, ServerPacket, PlayerValidator)} + * but with the player validator sets to null. + * + * @see #sendGroupedPacket(Collection, ServerPacket, PlayerValidator) + */ + public static void sendGroupedPacket(@NotNull Collection players, @NotNull ServerPacket packet) { + sendGroupedPacket(players, packet, null); + } + /** * Writes a {@link ServerPacket} into a {@link ByteBuf}. *