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

public class ItemUsedOnBlockTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return ItemUsedOnBlockTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final LocationPredicate bu5 = LocationPredicate.fromJson(jsonObject.get("location"));
        final ItemPredicate bo6 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(b, bu5, bo6);
    }
    
    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, ze.getLevel(), fu, bki));
    }
    
    static {
        ID = new ResourceLocation("item_used_on_block");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final LocationPredicate location;
        private final ItemPredicate item;
        
        public TriggerInstance(final EntityPredicate.Composite b, final LocationPredicate bu, final ItemPredicate bo) {
            super(ItemUsedOnBlockTrigger.ID, b);
            this.location = bu;
            this.item = bo;
        }
        
        public static TriggerInstance itemUsedOnBlock(final LocationPredicate.Builder a, final ItemPredicate.Builder a) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, a.build(), a.build());
        }
        
        public boolean matches(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final ItemStack bki) {
            return this.location.matches(zd, fu.getX() + 0.5, fu.getY() + 0.5, fu.getZ() + 0.5) && this.item.matches(bki);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("location", this.location.serializeToJson());
            jsonObject3.add("item", this.item.serializeToJson());
            return jsonObject3;
        }
    }
}
