package net.minecraft.advancements.critereon;

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

public class LocationTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private final ResourceLocation id;
    
    public LocationTrigger(final ResourceLocation sm) {
        this.id = sm;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final LocationPredicate bp4 = LocationPredicate.fromJson(jsonObject);
        return new TriggerInstance(this.id, bp4);
    }
    
    public void trigger(final ServerPlayer xe) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe.getLevel(), xe.getX(), xe.getY(), xe.getZ()));
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final LocationPredicate location;
        
        public TriggerInstance(final ResourceLocation sm, final LocationPredicate bp) {
            super(sm);
            this.location = bp;
        }
        
        public static TriggerInstance located(final LocationPredicate bp) {
            return new TriggerInstance(CriteriaTriggers.LOCATION.id, bp);
        }
        
        public static TriggerInstance sleptInBed() {
            return new TriggerInstance(CriteriaTriggers.SLEPT_IN_BED.id, LocationPredicate.ANY);
        }
        
        public static TriggerInstance raidWon() {
            return new TriggerInstance(CriteriaTriggers.RAID_WIN.id, LocationPredicate.ANY);
        }
        
        public boolean matches(final ServerLevel xd, final double double2, final double double3, final double double4) {
            return this.location.matches(xd, double2, double3, double4);
        }
        
        @Override
        public JsonElement serializeToJson() {
            return this.location.serializeToJson();
        }
    }
}
