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

import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.functions.LootItemFunctions;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntries;
import net.minecraft.world.level.storage.loot.entries.LootPoolEntryContainer;
import java.lang.reflect.Type;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
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 com.google.common.collect.ImmutableMap;
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 LootTables extends SimpleJsonResourceReloadListener {
    private static final Logger LOGGER;
    private static final Gson GSON;
    private Map<ResourceLocation, LootTable> tables;
    private final PredicateManager predicateManager;
    
    public LootTables(final PredicateManager csc) {
        super(LootTables.GSON, "loot_tables");
        this.tables = ImmutableMap.of();
        this.predicateManager = csc;
    }
    
    public LootTable get(final ResourceLocation sm) {
        return this.tables.getOrDefault(sm, LootTable.EMPTY);
    }
    
    @Override
    protected void apply(final Map<ResourceLocation, JsonObject> map, final ResourceManager zb, final ProfilerFiller aio) {
        final ImmutableMap.Builder<ResourceLocation, LootTable> builder5 = ImmutableMap.<ResourceLocation, LootTable>builder();
        final JsonObject jsonObject2 = map.remove(BuiltInLootTables.EMPTY);
        if (jsonObject2 != null) {
            LootTables.LOGGER.warn("Datapack tried to redefine {} loot table, ignoring", BuiltInLootTables.EMPTY);
        }
        LootTable csa2;
        final ImmutableMap.Builder<ResourceLocation, LootTable> builder6;
        map.forEach((sm, jsonObject) -> {
            try {
                csa2 = LootTables.GSON.<LootTable>fromJson(jsonObject, LootTable.class);
                builder6.put(sm, csa2);
            }
            catch (Exception exception4) {
                LootTables.LOGGER.error("Couldn't parse loot table {}", sm, exception4);
            }
            return;
        });
        builder5.put(BuiltInLootTables.EMPTY, LootTable.EMPTY);
        final ImmutableMap<ResourceLocation, LootTable> immutableMap7 = builder5.build();
        final ValidationContext csg8 = new ValidationContext(LootContextParamSets.ALL_PARAMS, this.predicateManager::get, immutableMap7::get);
        immutableMap7.forEach((sm, csa) -> validate(csg8, sm, csa));
        csg8.getProblems().forEach((string1, string2) -> LootTables.LOGGER.warn("Found validation problem in " + string1 + ": " + string2));
        this.tables = immutableMap7;
    }
    
    public static void validate(final ValidationContext csg, final ResourceLocation sm, final LootTable csa) {
        csa.validate(csg.setParams(csa.getParamSet()).enterTable("{" + sm + "}", sm));
    }
    
    public static JsonElement serialize(final LootTable csa) {
        return LootTables.GSON.toJsonTree(csa);
    }
    
    public Set<ResourceLocation> getIds() {
        return this.tables.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()).registerTypeAdapter(IntLimiter.class, new IntLimiter.Serializer()).registerTypeAdapter(LootPool.class, new LootPool.Serializer()).registerTypeAdapter(LootTable.class, new LootTable.Serializer()).registerTypeHierarchyAdapter(LootPoolEntryContainer.class, new LootPoolEntries.Serializer()).registerTypeHierarchyAdapter(LootItemFunction.class, new LootItemFunctions.Serializer()).registerTypeHierarchyAdapter(LootItemCondition.class, new LootItemConditions.Serializer()).registerTypeHierarchyAdapter(LootContext.EntityTarget.class, new LootContext.EntityTarget.Serializer()).create();
    }
}
