mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-04-19 19:08:16 +02:00
Add two more pre-sizing checks
This commit is contained in:
@@ -36,7 +36,9 @@ import io.netty.handler.codec.EncoderException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.nbt.BinaryTag;
|
import net.kyori.adventure.nbt.BinaryTag;
|
||||||
@@ -865,6 +867,18 @@ public enum ProtocolUtils {
|
|||||||
return new ArrayList<>(Math.min(initialCapacity, Short.MAX_VALUE));
|
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 <K> key type
|
||||||
|
* @param <V> value type
|
||||||
|
* @return pre-sized map
|
||||||
|
*/
|
||||||
|
public static <K, V> Map<K, V> newMap(int initialCapacity) {
|
||||||
|
return new HashMap<>(Math.min(initialCapacity, Short.MAX_VALUE));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the direction in which a packet flows.
|
* Represents the direction in which a packet flows.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
|
|
||||||
package com.velocitypowered.proxy.protocol.packet;
|
package com.velocitypowered.proxy.protocol.packet;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.velocitypowered.api.network.ProtocolVersion;
|
import com.velocitypowered.api.network.ProtocolVersion;
|
||||||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
||||||
import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
||||||
@@ -51,7 +50,7 @@ public class RemovePlayerInfoPacket implements MinecraftPacket {
|
|||||||
public void decode(ByteBuf buf, ProtocolUtils.Direction direction,
|
public void decode(ByteBuf buf, ProtocolUtils.Direction direction,
|
||||||
ProtocolVersion protocolVersion) {
|
ProtocolVersion protocolVersion) {
|
||||||
int length = ProtocolUtils.readVarInt(buf);
|
int length = ProtocolUtils.readVarInt(buf);
|
||||||
Collection<UUID> profilesToRemove = Lists.newArrayListWithCapacity(length);
|
Collection<UUID> profilesToRemove = ProtocolUtils.newList(length);
|
||||||
for (int idx = 0; idx < length; idx++) {
|
for (int idx = 0; idx < length; idx++) {
|
||||||
profilesToRemove.add(ProtocolUtils.readUuid(buf));
|
profilesToRemove.add(ProtocolUtils.readUuid(buf));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class ClientboundCustomReportDetailsPacket implements MinecraftPacket {
|
|||||||
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
|
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
|
||||||
int detailsCount = ProtocolUtils.readVarInt(buf);
|
int detailsCount = ProtocolUtils.readVarInt(buf);
|
||||||
|
|
||||||
this.details = new HashMap<>(detailsCount);
|
this.details = ProtocolUtils.newMap(detailsCount);
|
||||||
for (int i = 0; i < detailsCount; i++) {
|
for (int i = 0; i < detailsCount; i++) {
|
||||||
details.put(ProtocolUtils.readString(buf), ProtocolUtils.readString(buf));
|
details.put(ProtocolUtils.readString(buf), ProtocolUtils.readString(buf));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user