hasVelocity is now false if the entity just has gravity (#975)

This commit is contained in:
Weiiswurst 2022-04-28 16:06:53 +02:00 committed by GitHub
parent 4b0f10d448
commit afddb6f549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View File

@ -909,10 +909,16 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
/**
* Gets if the entity currently has a velocity applied.
*
* @return true if velocity is not set to 0
* @return true if the entity is moving
*/
public boolean hasVelocity() {
return !velocity.isZero();
if (isOnGround()) {
// if the entity is on the ground and only "moves" downwards, it does not have a velocity.
return Double.compare(velocity.x(), 0) != 0 || Double.compare(velocity.z(), 0) != 0 || velocity.y() > 0;
} else {
// The entity does not have velocity if the velocity is zero
return !velocity.isZero();
}
}
/**

View File

@ -8,8 +8,9 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.utils.chunk.ChunkUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
@EnvTest
public class EntityVelocityIntegrationTest {
@ -79,6 +80,8 @@ public class EntityVelocityIntegrationTest {
entity.takeKnockback(0.4f, 0, -1);
entity.takeKnockback(0.5f, 0, -1);
assertTrue(entity.hasVelocity());
testMovement(env, entity, new Vec[] {
new Vec(0.0, 40.0, 0.0),
new Vec(0.0, 40.4, 0.7000000029802322),
@ -102,6 +105,29 @@ public class EntityVelocityIntegrationTest {
});
}
@Test
public void testHasVelocity(Env env) {
var instance = env.createFlatInstance();
loadChunks(instance);
var entity = new Entity(EntityType.ZOMBIE);
// Should be false because the new entity should have no velocity
assertFalse(entity.hasVelocity());
entity.setInstance(instance, new Pos(0, 41, 0)).join();
env.tick();
// Should be true: The entity is currently falling (in the air), so it does have a velocity.
// Only entities on the ground should ignore the default velocity.
assertTrue(entity.hasVelocity());
env.tick();
// Now that the entity is on the ground, it should no longer have a velocity.
assertFalse(entity.hasVelocity());
}
private void testMovement(Env env, Entity entity, Vec[] sample) {
final double epsilon = 0.003;
for (Vec vec : sample) {