package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import com.google.common.collect.Sets;
import net.minecraft.network.FriendlyByteBuf;
import java.util.Iterator;
import com.google.common.collect.Maps;
import java.util.Collection;
import net.minecraft.advancements.AdvancementProgress;
import java.util.Set;
import net.minecraft.advancements.Advancement;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.network.protocol.Packet;

public class ClientboundUpdateAdvancementsPacket implements Packet<ClientGamePacketListener> {
    private boolean reset;
    private Map<ResourceLocation, Advancement.Builder> added;
    private Set<ResourceLocation> removed;
    private Map<ResourceLocation, AdvancementProgress> progress;
    
    public ClientboundUpdateAdvancementsPacket() {
    }
    
    public ClientboundUpdateAdvancementsPacket(final boolean boolean1, final Collection<Advancement> collection, final Set<ResourceLocation> set, final Map<ResourceLocation, AdvancementProgress> map) {
        this.reset = boolean1;
        this.added = Maps.newHashMap();
        for (final Advancement w7 : collection) {
            this.added.put(w7.getId(), w7.deconstruct());
        }
        this.removed = set;
        this.progress = Maps.newHashMap(map);
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleUpdateAdvancementsPacket(this);
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.reset = mg.readBoolean();
        this.added = Maps.newHashMap();
        this.removed = Sets.newLinkedHashSet();
        this.progress = Maps.newHashMap();
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            final ResourceLocation uh5 = mg.readResourceLocation();
            final Advancement.Builder a6 = Advancement.Builder.fromNetwork(mg);
            this.added.put(uh5, a6);
        }
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            final ResourceLocation uh5 = mg.readResourceLocation();
            this.removed.add(uh5);
        }
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            final ResourceLocation uh5 = mg.readResourceLocation();
            this.progress.put(uh5, AdvancementProgress.fromNetwork(mg));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeBoolean(this.reset);
        mg.writeVarInt(this.added.size());
        for (final Map.Entry<ResourceLocation, Advancement.Builder> entry4 : this.added.entrySet()) {
            final ResourceLocation uh5 = entry4.getKey();
            final Advancement.Builder a6 = entry4.getValue();
            mg.writeResourceLocation(uh5);
            a6.serializeToNetwork(mg);
        }
        mg.writeVarInt(this.removed.size());
        for (final ResourceLocation uh6 : this.removed) {
            mg.writeResourceLocation(uh6);
        }
        mg.writeVarInt(this.progress.size());
        for (final Map.Entry<ResourceLocation, AdvancementProgress> entry5 : this.progress.entrySet()) {
            mg.writeResourceLocation(entry5.getKey());
            entry5.getValue().serializeToNetwork(mg);
        }
    }
    
    public Map<ResourceLocation, Advancement.Builder> getAdded() {
        return this.added;
    }
    
    public Set<ResourceLocation> getRemoved() {
        return this.removed;
    }
    
    public Map<ResourceLocation, AdvancementProgress> getProgress() {
        return this.progress;
    }
    
    public boolean shouldReset() {
        return this.reset;
    }
}
