Update VIEWABLE_STORAGE_MAP as compute is no longer required

This commit is contained in:
themode 2022-02-13 19:15:24 +01:00
parent 2608071ee4
commit 0ca5e2ade6

View File

@ -1,5 +1,6 @@
package net.minestom.server.utils; package net.minestom.server.utils;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -31,7 +32,6 @@ import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
@ -54,7 +54,7 @@ public final class PacketUtils {
public static final boolean VIEWABLE_PACKET = getBoolean("minestom.viewable-packet", true); public static final boolean VIEWABLE_PACKET = getBoolean("minestom.viewable-packet", true);
// Viewable packets // Viewable packets
private static final ConcurrentMap<Viewable, ViewableStorage> VIEWABLE_STORAGE_MAP = ConcurrentMap.class.cast(Caffeine.newBuilder().weakKeys().build().asMap()); private static final Cache<Viewable, ViewableStorage> VIEWABLE_STORAGE_MAP = Caffeine.newBuilder().weakKeys().build();
private PacketUtils() { private PacketUtils() {
} }
@ -139,11 +139,8 @@ public final class PacketUtils {
return; return;
} }
final Player exception = entity instanceof Player ? (Player) entity : null; final Player exception = entity instanceof Player ? (Player) entity : null;
VIEWABLE_STORAGE_MAP.compute(viewable, (v, storage) -> { ViewableStorage storage = VIEWABLE_STORAGE_MAP.get(viewable, (unused) -> new ViewableStorage());
if (storage == null) storage = new ViewableStorage(); storage.append(viewable, serverPacket, exception);
storage.append(v, serverPacket, exception);
return storage;
});
} }
@ApiStatus.Experimental @ApiStatus.Experimental
@ -154,14 +151,14 @@ public final class PacketUtils {
@ApiStatus.Internal @ApiStatus.Internal
public static void flush() { public static void flush() {
if (VIEWABLE_PACKET) { if (VIEWABLE_PACKET) {
VIEWABLE_STORAGE_MAP.entrySet().parallelStream().forEach(entry -> VIEWABLE_STORAGE_MAP.asMap().entrySet().parallelStream().forEach(entry ->
entry.getValue().process(entry.getKey())); entry.getValue().process(entry.getKey()));
} }
} }
@ApiStatus.Internal @ApiStatus.Internal
public static @Nullable BinaryBuffer readPackets(@NotNull BinaryBuffer readBuffer, boolean compressed, public static @Nullable BinaryBuffer readPackets(@NotNull BinaryBuffer readBuffer, boolean compressed,
BiConsumer<Integer, ByteBuffer> payloadConsumer) throws DataFormatException { BiConsumer<Integer, ByteBuffer> payloadConsumer) throws DataFormatException {
BinaryBuffer remaining = null; BinaryBuffer remaining = null;
while (readBuffer.readableBytes() > 0) { while (readBuffer.readableBytes() > 0) {
final var beginMark = readBuffer.mark(); final var beginMark = readBuffer.mark();