Another fix for EntityProjectile (#826)

This commit is contained in:
Konstantin Shandurenko 2022-03-29 01:00:43 +03:00 committed by GitHub
parent 29a06487ce
commit 244f8785ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -159,8 +159,9 @@ public class EntityProjectile extends Entity {
.map(entity -> (LivingEntity) entity) .map(entity -> (LivingEntity) entity)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
final Point currentPos = pos;
Stream<LivingEntity> victimsStream = entities.stream() Stream<LivingEntity> victimsStream = entities.stream()
.filter(entity -> bb.intersectEntity(getPosition(), entity)); .filter(entity -> bb.intersectEntity(currentPos, entity));
/* /*
We won't check collisions with a shooter for first ticks of arrow's life, because it spawns in him We won't check collisions with a shooter for first ticks of arrow's life, because it spawns in him
and will immediately deal damage. and will immediately deal damage.

View File

@ -10,6 +10,7 @@ import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityProjectile; import net.minestom.server.entity.EntityProjectile;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.LivingEntity; import net.minestom.server.entity.LivingEntity;
import net.minestom.server.event.EventNode;
import net.minestom.server.event.entity.projectile.ProjectileCollideWithBlockEvent; import net.minestom.server.event.entity.projectile.ProjectileCollideWithBlockEvent;
import net.minestom.server.event.entity.projectile.ProjectileCollideWithEntityEvent; import net.minestom.server.event.entity.projectile.ProjectileCollideWithEntityEvent;
import net.minestom.server.event.entity.projectile.ProjectileUncollideEvent; import net.minestom.server.event.entity.projectile.ProjectileUncollideEvent;
@ -77,19 +78,31 @@ public class EntityProjectileCollisionIntegrationTest {
final Entity shooter = new Entity(EntityType.SKELETON); final Entity shooter = new Entity(EntityType.SKELETON);
shooter.setInstance(instance, new Pos(0, 40, 0)).join(); shooter.setInstance(instance, new Pos(0, 40, 0)).join();
for (double dx = 1; dx <= 3; dx += .2) {
singleEntityShoot(instance, shooter, new Vec(dx, 40, 0));
}
}
private void singleEntityShoot(
Instance instance,
Entity shooter,
final Point targetPosition
) {
final EntityProjectile projectile = new EntityProjectile(shooter, EntityType.ARROW); final EntityProjectile projectile = new EntityProjectile(shooter, EntityType.ARROW);
projectile.setInstance(instance, shooter.getPosition().withY(y -> y + shooter.getEyeHeight())).join(); projectile.setInstance(instance, shooter.getPosition().withY(y -> y + shooter.getEyeHeight())).join();
final Point targetPosition = new Vec(5, 40, 0); final LivingEntity target = new LivingEntity(EntityType.RABBIT);
final LivingEntity target = new LivingEntity(EntityType.ZOMBIE);
target.setInstance(instance, Pos.fromPoint(targetPosition)).join(); target.setInstance(instance, Pos.fromPoint(targetPosition)).join();
projectile.shoot(targetPosition, 1, 0); projectile.shoot(targetPosition, 1, 0);
final var eventRef = new AtomicReference<ProjectileCollideWithEntityEvent>(); final var eventRef = new AtomicReference<ProjectileCollideWithEntityEvent>();
MinecraftServer.getGlobalEventHandler().addListener(ProjectileCollideWithEntityEvent.class, event -> { final var eventNode = EventNode.all("projectile-test");
eventNode.addListener(ProjectileCollideWithEntityEvent.class, event -> {
event.getEntity().remove(); event.getEntity().remove();
eventRef.set(event); eventRef.set(event);
MinecraftServer.getGlobalEventHandler().removeChild(eventNode);
}); });
MinecraftServer.getGlobalEventHandler().addChild(eventNode);
final long tick = TimeUnit.SERVER_TICK.getDuration().toMillis(); final long tick = TimeUnit.SERVER_TICK.getDuration().toMillis();
for (int i = 0; i < MinecraftServer.TICK_PER_SECOND; ++i) { for (int i = 0; i < MinecraftServer.TICK_PER_SECOND; ++i) {
@ -99,9 +112,10 @@ public class EntityProjectileCollisionIntegrationTest {
} }
final var event = eventRef.get(); final var event = eventRef.get();
assertNotNull(event); assertNotNull(event, "Could not hit entity at " + targetPosition);
assertSame(target, event.getTarget()); assertSame(target, event.getTarget());
assertTrue(target.getBoundingBox().intersectEntity(target.getPosition(), projectile)); assertTrue(projectile.getBoundingBox().intersectEntity(event.getCollisionPosition(), target));
target.remove();
} }
@Test @Test