package net.minecraft.advancements.critereon; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.util.Mth; public record DistancePredicate( MinMaxBounds.Doubles x, MinMaxBounds.Doubles y, MinMaxBounds.Doubles z, MinMaxBounds.Doubles horizontal, MinMaxBounds.Doubles absolute ) { public static final Codec CODEC = RecordCodecBuilder.create( instance -> instance.group( MinMaxBounds.Doubles.CODEC.optionalFieldOf("x", MinMaxBounds.Doubles.ANY).forGetter(DistancePredicate::x), MinMaxBounds.Doubles.CODEC.optionalFieldOf("y", MinMaxBounds.Doubles.ANY).forGetter(DistancePredicate::y), MinMaxBounds.Doubles.CODEC.optionalFieldOf("z", MinMaxBounds.Doubles.ANY).forGetter(DistancePredicate::z), MinMaxBounds.Doubles.CODEC.optionalFieldOf("horizontal", MinMaxBounds.Doubles.ANY).forGetter(DistancePredicate::horizontal), MinMaxBounds.Doubles.CODEC.optionalFieldOf("absolute", MinMaxBounds.Doubles.ANY).forGetter(DistancePredicate::absolute) ) .apply(instance, DistancePredicate::new) ); public static DistancePredicate horizontal(MinMaxBounds.Doubles horizontal) { return new DistancePredicate(MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, horizontal, MinMaxBounds.Doubles.ANY); } public static DistancePredicate vertical(MinMaxBounds.Doubles vertical) { return new DistancePredicate(MinMaxBounds.Doubles.ANY, vertical, MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY); } public static DistancePredicate absolute(MinMaxBounds.Doubles absolute) { return new DistancePredicate(MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, MinMaxBounds.Doubles.ANY, absolute); } public boolean matches(double x1, double y1, double z1, double x2, double y2, double z2) { float f = (float)(x1 - x2); float g = (float)(y1 - y2); float h = (float)(z1 - z2); if (!this.x.matches(Mth.abs(f)) || !this.y.matches(Mth.abs(g)) || !this.z.matches(Mth.abs(h))) { return false; } else { return !this.horizontal.matchesSqr(f * f + h * h) ? false : this.absolute.matchesSqr(f * f + g * g + h * h); } } }