mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-14 04:02:00 +01:00
Ensure unmodifiable view
This commit is contained in:
parent
2c7aae6bec
commit
5d4dd1f9f4
@ -119,7 +119,8 @@ final class EntityTrackerImpl implements EntityTracker {
|
|||||||
public @Unmodifiable <T extends Entity> Collection<T> chunkEntities(int chunkX, int chunkZ, @NotNull Target<T> target) {
|
public @Unmodifiable <T extends Entity> Collection<T> chunkEntities(int chunkX, int chunkZ, @NotNull Target<T> target) {
|
||||||
final TargetEntry<Entity> entry = entries[target.ordinal()];
|
final TargetEntry<Entity> entry = entries[target.ordinal()];
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return (Collection<T>) entry.chunkEntities.computeIfAbsent(getChunkIndex(chunkX, chunkZ), i -> LIST_SUPPLIER.get());
|
var chunkEntities = (List<T>) entry.chunkEntities.computeIfAbsent(getChunkIndex(chunkX, chunkZ), i -> LIST_SUPPLIER.get());
|
||||||
|
return Collections.unmodifiableList(chunkEntities);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -31,13 +31,9 @@ public class EntityTrackerTest {
|
|||||||
assertTrue(chunkEntities.isEmpty());
|
assertTrue(chunkEntities.isEmpty());
|
||||||
|
|
||||||
tracker.register(ent1, Vec.ZERO, EntityTracker.Target.ENTITIES, updater);
|
tracker.register(ent1, Vec.ZERO, EntityTracker.Target.ENTITIES, updater);
|
||||||
assertTrue(chunkEntities.isEmpty());
|
|
||||||
|
|
||||||
chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
|
||||||
assertEquals(1, chunkEntities.size());
|
assertEquals(1, chunkEntities.size());
|
||||||
|
|
||||||
tracker.unregister(ent1, EntityTracker.Target.ENTITIES, updater);
|
tracker.unregister(ent1, EntityTracker.Target.ENTITIES, updater);
|
||||||
chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
|
||||||
assertEquals(0, chunkEntities.size());
|
assertEquals(0, chunkEntities.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,4 +120,35 @@ public class EntityTrackerTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void collectionView() {
|
||||||
|
var ent1 = new Entity(EntityType.ZOMBIE);
|
||||||
|
var updater = 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");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
EntityTracker tracker = EntityTracker.newTracker();
|
||||||
|
var entities = tracker.entities();
|
||||||
|
var chunkEntities = tracker.chunkEntities(Vec.ZERO, EntityTracker.Target.ENTITIES);
|
||||||
|
|
||||||
|
assertTrue(entities.isEmpty());
|
||||||
|
assertTrue(chunkEntities.isEmpty());
|
||||||
|
tracker.register(ent1, Vec.ZERO, EntityTracker.Target.ENTITIES, updater);
|
||||||
|
assertEquals(1, entities.size());
|
||||||
|
assertEquals(1, chunkEntities.size());
|
||||||
|
|
||||||
|
assertThrows(Exception.class, () -> entities.add(new Entity(EntityType.ZOMBIE)));
|
||||||
|
assertThrows(Exception.class, () -> chunkEntities.add(new Entity(EntityType.ZOMBIE)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user