package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.advancements.CriteriaTriggers;
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 com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class ItemUsedOnBlockTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private final ResourceLocation id;
    
    public ItemUsedOnBlockTrigger(final ResourceLocation sm) {
        this.id = sm;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final BlockPredicate aj4 = BlockPredicate.fromJson(jsonObject.get("block"));
        final StatePropertiesPredicate cc5 = StatePropertiesPredicate.fromJson(jsonObject.get("state"));
        final ItemPredicate bj6 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(this.id, aj4, cc5, bj6);
    }
    
    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, xe.getLevel(), fk, ben));
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final BlockPredicate block;
        private final StatePropertiesPredicate state;
        private final ItemPredicate item;
        
        public TriggerInstance(final ResourceLocation sm, final BlockPredicate aj, final StatePropertiesPredicate cc, final ItemPredicate bj) {
            super(sm);
            this.block = aj;
            this.state = cc;
            this.item = bj;
        }
        
        public static TriggerInstance safelyHarvestedHoney(final BlockPredicate.Builder a, final ItemPredicate.Builder a) {
            return new TriggerInstance(CriteriaTriggers.SAFELY_HARVEST_HONEY.id, a.build(), StatePropertiesPredicate.ANY, a.build());
        }
        
        public boolean matches(final BlockState byj, final ServerLevel xd, final BlockPos fk, final ItemStack ben) {
            return this.block.matches(xd, fk) && this.state.matches(byj) && this.item.matches(ben);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("block", this.block.serializeToJson());
            jsonObject2.add("state", this.state.serializeToJson());
            jsonObject2.add("item", this.item.serializeToJson());
            return jsonObject2;
        }
    }
}
