package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
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 LevitationTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return LevitationTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final DistancePredicate at4 = DistancePredicate.fromJson(jsonObject.get("distance"));
        final MinMaxBounds.Ints d5 = MinMaxBounds.Ints.fromJson(jsonObject.get("duration"));
        return new TriggerInstance(at4, d5);
    }
    
    public void trigger(final ServerPlayer xe, final Vec3 cvl, final int integer) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, cvl, integer));
    }
    
    static {
        ID = new ResourceLocation("levitation");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final DistancePredicate distance;
        private final MinMaxBounds.Ints duration;
        
        public TriggerInstance(final DistancePredicate at, final MinMaxBounds.Ints d) {
            super(LevitationTrigger.ID);
            this.distance = at;
            this.duration = d;
        }
        
        public static TriggerInstance levitated(final DistancePredicate at) {
            return new TriggerInstance(at, MinMaxBounds.Ints.ANY);
        }
        
        public boolean matches(final ServerPlayer xe, final Vec3 cvl, final int integer) {
            return this.distance.matches(cvl.x, cvl.y, cvl.z, xe.getX(), xe.getY(), xe.getZ()) && this.duration.matches(integer);
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("distance", this.distance.serializeToJson());
            jsonObject2.add("duration", this.duration.serializeToJson());
            return jsonObject2;
        }
    }
}
