package net.minecraft.world.level.storage.loot;

import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
import java.lang.reflect.Type;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import java.util.Collections;
import java.util.Set;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import com.google.gson.JsonElement;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import com.google.gson.JsonObject;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import com.google.gson.Gson;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;

public class PredicateManager extends SimpleJsonResourceReloadListener {
    private static final Logger LOGGER;
    private static final Gson GSON;
    private Map<ResourceLocation, LootItemCondition> conditions;
    
    public PredicateManager() {
        super(PredicateManager.GSON, "predicates");
        this.conditions = ImmutableMap.of();
    }
    
    @Nullable
    public LootItemCondition get(final ResourceLocation sm) {
        return this.conditions.get(sm);
    }
    
    @Override
    protected void apply(final Map<ResourceLocation, JsonObject> map, final ResourceManager zb, final ProfilerFiller aio) {
        final ImmutableMap.Builder<ResourceLocation, LootItemCondition> builder5 = ImmutableMap.<ResourceLocation, LootItemCondition>builder();
        LootItemCondition cum2;
        final ImmutableMap.Builder<ResourceLocation, LootItemCondition> builder6;
        map.forEach((sm, jsonObject) -> {
            try {
                cum2 = PredicateManager.GSON.<LootItemCondition>fromJson(jsonObject, LootItemCondition.class);
                builder6.put(sm, cum2);
            }
            catch (Exception exception4) {
                PredicateManager.LOGGER.error("Couldn't parse loot table {}", sm, exception4);
            }
            return;
        });
        final Map<ResourceLocation, LootItemCondition> map2 = builder5.build();
        final ValidationContext csg7 = new ValidationContext(LootContextParamSets.ALL_PARAMS, map2::get, sm -> null);
        map2.forEach((sm, cum) -> cum.validate(csg7.enterCondition("{" + sm + "}", sm)));
        csg7.getProblems().forEach((string1, string2) -> PredicateManager.LOGGER.warn("Found validation problem in " + string1 + ": " + string2));
        this.conditions = map2;
    }
    
    public Set<ResourceLocation> getKeys() {
        return Collections.<ResourceLocation>unmodifiableSet(this.conditions.keySet());
    }
    
    static {
        LOGGER = LogManager.getLogger();
        GSON = new GsonBuilder().registerTypeAdapter(RandomValueBounds.class, new RandomValueBounds.Serializer()).registerTypeAdapter(BinomialDistributionGenerator.class, new BinomialDistributionGenerator.Serializer()).registerTypeAdapter(ConstantIntValue.class, new ConstantIntValue.Serializer()).registerTypeHierarchyAdapter(LootItemCondition.class, new LootItemConditions.Serializer()).registerTypeHierarchyAdapter(LootContext.EntityTarget.class, new LootContext.EntityTarget.Serializer()).create();
    }
}
