package net.minecraft.advancements.critereon;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonElement;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class LocationTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private final ResourceLocation id;
    
    public LocationTrigger(final ResourceLocation uh) {
        this.id = uh;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final JsonObject jsonObject2 = GsonHelper.getAsJsonObject(jsonObject, "location", jsonObject);
        final LocationPredicate bu6 = LocationPredicate.fromJson(jsonObject2);
        return new TriggerInstance(this.id, b, bu6);
    }
    
    public void trigger(final ServerPlayer ze) {
        this.trigger(ze, a -> a.matches(ze.getLevel(), ze.getX(), ze.getY(), ze.getZ()));
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final LocationPredicate location;
        
        public TriggerInstance(final ResourceLocation uh, final EntityPredicate.Composite b, final LocationPredicate bu) {
            super(uh, b);
            this.location = bu;
        }
        
        public static TriggerInstance located(final LocationPredicate bu) {
            return new TriggerInstance(CriteriaTriggers.LOCATION.id, EntityPredicate.Composite.ANY, bu);
        }
        
        public static TriggerInstance sleptInBed() {
            return new TriggerInstance(CriteriaTriggers.SLEPT_IN_BED.id, EntityPredicate.Composite.ANY, LocationPredicate.ANY);
        }
        
        public static TriggerInstance raidWon() {
            return new TriggerInstance(CriteriaTriggers.RAID_WIN.id, EntityPredicate.Composite.ANY, LocationPredicate.ANY);
        }
        
        public boolean matches(final ServerLevel zd, final double double2, final double double3, final double double4) {
            return this.location.matches(zd, double2, double3, double4);
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("location", this.location.serializeToJson());
            return jsonObject3;
        }
    }
}
