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

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.world.level.storage.loot.predicates.ConditionUserBuilder;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
import net.minecraft.world.level.storage.loot.LootContext;
import java.util.function.Predicate;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;

public abstract class LootPoolEntryContainer implements ComposableEntryContainer {
    protected final LootItemCondition[] conditions;
    private final Predicate<LootContext> compositeCondition;
    
    protected LootPoolEntryContainer(final LootItemCondition[] arr) {
        this.conditions = arr;
        this.compositeCondition = LootItemConditions.<LootContext>andConditions(arr);
    }
    
    public void validate(final ValidationContext csg) {
        for (int integer3 = 0; integer3 < this.conditions.length; ++integer3) {
            this.conditions[integer3].validate(csg.forChild(".condition[" + integer3 + "]"));
        }
    }
    
    protected final boolean canRun(final LootContext crx) {
        return this.compositeCondition.test(crx);
    }
    
    public abstract static class Builder<T extends Builder<T>> implements ConditionUserBuilder<T> {
        private final List<LootItemCondition> conditions;
        
        public Builder() {
            this.conditions = Lists.newArrayList();
        }
        
        protected abstract T getThis();
        
        @Override
        public T when(final LootItemCondition.Builder a) {
            this.conditions.add(a.build());
            return this.getThis();
        }
        
        @Override
        public final T unwrap() {
            return this.getThis();
        }
        
        protected LootItemCondition[] getConditions() {
            return this.conditions.<LootItemCondition>toArray(new LootItemCondition[0]);
        }
        
        public AlternativesEntry.Builder otherwise(final Builder<?> a) {
            return new AlternativesEntry.Builder(new Builder[] { this, a });
        }
        
        public abstract LootPoolEntryContainer build();
    }
    
    public abstract static class Serializer<T extends LootPoolEntryContainer> {
        private final ResourceLocation name;
        private final Class<T> clazz;
        
        protected Serializer(final ResourceLocation sm, final Class<T> class2) {
            this.name = sm;
            this.clazz = class2;
        }
        
        public ResourceLocation getName() {
            return this.name;
        }
        
        public Class<T> getContainerClass() {
            return this.clazz;
        }
        
        public abstract void serialize(final JsonObject jsonObject, final T csq, final JsonSerializationContext jsonSerializationContext);
        
        public abstract T deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr);
    }
}
