#How do I get a PacketCodec for a Map<Integer, Pair<String,List<Integer>>>?
8 messages · Page 1 of 1 (latest)
you would need to build your own pair packet codec, I don't think vanilla has one
something like this should do:
public record MapOfPariListCodecSample(Map<Integer, Pair<String, List<Integer>>> map) {
public static PacketCodec<ByteBuf, MapOfPariListCodecSample> STREAM_CODEC = PacketCodec.tuple(
PacketCodecs.map(HashMap::new,
PacketCodecs.INTEGER,
pairCodec(PacketCodecs.STRING, PacketCodecs.INTEGER.collect(PacketCodecs.toList()))),
MapOfPariListCodecSample::map,
MapOfPariListCodecSample::new);
private static <Buffer, F, S> PacketCodec<Buffer, Pair<F, S>> pairCodec(PacketCodec<? super Buffer, F> firstCodec,
PacketCodec<? super Buffer, S> secondCodec) {
return PacketCodec.tuple(firstCodec, Pair::getFirst, secondCodec, Pair::getSecond, Pair::of);
}
}
@main tinsel
I'd recommend using FastUtil maps instead of regular HashMaps, so Int2ObjectOpenHashMap::new instead of HashMap::new
It's just an example. They can use the map implementation that best suits their needs
works like a charm! Thank you so much!