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

import net.minecraft.world.level.block.state.StateDefinition;
import com.google.gson.JsonSyntaxException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import net.minecraft.core.Registry;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.Serializer;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.block.state.BlockState;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import net.minecraft.advancements.critereon.StatePropertiesPredicate;
import net.minecraft.world.level.block.Block;

public class LootItemBlockStatePropertyCondition implements LootItemCondition {
    private final Block block;
    private final StatePropertiesPredicate properties;
    
    private LootItemBlockStatePropertyCondition(final Block bvr, final StatePropertiesPredicate ck) {
        this.block = bvr;
        this.properties = ck;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.BLOCK_STATE_PROPERTY;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_STATE);
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final BlockState cfj3 = dat.<BlockState>getParamOrNull(LootContextParams.BLOCK_STATE);
        return cfj3 != null && this.block == cfj3.getBlock() && this.properties.matches(cfj3);
    }
    
    public static Builder hasBlockStateProperties(final Block bvr) {
        return new Builder(bvr);
    }
    
    public static class Builder implements LootItemCondition.Builder {
        private final Block block;
        private StatePropertiesPredicate properties;
        
        public Builder(final Block bvr) {
            this.properties = StatePropertiesPredicate.ANY;
            this.block = bvr;
        }
        
        public Builder setProperties(final StatePropertiesPredicate.Builder a) {
            this.properties = a.build();
            return this;
        }
        
        @Override
        public LootItemCondition build() {
            return new LootItemBlockStatePropertyCondition(this.block, this.properties, null);
        }
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<LootItemBlockStatePropertyCondition> {
        @Override
        public void serialize(final JsonObject jsonObject, final LootItemBlockStatePropertyCondition ddl, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.addProperty("block", Registry.BLOCK.getKey(ddl.block).toString());
            jsonObject.add("properties", ddl.properties.serializeToJson());
        }
        
        @Override
        public LootItemBlockStatePropertyCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final ResourceLocation uh4 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "block"));
            final Object o;
            final Object obj;
            final Block bvr5 = Registry.BLOCK.getOptional(uh4).<Throwable>orElseThrow(() -> {
                new IllegalArgumentException("Can't find block " + obj);
                return o;
            });
            final StatePropertiesPredicate ck6 = StatePropertiesPredicate.fromJson(jsonObject.get("properties"));
            final JsonSyntaxException ex;
            final Object obj2;
            ck6.checkState(bvr5.getStateDefinition(), string -> {
                new JsonSyntaxException("Block " + obj2 + " has no property " + string);
                throw ex;
            });
            return new LootItemBlockStatePropertyCondition(bvr5, ck6, null);
        }
    }
}
