package net.minecraft.server;

import com.google.gson.TypeAdapterFactory;
import net.minecraft.util.LowerCaseEnumTypeAdapterFactory;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.Component;
import net.minecraft.advancements.AdvancementRewards;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import java.lang.reflect.Type;
import java.util.Collection;
import javax.annotation.Nullable;
import java.util.Iterator;
import net.minecraft.advancements.TreeNodePosition;
import com.google.gson.JsonParseException;
import com.google.gson.JsonElement;
import net.minecraft.advancements.Advancement;
import com.google.common.collect.Maps;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
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;
    
    public ServerAdvancementManager() {
        super(ServerAdvancementManager.GSON, "advancements");
        this.advancements = new AdvancementList();
    }
    
    @Override
    protected void apply(final Map<ResourceLocation, JsonObject> map, final ResourceManager zb, final ProfilerFiller aio) {
        final Map<ResourceLocation, Advancement.Builder> map2 = Maps.newHashMap();
        Advancement.Builder a4;
        final Map<ResourceLocation, Advancement.Builder> map3;
        final RuntimeException ex;
        RuntimeException runtimeException4;
        map.forEach((sm, jsonObject) -> {
            try {
                a4 = ServerAdvancementManager.GSON.<Advancement.Builder>fromJson(jsonObject, Advancement.Builder.class);
                map3.put(sm, a4);
            }
            catch (JsonParseException | IllegalArgumentException ex2) {
                runtimeException4 = ex;
                ServerAdvancementManager.LOGGER.error("Parsing error loading custom advancement {}: {}", sm, runtimeException4.getMessage());
            }
            return;
        });
        final AdvancementList v6 = new AdvancementList();
        v6.add(map2);
        for (final Advancement u8 : v6.getRoots()) {
            if (u8.getDisplay() != null) {
                TreeNodePosition.run(u8);
            }
        }
        this.advancements = v6;
    }
    
    @Nullable
    public Advancement getAdvancement(final ResourceLocation sm) {
        return this.advancements.get(sm);
    }
    
    public Collection<Advancement> getAllAdvancements() {
        return this.advancements.getAllAdvancements();
    }
    
    static {
        LOGGER = LogManager.getLogger();
        final JsonObject jsonObject4;
        GSON = new GsonBuilder().registerTypeHierarchyAdapter(Advancement.Builder.class, (jsonElement, type, jsonDeserializationContext) -> {
            jsonObject4 = GsonHelper.convertToJsonObject(jsonElement, "advancement");
            return Advancement.Builder.fromJson(jsonObject4, jsonDeserializationContext);
        }).registerTypeAdapter(AdvancementRewards.class, new AdvancementRewards.Deserializer()).registerTypeHierarchyAdapter(Component.class, new Component.Serializer()).registerTypeHierarchyAdapter(Style.class, new Style.Serializer()).registerTypeAdapterFactory(new LowerCaseEnumTypeAdapterFactory()).create();
    }
}
