#How do I get a PacketCodec for a Map<Integer, Pair<String,List<Integer>>>?

8 messages · Page 1 of 1 (latest)

main tinsel
#

see title, and please ping me when replying

olive moth
#

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

stuck saddle
#

I'd recommend using FastUtil maps instead of regular HashMaps, so Int2ObjectOpenHashMap::new instead of HashMap::new

olive moth
#

It's just an example. They can use the map implementation that best suits their needs

main tinsel
#

works like a charm! Thank you so much!