Fixed block break particles and sound being played two times for the breaker

This commit is contained in:
themode 2020-12-14 06:06:28 +01:00
parent c1b584da1b
commit 3a988ddba1
3 changed files with 32 additions and 4 deletions

View File

@ -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.network.packet.server.play.UnloadChunkPacket;
import net.minestom.server.storage.StorageLocation; import net.minestom.server.storage.StorageLocation;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.block.CustomBlockUtils; import net.minestom.server.utils.block.CustomBlockUtils;
import net.minestom.server.utils.callback.OptionalCallback; import net.minestom.server.utils.callback.OptionalCallback;
@ -398,7 +399,14 @@ public class InstanceContainer extends Instance {
effectPacket.data = blockStateId; effectPacket.data = blockStateId;
effectPacket.disableRelativeVolume = false; 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;
});
} }
} }

View File

@ -416,6 +416,7 @@ public final class NBTUtils {
* @return the value representation of a tag * @return the value representation of a tag
* @throws UnsupportedOperationException if the tag type is not supported * @throws UnsupportedOperationException if the tag type is not supported
*/ */
@NotNull
public static Object fromNBT(@NotNull NBT nbt) { public static Object fromNBT(@NotNull NBT nbt) {
if (nbt instanceof NBTNumber) { if (nbt instanceof NBTNumber) {
return ((NBTNumber) nbt).getValue(); return ((NBTNumber) nbt).getValue();

View File

@ -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.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.callback.validator.PlayerValidator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.zip.Deflater; import java.util.zip.Deflater;
@ -36,10 +38,12 @@ public final class PacketUtils {
* <p> * <p>
* Can drastically improve performance since the packet will not have to be processed as much. * 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 players the players to send the packet to
* @param packet the packet to send to the players * @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<Player> players, @NotNull ServerPacket packet) { public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet,
@Nullable PlayerValidator playerValidator) {
if (players.isEmpty()) if (players.isEmpty())
return; return;
@ -50,6 +54,11 @@ public final class PacketUtils {
// Send packet to all players // Send packet to all players
for (Player player : 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(); final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) { if (playerConnection instanceof NettyPlayerConnection) {
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection; 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<Player> players, @NotNull ServerPacket packet) {
sendGroupedPacket(players, packet, null);
}
/** /**
* Writes a {@link ServerPacket} into a {@link ByteBuf}. * Writes a {@link ServerPacket} into a {@link ByteBuf}.
* *