package net.minecraft.advancements.critereon;

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

public class NetherTravelTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return NetherTravelTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final LocationPredicate bp4 = LocationPredicate.fromJson(jsonObject.get("entered"));
        final LocationPredicate bp5 = LocationPredicate.fromJson(jsonObject.get("exited"));
        final DistancePredicate at6 = DistancePredicate.fromJson(jsonObject.get("distance"));
        return new TriggerInstance(bp4, bp5, at6);
    }
    
    public void trigger(final ServerPlayer xe, final Vec3 cvl) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe.getLevel(), cvl, xe.getX(), xe.getY(), xe.getZ()));
    }
    
    static {
        ID = new ResourceLocation("nether_travel");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final LocationPredicate entered;
        private final LocationPredicate exited;
        private final DistancePredicate distance;
        
        public TriggerInstance(final LocationPredicate bp1, final LocationPredicate bp2, final DistancePredicate at) {
            super(NetherTravelTrigger.ID);
            this.entered = bp1;
            this.exited = bp2;
            this.distance = at;
        }
        
        public static TriggerInstance travelledThroughNether(final DistancePredicate at) {
            return new TriggerInstance(LocationPredicate.ANY, LocationPredicate.ANY, at);
        }
        
        public boolean matches(final ServerLevel xd, final Vec3 cvl, final double double3, final double double4, final double double5) {
            return this.entered.matches(xd, cvl.x, cvl.y, cvl.z) && this.exited.matches(xd, double3, double4, double5) && this.distance.matches(cvl.x, cvl.y, cvl.z, double3, double4, double5);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("entered", this.entered.serializeToJson());
            jsonObject2.add("exited", this.exited.serializeToJson());
            jsonObject2.add("distance", this.distance.serializeToJson());
            return jsonObject2;
        }
    }
}
