package net.minecraft.advancements.critereon;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.BlockPos;
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 PlacedBlockTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return PlacedBlockTrigger.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;
            });
        }
        final LocationPredicate bu7 = LocationPredicate.fromJson(jsonObject.get("location"));
        final ItemPredicate bo8 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(b, bvr5, ck6, bu7, bo8);
    }
    
    @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 BlockPos fu, final ItemStack bki) {
        final BlockState cfj5 = ze.getLevel().getBlockState(fu);
        this.trigger(ze, a -> a.matches(cfj5, fu, ze.getLevel(), bki));
    }
    
    static {
        ID = new ResourceLocation("placed_block");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final Block block;
        private final StatePropertiesPredicate state;
        private final LocationPredicate location;
        private final ItemPredicate item;
        
        public TriggerInstance(final EntityPredicate.Composite b, @Nullable final Block bvr, final StatePropertiesPredicate ck, final LocationPredicate bu, final ItemPredicate bo) {
            super(PlacedBlockTrigger.ID, b);
            this.block = bvr;
            this.state = ck;
            this.location = bu;
            this.item = bo;
        }
        
        public static TriggerInstance placedBlock(final Block bvr) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, bvr, StatePropertiesPredicate.ANY, LocationPredicate.ANY, ItemPredicate.ANY);
        }
        
        public boolean matches(final BlockState cfj, final BlockPos fu, final ServerLevel zd, final ItemStack bki) {
            return (this.block == null || cfj.is(this.block)) && this.state.matches(cfj) && this.location.matches(zd, (float)fu.getX(), (float)fu.getY(), (float)fu.getZ()) && this.item.matches(bki);
        }
        
        @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());
            jsonObject3.add("location", this.location.serializeToJson());
            jsonObject3.add("item", this.item.serializeToJson());
            return jsonObject3;
        }
    }
}
