package net.minecraft.advancements.critereon;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.server.level.ServerPlayer;
import javax.annotation.Nullable;
import net.minecraft.core.Registry;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.StateDefinition;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class SlideDownBlockTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return SlideDownBlockTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final Block bvr5 = deserializeBlock(jsonObject);
        final StatePropertiesPredicate ck6 = StatePropertiesPredicate.fromJson(jsonObject.get("state"));
        if (bvr5 != null) {
            final JsonSyntaxException ex;
            final Object obj;
            ck6.checkState(bvr5.getStateDefinition(), string -> {
                new JsonSyntaxException("Block " + obj + " has no property " + string);
                throw ex;
            });
        }
        return new TriggerInstance(b, bvr5, ck6);
    }
    
    @Nullable
    private static Block deserializeBlock(final JsonObject jsonObject) {
        if (jsonObject.has("block")) {
            final ResourceLocation uh2 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "block"));
            final Object o;
            final Object obj;
            return Registry.BLOCK.getOptional(uh2).<Throwable>orElseThrow(() -> {
                new JsonSyntaxException("Unknown block type '" + obj + "'");
                return o;
            });
        }
        return null;
    }
    
    public void trigger(final ServerPlayer ze, final BlockState cfj) {
        this.trigger(ze, a -> a.matches(cfj));
    }
    
    static {
        ID = new ResourceLocation("slide_down_block");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final Block block;
        private final StatePropertiesPredicate state;
        
        public TriggerInstance(final EntityPredicate.Composite b, @Nullable final Block bvr, final StatePropertiesPredicate ck) {
            super(SlideDownBlockTrigger.ID, b);
            this.block = bvr;
            this.state = ck;
        }
        
        public static TriggerInstance slidesDownBlock(final Block bvr) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, bvr, StatePropertiesPredicate.ANY);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            if (this.block != null) {
                jsonObject3.addProperty("block", Registry.BLOCK.getKey(this.block).toString());
            }
            jsonObject3.add("state", this.state.serializeToJson());
            return jsonObject3;
        }
        
        public boolean matches(final BlockState cfj) {
            return (this.block == null || cfj.is(this.block)) && this.state.matches(cfj);
        }
    }
}
