package net.minecraft.server;

import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import java.util.Collection;
import javax.annotation.Nullable;
import java.util.Iterator;
import com.google.gson.JsonObject;
import net.minecraft.advancements.TreeNodePosition;
import com.google.gson.JsonParseException;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.critereon.DeserializationContext;
import net.minecraft.util.GsonHelper;
import com.google.common.collect.Maps;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.world.level.storage.loot.PredicateManager;
import net.minecraft.advancements.AdvancementList;
import com.google.gson.Gson;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;

public class ServerAdvancementManager extends SimpleJsonResourceReloadListener {
    private static final Logger LOGGER;
    private static final Gson GSON;
    private AdvancementList advancements;
    private final PredicateManager predicateManager;
    
    public ServerAdvancementManager(final PredicateManager day) {
        super(ServerAdvancementManager.GSON, "advancements");
        this.advancements = new AdvancementList();
        this.predicateManager = day;
    }
    
    @Override
    protected void apply(final Map<ResourceLocation, JsonElement> map, final ResourceManager abc, final ProfilerFiller ami) {
        final Map<ResourceLocation, Advancement.Builder> map2 = Maps.newHashMap();
        JsonObject jsonObject5;
        Advancement.Builder a6;
        final Map<ResourceLocation, Advancement.Builder> map3;
        final RuntimeException ex;
        RuntimeException runtimeException5;
        map.forEach((uh, jsonElement) -> {
            try {
                jsonObject5 = GsonHelper.convertToJsonObject(jsonElement, "advancement");
                a6 = Advancement.Builder.fromJson(jsonObject5, new DeserializationContext(uh, this.predicateManager));
                map3.put(uh, a6);
            }
            catch (JsonParseException | IllegalArgumentException ex2) {
                runtimeException5 = ex;
                ServerAdvancementManager.LOGGER.error("Parsing error loading custom advancement {}: {}", uh, runtimeException5.getMessage());
            }
            return;
        });
        final AdvancementList x6 = new AdvancementList();
        x6.add(map2);
        for (final Advancement w8 : x6.getRoots()) {
            if (w8.getDisplay() != null) {
                TreeNodePosition.run(w8);
            }
        }
        this.advancements = x6;
    }
    
    @Nullable
    public Advancement getAdvancement(final ResourceLocation uh) {
        return this.advancements.get(uh);
    }
    
    public Collection<Advancement> getAllAdvancements() {
        return this.advancements.getAllAdvancements();
    }
    
    static {
        LOGGER = LogManager.getLogger();
        GSON = new GsonBuilder().create();
    }
}
