package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.item.ItemStack;
import net.minecraft.server.level.ServerPlayer;
import javax.annotation.Nullable;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.Registry;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.level.block.Block;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class BeeNestDestroyedTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return BeeNestDestroyedTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final Block bph4 = deserializeBlock(jsonObject);
        final ItemPredicate bj5 = ItemPredicate.fromJson(jsonObject.get("item"));
        final MinMaxBounds.Ints d6 = MinMaxBounds.Ints.fromJson(jsonObject.get("num_bees_inside"));
        return new TriggerInstance(bph4, bj5, d6);
    }
    
    @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 Block bph, final ItemStack ben, final int integer) {
        this.trigger(xe.getAdvancements(), a -> a.matches(bph, ben, integer));
    }
    
    static {
        ID = new ResourceLocation("bee_nest_destroyed");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final Block block;
        private final ItemPredicate item;
        private final MinMaxBounds.Ints numBees;
        
        public TriggerInstance(final Block bph, final ItemPredicate bj, final MinMaxBounds.Ints d) {
            super(BeeNestDestroyedTrigger.ID);
            this.block = bph;
            this.item = bj;
            this.numBees = d;
        }
        
        public static TriggerInstance destroyedBeeNest(final Block bph, final ItemPredicate.Builder a, final MinMaxBounds.Ints d) {
            return new TriggerInstance(bph, a.build(), d);
        }
        
        public boolean matches(final Block bph, final ItemStack ben, final int integer) {
            return (this.block == null || bph == this.block) && this.item.matches(ben) && this.numBees.matches(integer);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            if (this.block != null) {
                jsonObject2.addProperty("block", Registry.BLOCK.getKey(this.block).toString());
            }
            jsonObject2.add("item", this.item.serializeToJson());
            jsonObject2.add("num_bees_inside", this.numBees.serializeToJson());
            return jsonObject2;
        }
    }
}
