diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java index a3aad80ae..c8422d1b4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java @@ -30,7 +30,8 @@ import java.util.concurrent.TimeUnit; public class ChunkDataPacket implements ServerPacket, CacheablePacket { private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); - private static final TemporaryCache CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES); + private static final TemporaryCache CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES, + notification -> notification.getValue().getBuffer().release()); public boolean fullChunk; public Biome[] biomes; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java index 68fe51a54..cb1d23aa8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java @@ -15,7 +15,8 @@ import java.util.concurrent.TimeUnit; public class UpdateLightPacket implements ServerPacket, CacheablePacket { - private static final TemporaryCache CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES); + private static final TemporaryCache CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES, + notification -> notification.getValue().getBuffer().release()); public int chunkX; public int chunkZ; diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 0b2bbf0ac..e6aef1096 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -153,13 +153,10 @@ public class NettyPlayerConnection extends PlayerConnection { timestamp > timedBuffer.getTimestamp(); if (shouldUpdate) { - final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false); - TimedBuffer oldBuffer = timedBuffer; + // Buffer freed by guava cache #removalListener + final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true); timedBuffer = new TimedBuffer(buffer, timestamp); temporaryCache.cache(identifier, timedBuffer); - if (oldBuffer != null) { - oldBuffer.getBuffer().release(); - } } write(new FramedPacket(timedBuffer.getBuffer())); diff --git a/src/main/java/net/minestom/server/utils/cache/TemporaryCache.java b/src/main/java/net/minestom/server/utils/cache/TemporaryCache.java index ed0b43150..af5cf714e 100644 --- a/src/main/java/net/minestom/server/utils/cache/TemporaryCache.java +++ b/src/main/java/net/minestom/server/utils/cache/TemporaryCache.java @@ -2,6 +2,7 @@ package net.minestom.server.utils.cache; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,10 +23,11 @@ public class TemporaryCache { * * @param duration the time before considering an object unused */ - public TemporaryCache(long duration, TimeUnit timeUnit) { + public TemporaryCache(long duration, TimeUnit timeUnit, RemovalListener removalListener) { this.cache = CacheBuilder.newBuilder() .expireAfterWrite(duration, timeUnit) .softValues() + .removalListener(removalListener) .build(); }