diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index ad15174e1..95a07e1e1 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -7,7 +7,6 @@ import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceManager; import net.minestom.server.monitoring.TickMonitor; import net.minestom.server.network.ConnectionManager; -import net.minestom.server.network.player.PlayerSocketConnection; import net.minestom.server.thread.SingleThreadProvider; import net.minestom.server.thread.ThreadProvider; import org.jetbrains.annotations.NotNull; @@ -83,10 +82,7 @@ public final class UpdateManager { // Flush all waiting packets for (Player player : connectionManager.getOnlinePlayers()) { - final var connection = player.getPlayerConnection(); - if (connection instanceof PlayerSocketConnection) { - ((PlayerSocketConnection) connection).flush(); - } + player.getPlayerConnection().flush(); } // Disable thread until next tick diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 1221ca9c9..0517c1504 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -571,9 +571,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(this, instance, firstSpawn); EventDispatcher.call(spawnEvent); - if (playerConnection instanceof PlayerSocketConnection) { - ((PlayerSocketConnection) playerConnection).flush(); - } + this.playerConnection.flush(); } /** diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index d6fc71398..24aaf4360 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -293,9 +293,7 @@ public final class ConnectionManager { EventDispatcher.call(asyncPlayerPreLoginEvent); // Close the player channel if he has been disconnected (kick) if (!player.isOnline()) { - if (playerConnection instanceof PlayerSocketConnection) { - ((PlayerSocketConnection) playerConnection).flush(); - } + playerConnection.flush(); //playerConnection.disconnect(); return; } diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index b7845baf9..4f1548a89 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -50,9 +50,8 @@ public abstract class PlayerConnection { if (tickCounter % MinecraftServer.TICK_PER_SECOND == 0 && tickCounter > 0) { tickCounter = 0; // Retrieve the packet count - final int count = packetCounter.get(); + final int count = packetCounter.getAndSet(0); this.lastPacketCounter.set(count); - this.packetCounter.set(0); if (count > MinecraftServer.getRateLimit()) { // Sent too many packets player.kick(rateLimitKickMessage); @@ -101,6 +100,15 @@ public abstract class PlayerConnection { */ public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating); + /** + * Flush waiting data to the connection. + *

+ * Might not do anything depending on the implementation. + */ + public void flush() { + // Empty + } + protected boolean shouldSendPacket(@NotNull ServerPacket serverPacket) { return player == null || PACKET_LISTENER_MANAGER.processServerPacket(serverPacket, Collections.singleton(player)); diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index d958242be..2fbba6aba 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -252,6 +252,7 @@ public class PlayerSocketConnection extends PlayerConnection { } } + @Override public void flush() { if (!channel.isOpen()) return; synchronized (tickBuffer) {