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

import net.minecraft.world.level.block.state.StateDefinition;
import com.google.gson.JsonSyntaxException;
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.resources.ResourceLocation;
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 bph, final StatePropertiesPredicate cc) {
        this.block = bph;
        this.properties = cc;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_STATE);
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final BlockState byj3 = crx.<BlockState>getParamOrNull(LootContextParams.BLOCK_STATE);
        return byj3 != null && this.block == byj3.getBlock() && this.properties.matches(byj3);
    }
    
    public static Builder hasBlockStateProperties(final Block bph) {
        return new Builder(bph);
    }
    
    public static class Builder implements LootItemCondition.Builder {
        private final Block block;
        private StatePropertiesPredicate properties;
        
        public Builder(final Block bph) {
            this.properties = StatePropertiesPredicate.ANY;
            this.block = bph;
        }
        
        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 extends LootItemCondition.Serializer<LootItemBlockStatePropertyCondition> {
        protected Serializer() {
            super(new ResourceLocation("block_state_property"), LootItemBlockStatePropertyCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final LootItemBlockStatePropertyCondition cul, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.addProperty("block", Registry.BLOCK.getKey(cul.block).toString());
            jsonObject.add("properties", cul.properties.serializeToJson());
        }
        
        @Override
        public LootItemBlockStatePropertyCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final ResourceLocation sm4 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "block"));
            final Object o;
            final Object obj;
            final Block bph5 = Registry.BLOCK.getOptional(sm4).<Throwable>orElseThrow(() -> {
                new IllegalArgumentException("Can't find block " + obj);
                return o;
            });
            final StatePropertiesPredicate cc6 = StatePropertiesPredicate.fromJson(jsonObject.get("properties"));
            final JsonSyntaxException ex;
            final Object obj2;
            cc6.checkState(bph5.getStateDefinition(), string -> {
                new JsonSyntaxException("Block " + obj2 + " has no property " + string);
                throw ex;
            });
            return new LootItemBlockStatePropertyCondition(bph5, cc6, null);
        }
    }
}
