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

import net.minecraft.util.GsonHelper;
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.ValidationContext;
import net.minecraft.world.level.storage.loot.LootContext;
import java.util.function.Predicate;

public class AlternativeLootItemCondition implements LootItemCondition {
    private final LootItemCondition[] terms;
    private final Predicate<LootContext> composedPredicate;
    
    private AlternativeLootItemCondition(final LootItemCondition[] arr) {
        this.terms = arr;
        this.composedPredicate = LootItemConditions.<LootContext>orConditions(arr);
    }
    
    @Override
    public final boolean test(final LootContext crx) {
        return this.composedPredicate.test(crx);
    }
    
    @Override
    public void validate(final ValidationContext csg) {
        super.validate(csg);
        for (int integer3 = 0; integer3 < this.terms.length; ++integer3) {
            this.terms[integer3].validate(csg.forChild(".term[" + integer3 + "]"));
        }
    }
    
    public static Builder alternative(final LootItemCondition.Builder... arr) {
        return new Builder(arr);
    }
    
    public static class Builder implements LootItemCondition.Builder {
        private final List<LootItemCondition> terms;
        
        public Builder(final LootItemCondition.Builder... arr) {
            this.terms = Lists.newArrayList();
            for (final LootItemCondition.Builder a6 : arr) {
                this.terms.add(a6.build());
            }
        }
        
        @Override
        public Builder or(final LootItemCondition.Builder a) {
            this.terms.add(a.build());
            return this;
        }
        
        @Override
        public LootItemCondition build() {
            return new AlternativeLootItemCondition(this.terms.<LootItemCondition>toArray(new LootItemCondition[0]), null);
        }
    }
    
    public static class Serializer extends LootItemCondition.Serializer<AlternativeLootItemCondition> {
        public Serializer() {
            super(new ResourceLocation("alternative"), AlternativeLootItemCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final AlternativeLootItemCondition cuc, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("terms", jsonSerializationContext.serialize(cuc.terms));
        }
        
        @Override
        public AlternativeLootItemCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final LootItemCondition[] arr4 = GsonHelper.<LootItemCondition[]>getAsObject(jsonObject, "terms", jsonDeserializationContext, LootItemCondition[].class);
            return new AlternativeLootItemCondition(arr4, null);
        }
    }
}
