package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.advancements.CriterionTriggerInstance;
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.JsonDeserializationContext;
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;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final Block bph4 = deserializeBlock(jsonObject);
        final StatePropertiesPredicate cc5 = StatePropertiesPredicate.fromJson(jsonObject.get("state"));
        if (bph4 != null) {
            final JsonSyntaxException ex;
            final Object obj;
            cc5.checkState(bph4.getStateDefinition(), string -> {
                new JsonSyntaxException("Block " + obj + " has no property " + string + ":");
                throw ex;
            });
        }
        final LocationPredicate bp6 = LocationPredicate.fromJson(jsonObject.get("location"));
        final ItemPredicate bj7 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(bph4, cc5, bp6, bj7);
    }
    
    @Nullable
    private static Block deserializeBlock(final JsonObject jsonObject) {
        if (jsonObject.has("block")) {
            final ResourceLocation sm2 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "block"));
            final Object o;
            final Object obj;
            return Registry.BLOCK.getOptional(sm2).<Throwable>orElseThrow(() -> {
                new JsonSyntaxException("Unknown block type '" + obj + "'");
                return o;
            });
        }
        return null;
    }
    
    public void trigger(final ServerPlayer xe, final BlockPos fk, final ItemStack ben) {
        final BlockState byj5 = xe.getLevel().getBlockState(fk);
        this.trigger(xe.getAdvancements(), a -> a.matches(byj5, fk, xe.getLevel(), ben));
    }
    
    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(@Nullable final Block bph, final StatePropertiesPredicate cc, final LocationPredicate bp, final ItemPredicate bj) {
            super(PlacedBlockTrigger.ID);
            this.block = bph;
            this.state = cc;
            this.location = bp;
            this.item = bj;
        }
        
        public static TriggerInstance placedBlock(final Block bph) {
            return new TriggerInstance(bph, StatePropertiesPredicate.ANY, LocationPredicate.ANY, ItemPredicate.ANY);
        }
        
        public boolean matches(final BlockState byj, final BlockPos fk, final ServerLevel xd, final ItemStack ben) {
            return (this.block == null || byj.getBlock() == this.block) && this.state.matches(byj) && this.location.matches(xd, (float)fk.getX(), (float)fk.getY(), (float)fk.getZ()) && this.item.matches(ben);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            if (this.block != null) {
                jsonObject2.addProperty("block", Registry.BLOCK.getKey(this.block).toString());
            }
            jsonObject2.add("state", this.state.serializeToJson());
            jsonObject2.add("location", this.location.serializeToJson());
            jsonObject2.add("item", this.item.serializeToJson());
            return jsonObject2;
        }
    }
}
