mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-13 19:51:27 +01:00
Add test for entity tracking registration
This commit is contained in:
parent
18814f77e2
commit
91eb68661c
@ -18,6 +18,9 @@ import java.util.Set;
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
public sealed interface EntityTracker permits EntityTrackerImpl {
|
||||
static @NotNull EntityTracker newTracker() {
|
||||
return new EntityTrackerImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an entity to be tracked.
|
||||
|
@ -40,7 +40,10 @@ final class EntityTrackerImpl implements EntityTracker {
|
||||
}
|
||||
}
|
||||
if (update != null) {
|
||||
visibleEntities(point, target, update::add);
|
||||
visibleEntities(point, target, newEntity -> {
|
||||
if (newEntity == entity) return;
|
||||
update.add(newEntity);
|
||||
});
|
||||
update.updateTracker(point, this);
|
||||
}
|
||||
}
|
||||
|
@ -98,8 +98,6 @@ public final class ViewEngine {
|
||||
}
|
||||
|
||||
private void handleAutoView(Entity entity, Consumer<Entity> viewer, Consumer<Player> viewable) {
|
||||
if (this.entity == entity)
|
||||
return; // Ensure that self isn't added or removed as viewer
|
||||
if (entity.getVehicle() != null)
|
||||
return; // Passengers are handled by the vehicle, inheriting its viewing settings
|
||||
if (this.entity instanceof Player && viewerOption.isAuto() && entity.isAutoViewable()) {
|
||||
|
55
src/test/java/instance/EntityTrackerTest.java
Normal file
55
src/test/java/instance/EntityTrackerTest.java
Normal file
@ -0,0 +1,55 @@
|
||||
package instance;
|
||||
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.coordinate.Vec;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.instance.EntityTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class EntityTrackerTest {
|
||||
@Test
|
||||
public void register() {
|
||||
var ent1 = new Entity(EntityType.ZOMBIE);
|
||||
var updated = new EntityTracker.Update<>() {
|
||||
@Override
|
||||
public void add(@NotNull Entity entity) {
|
||||
assertNotSame(ent1, entity);
|
||||
fail("No other entity should be registered yet");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(@NotNull Entity entity) {
|
||||
assertNotSame(ent1, entity);
|
||||
fail("No other entity should be registered yet");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTracker(@NotNull Point point, @Nullable EntityTracker tracker) {
|
||||
// Empty
|
||||
}
|
||||
};
|
||||
EntityTracker tracker = EntityTracker.newTracker();
|
||||
var chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
||||
assertTrue(chunkEntities.isEmpty());
|
||||
|
||||
tracker.register(ent1, Vec.ZERO, EntityTracker.Target.ENTITIES, updated);
|
||||
assertTrue(chunkEntities.isEmpty());
|
||||
|
||||
chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
||||
assertEquals(1, chunkEntities.size());
|
||||
|
||||
tracker.unregister(ent1, Vec.ZERO, EntityTracker.Target.ENTITIES, updated);
|
||||
chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
||||
assertEquals(0, chunkEntities.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void move() {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user