package net.minecraft.advancements.critereon;

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.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class BeeNestDestroyedTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return BeeNestDestroyedTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final Block bvr5 = deserializeBlock(jsonObject);
        final ItemPredicate bo6 = ItemPredicate.fromJson(jsonObject.get("item"));
        final MinMaxBounds.Ints d7 = MinMaxBounds.Ints.fromJson(jsonObject.get("num_bees_inside"));
        return new TriggerInstance(b, bvr5, bo6, d7);
    }
    
    @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 Block bvr, final ItemStack bki, final int integer) {
        this.trigger(ze, a -> a.matches(bvr, bki, integer));
    }
    
    static {
        ID = new ResourceLocation("bee_nest_destroyed");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        @Nullable
        private final Block block;
        private final ItemPredicate item;
        private final MinMaxBounds.Ints numBees;
        
        public TriggerInstance(final EntityPredicate.Composite b, @Nullable final Block bvr, final ItemPredicate bo, final MinMaxBounds.Ints d) {
            super(BeeNestDestroyedTrigger.ID, b);
            this.block = bvr;
            this.item = bo;
            this.numBees = d;
        }
        
        public static TriggerInstance destroyedBeeNest(final Block bvr, final ItemPredicate.Builder a, final MinMaxBounds.Ints d) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, bvr, a.build(), d);
        }
        
        public boolean matches(final Block bvr, final ItemStack bki, final int integer) {
            return (this.block == null || bvr == this.block) && this.item.matches(bki) && this.numBees.matches(integer);
        }
        
        @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("item", this.item.serializeToJson());
            jsonObject3.add("num_bees_inside", this.numBees.serializeToJson());
            return jsonObject3;
        }
    }
}
