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 nl) {
        nl.handleAwardStats(this);
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        final int integer3 = mg.readVarInt();
        this.stats = (Object2IntMap<Stat<?>>)new Object2IntOpenHashMap(integer3);
        for (int integer4 = 0; integer4 < integer3; ++integer4) {
            this.readStat(Registry.STAT_TYPE.byId(mg.readVarInt()), mg);
        }
    }
    
    private <T> void readStat(final StatType<T> act, final FriendlyByteBuf mg) {
        final int integer4 = mg.readVarInt();
        final int integer5 = mg.readVarInt();
        this.stats.put(act.get(act.getRegistry().byId(integer4)), integer5);
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.stats.size());
        for (final Object2IntMap.Entry<Stat<?>> entry4 : this.stats.object2IntEntrySet()) {
            final Stat<?> acr5 = entry4.getKey();
            mg.writeVarInt(Registry.STAT_TYPE.getId(acr5.getType()));
            mg.writeVarInt(this.getId(acr5));
            mg.writeVarInt(entry4.getIntValue());
        }
    }
    
    private <T> int getId(final Stat<T> acr) {
        return acr.getType().getRegistry().getId(acr.getValue());
    }
    
    public Map<Stat<?>, Integer> getStats() {
        return (Map<Stat<?>, Integer>)this.stats;
    }
}
