| 1 | package net.minecraft.advancements.predicates.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public record MovementPredicate(
|
| 13 | MinMaxBounds.Doubles x,
|
| 14 | MinMaxBounds.Doubles y,
|
| 15 | MinMaxBounds.Doubles z,
|
| 16 | MinMaxBounds.Doubles speed,
|
| 17 | MinMaxBounds.Doubles horizontalSpeed,
|
| 18 | MinMaxBounds.Doubles verticalSpeed,
|
| 19 | MinMaxBounds.Doubles fallDistance
|
| 20 | ) implements EntitySubPredicate {
|
| 21 | public static final Codec<MovementPredicate> CODEC = RecordCodecBuilder.create(
|
| 22 | i -> i.group(
|
| 23 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("x", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::x),
|
| 24 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("y", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::y),
|
| 25 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("z", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::z),
|
| 26 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("speed", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::speed),
|
| 27 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("horizontal_speed", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::horizontalSpeed),
|
| 28 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("vertical_speed", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::verticalSpeed),
|
| 29 | MinMaxBounds.Doubles.CODEC.optionalFieldOf("fall_distance", MinMaxBounds.Doubles.ANY).forGetter(MovementPredicate::fallDistance)
|
| 30 | )
|
| 31 | .apply(i, MovementPredicate::new)
|
| 32 | );
|
| 33 |
|
| 34 | public static MovementPredicate speed(final MinMaxBounds.Doubles bounds) {
|
| 35 | return new MovementPredicate(
|
| 36 | MinMaxBounds.Doubles.ANY,
|
| 37 | MinMaxBounds.Doubles.ANY,
|
| 38 | MinMaxBounds.Doubles.ANY,
|
| 39 | bounds,
|
| 40 | MinMaxBounds.Doubles.ANY,
|
| 41 | MinMaxBounds.Doubles.ANY,
|
| 42 | MinMaxBounds.Doubles.ANY
|
| 43 | );
|
| 44 | }
|
| 45 |
|
| 46 | public static MovementPredicate horizontalSpeed(final MinMaxBounds.Doubles bounds) {
|
| 47 | return new MovementPredicate(
|
| 48 | MinMaxBounds.Doubles.ANY,
|
| 49 | MinMaxBounds.Doubles.ANY,
|
| 50 | MinMaxBounds.Doubles.ANY,
|
| 51 | MinMaxBounds.Doubles.ANY,
|
| 52 | bounds,
|
| 53 | MinMaxBounds.Doubles.ANY,
|
| 54 | MinMaxBounds.Doubles.ANY
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | public static MovementPredicate verticalSpeed(final MinMaxBounds.Doubles bounds) {
|
| 59 | return new MovementPredicate(
|
| 60 | MinMaxBounds.Doubles.ANY,
|
| 61 | MinMaxBounds.Doubles.ANY,
|
| 62 | MinMaxBounds.Doubles.ANY,
|
| 63 | MinMaxBounds.Doubles.ANY,
|
| 64 | MinMaxBounds.Doubles.ANY,
|
| 65 | bounds,
|
| 66 | MinMaxBounds.Doubles.ANY
|
| 67 | );
|
| 68 | }
|
| 69 |
|
| 70 | public static MovementPredicate fallDistance(final MinMaxBounds.Doubles bounds) {
|
| 71 | return new MovementPredicate(
|
| 72 | MinMaxBounds.Doubles.ANY,
|
| 73 | MinMaxBounds.Doubles.ANY,
|
| 74 | MinMaxBounds.Doubles.ANY,
|
| 75 | MinMaxBounds.Doubles.ANY,
|
| 76 | MinMaxBounds.Doubles.ANY,
|
| 77 | MinMaxBounds.Doubles.ANY,
|
| 78 | bounds
|
| 79 | );
|
| 80 | }
|
| 81 |
|
| 82 | public boolean matches(final double x, final double y, final double z, final double fallDistance) {
|
| 83 | if (this.x.matches(x) && this.y.matches(y) && this.z.matches(z)) {
|
| 84 | double speedSqr = Mth.lengthSquared(x, y, z);
|
| 85 | if (!this.speed.matchesSqr(speedSqr)) {
|
| 86 | return false;
|
| 87 | }
|
| 88 |
|
| 89 | double horizontalSpeedSqr = Mth.lengthSquared(x, z);
|
| 90 | if (!this.horizontalSpeed.matchesSqr(horizontalSpeedSqr)) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | double verticalSpeed = Math.abs(y);
|
| 95 | return !this.verticalSpeed.matches(verticalSpeed) ? false : this.fallDistance.matches(fallDistance);
|
| 96 | } else {
|
| 97 | return false;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public boolean matches(final Entity entity, final ServerLevel level, final @Nullable Vec3 position) {
|
| 103 | Vec3 velocity = entity.getKnownMovement().scale(20.0);
|
| 104 | return this.matches(velocity.x, velocity.y, velocity.z, entity.fallDistance);
|
| 105 | }
|
| 106 | }
|
| 107 |
|