diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 441fb2eb8..e70c209d9 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -170,7 +170,7 @@ public final class PacketUtils { public static void flush() { synchronized (VIEWABLE_PACKET_LOCK) { for (ViewableStorage viewableStorage : VIEWABLE_STORAGE_MAP.values()) { - viewableStorage.process(); + viewableStorage.process(null); } } } @@ -251,7 +251,7 @@ public final class PacketUtils { process(new SingleEntry(framedPacket, connection)); return; } - if (!buffer.canWrite(packetSize)) process(); + if (!buffer.canWrite(packetSize)) process(null); final int start = buffer.writerOffset(); buffer.write(framedPacket); final int end = buffer.writerOffset(); @@ -262,10 +262,11 @@ public final class PacketUtils { } private synchronized void process(@Nullable SingleEntry singleEntry) { - if (buffer.writerOffset() == 0) - return; // TODO: there is nothing in the buffer, remove from VIEWABLE_STORAGE_MAP - final Viewable viewable = this.viewable.get(); - if (viewable == null) return; + final Viewable viewable; + if (buffer.writerOffset() == 0 || (viewable = this.viewable.get()) == null) { + clear(); + return; + } for (Player player : viewable.getViewers()) { PlayerConnection connection = player.getPlayerConnection(); Consumer writer = connection instanceof PlayerSocketConnection @@ -298,13 +299,12 @@ public final class PacketUtils { writer.accept(singleEntry.buffer.position(0)); } } - // Clear state - this.entityIdMap.clear(); - this.buffer.clear(); + clear(); } - private void process() { - process(null); + private void clear() { + this.entityIdMap.clear(); + this.buffer.clear(); } private static final class SingleEntry {