diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 8be90c860..c154c55a5 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -40,7 +40,7 @@ import java.util.zip.Deflater; */ public final class PacketUtils { private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager(); - private static final ThreadLocal LOCAL_DEFLATER = ThreadLocal.withInitial(Deflater::new); + private static final LocalCache LOCAL_DEFLATER = LocalCache.of(Deflater::new); /// Local buffers private static final LocalCache PACKET_BUFFER = LocalCache.ofBuffer(Server.MAX_PACKET_SIZE); diff --git a/src/main/java/net/minestom/server/utils/cache/LocalCache.java b/src/main/java/net/minestom/server/utils/cache/LocalCache.java index 8f45b2425..c7f9b4602 100644 --- a/src/main/java/net/minestom/server/utils/cache/LocalCache.java +++ b/src/main/java/net/minestom/server/utils/cache/LocalCache.java @@ -27,8 +27,12 @@ public final class LocalCache { this.fallback = ThreadLocal.withInitial(supplier); } + public static LocalCache of(@NotNull Supplier supplier) { + return new LocalCache<>(supplier); + } + public static LocalCache ofBuffer(int size) { - return new LocalCache<>(() -> ByteBuffer.allocateDirect(size)); + return of(() -> ByteBuffer.allocateDirect(size)); } public T get() {