package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.util.Map;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.io.IOException;
import net.minecraft.core.Registry;
import net.minecraft.stats.StatType;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.stats.Stat;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.network.protocol.Packet;

public class ClientboundAwardStatsPacket implements Packet<ClientGamePacketListener> {
    private Object2IntMap<Stat<?>> stats;
    
    public ClientboundAwardStatsPacket() {
    }
    
    public ClientboundAwardStatsPacket(final Object2IntMap<Stat<?>> object2IntMap) {
        this.stats = object2IntMap;
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleAwardStats(this);
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        final int integer3 = kv.readVarInt();
        this.stats = (Object2IntMap<Stat<?>>)new Object2IntOpenHashMap(integer3);
        for (int integer4 = 0; integer4 < integer3; ++integer4) {
            this.readStat(Registry.STAT_TYPE.byId(kv.readVarInt()), kv);
        }
    }
    
    private <T> void readStat(final StatType<T> aaq, final FriendlyByteBuf kv) {
        final int integer4 = kv.readVarInt();
        final int integer5 = kv.readVarInt();
        this.stats.put(aaq.get(aaq.getRegistry().byId(integer4)), integer5);
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.stats.size());
        for (final Object2IntMap.Entry<Stat<?>> entry4 : this.stats.object2IntEntrySet()) {
            final Stat<?> aao5 = entry4.getKey();
            kv.writeVarInt(Registry.STAT_TYPE.getId(aao5.getType()));
            kv.writeVarInt(this.getId(aao5));
            kv.writeVarInt(entry4.getIntValue());
        }
    }
    
    private <T> int getId(final Stat<T> aao) {
        return aao.getType().getRegistry().getId(aao.getValue());
    }
    
    public Map<Stat<?>, Integer> getStats() {
        return (Map<Stat<?>, Integer>)this.stats;
    }
}
