diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index ab7c4831f..21d3fb981 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -36,7 +36,9 @@ import io.netty.handler.codec.EncoderException; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import net.kyori.adventure.key.Key; import net.kyori.adventure.nbt.BinaryTag; @@ -865,6 +867,18 @@ public enum ProtocolUtils { return new ArrayList<>(Math.min(initialCapacity, Short.MAX_VALUE)); } + /** + * Returns a pre-sized map with a max initial size of {@code Short.MAX_VALUE}. + * + * @param initialCapacity expected initial capacity + * @param key type + * @param value type + * @return pre-sized map + */ + public static Map newMap(int initialCapacity) { + return new HashMap<>(Math.min(initialCapacity, Short.MAX_VALUE)); + } + /** * Represents the direction in which a packet flows. */ diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RemovePlayerInfoPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RemovePlayerInfoPacket.java index 90ab38717..3851d2783 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RemovePlayerInfoPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/RemovePlayerInfoPacket.java @@ -17,7 +17,6 @@ package com.velocitypowered.proxy.protocol.packet; -import com.google.common.collect.Lists; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; @@ -51,7 +50,7 @@ public class RemovePlayerInfoPacket implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { int length = ProtocolUtils.readVarInt(buf); - Collection profilesToRemove = Lists.newArrayListWithCapacity(length); + Collection profilesToRemove = ProtocolUtils.newList(length); for (int idx = 0; idx < length; idx++) { profilesToRemove.add(ProtocolUtils.readUuid(buf)); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/config/ClientboundCustomReportDetailsPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/config/ClientboundCustomReportDetailsPacket.java index 6a3618cb7..c51c5ff20 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/config/ClientboundCustomReportDetailsPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/config/ClientboundCustomReportDetailsPacket.java @@ -40,7 +40,7 @@ public class ClientboundCustomReportDetailsPacket implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { int detailsCount = ProtocolUtils.readVarInt(buf); - this.details = new HashMap<>(detailsCount); + this.details = ProtocolUtils.newMap(detailsCount); for (int i = 0; i < detailsCount; i++) { details.put(ProtocolUtils.readString(buf), ProtocolUtils.readString(buf)); }