Search in sources :

Example 1 with OnChunkLoaded

use of org.terasology.engine.world.chunks.event.OnChunkLoaded in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testGenerateSingleChunkWithBlockLifeCycle.

@Test
void testGenerateSingleChunkWithBlockLifeCycle() throws InterruptedException, ExecutionException, TimeoutException {
    Vector3i chunkPosition = new Vector3i(0, 0, 0);
    blockAtBlockManager.setLifecycleEventsRequired(true);
    blockAtBlockManager.setEntity(mock(EntityRef.class));
    requestCreatingOrLoadingArea(chunkPosition).get(WAIT_CHUNK_IS_READY_IN_SECONDS, TimeUnit.SECONDS);
    chunkProvider.update();
    final ArgumentCaptor<Event> worldEventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(worldEntity, atLeast(2)).send(worldEventCaptor.capture());
    Assertions.assertAll("World Events not valid", () -> {
        Event mustBeOnGeneratedEvent = worldEventCaptor.getAllValues().get(0);
        Assertions.assertTrue(mustBeOnGeneratedEvent instanceof OnChunkGenerated, "First world event must be OnChunkGenerated");
        Assertions.assertEquals(((OnChunkGenerated) mustBeOnGeneratedEvent).getChunkPos(), chunkPosition, "Chunk position at event not expected");
    }, () -> {
        Event mustBeOnLoadedEvent = worldEventCaptor.getAllValues().get(1);
        Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded");
        Assertions.assertEquals(chunkPosition, ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected");
    });
    // TODO, it is not clear if the activate/addedBlocks event logic is correct.
    // See https://github.com/MovingBlocks/Terasology/issues/3244
    final ArgumentCaptor<Event> blockEventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockAtBlockManager.getEntity(), atLeast(1)).send(blockEventCaptor.capture());
    Event mustBeOnActivatedBlocks = blockEventCaptor.getAllValues().get(0);
    Assertions.assertTrue(mustBeOnActivatedBlocks instanceof OnActivatedBlocks, "First block event must be OnActivatedBlocks");
    Assertions.assertTrue(((OnActivatedBlocks) mustBeOnActivatedBlocks).blockCount() > 0, "Block count on activate must be non zero");
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 2 with OnChunkLoaded

use of org.terasology.engine.world.chunks.event.OnChunkLoaded in project Terasology by MovingBlocks.

the class SectorSimulationSystem method chunkLoad.

/**
 * Handles the OnChunkLoaded event for sector entities.
 *
 * Forwards the event to the appropriate sector-scope entities, if they are watching that chunk, and sends a
 * {@link SectorEntityLoad} event if this is the first watched chunk to be loaded for that entity.
 *
 * @param event the event sent when any chunk is loaded
 * @param worldEntity ignored
 */
@ReceiveEvent(components = WorldComponent.class)
public void chunkLoad(OnChunkLoaded event, EntityRef worldEntity) {
    for (EntityRef entity : entityManager.getEntitiesWith(SectorSimulationComponent.class)) {
        if (SectorUtil.getWatchedChunks(entity).contains(event.getChunkPos())) {
            entity.send(new OnChunkLoaded(event.getChunkPos()));
            if (SectorUtil.onlyWatchedChunk(entity, event.getChunkPos(), chunkProvider)) {
                entity.send(new SectorEntityLoad());
            }
            sendLoadedSectorUpdateEvent(entity, simulationDelta(entity));
        }
    }
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BaseEntityRef(org.terasology.engine.entitySystem.entity.internal.BaseEntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 3 with OnChunkLoaded

use of org.terasology.engine.world.chunks.event.OnChunkLoaded in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testLoadSingleChunk.

@Test
void testLoadSingleChunk() throws InterruptedException, ExecutionException, TimeoutException {
    Vector3i chunkPosition = new Vector3i(0, 0, 0);
    Chunk chunk = new ChunkImpl(chunkPosition, blockManager, extraDataManager);
    generator.createChunk(chunk, null);
    storageManager.add(chunk);
    requestCreatingOrLoadingArea(chunkPosition).get(WAIT_CHUNK_IS_READY_IN_SECONDS, TimeUnit.SECONDS);
    chunkProvider.update();
    Assertions.assertTrue(((TestChunkStore) storageManager.loadChunkStore(chunkPosition)).isEntityRestored(), "Entities must be restored by loading");
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(worldEntity, atLeast(1)).send(eventArgumentCaptor.capture());
    Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(0);
    Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded");
    Assertions.assertEquals(chunkPosition, ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected");
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) Chunk(org.terasology.engine.world.chunks.Chunk) Test(org.junit.jupiter.api.Test)

Example 4 with OnChunkLoaded

use of org.terasology.engine.world.chunks.event.OnChunkLoaded in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testGenerateSingleChunk.

@Test
void testGenerateSingleChunk() throws InterruptedException, ExecutionException, TimeoutException {
    Vector3i chunkPosition = new Vector3i(0, 0, 0);
    requestCreatingOrLoadingArea(chunkPosition).get(WAIT_CHUNK_IS_READY_IN_SECONDS, TimeUnit.SECONDS);
    chunkProvider.update();
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(worldEntity, atLeast(2)).send(eventArgumentCaptor.capture());
    Assertions.assertAll("WorldEvents not valid", () -> {
        Event mustBeOnGeneratedEvent = eventArgumentCaptor.getAllValues().get(0);
        Assertions.assertTrue(mustBeOnGeneratedEvent instanceof OnChunkGenerated, "First world event must be OnChunkGenerated");
        Assertions.assertEquals(((OnChunkGenerated) mustBeOnGeneratedEvent).getChunkPos(), chunkPosition, "Chunk position at event not expected");
    }, () -> {
        Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(1);
        Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded");
        Assertions.assertEquals(chunkPosition, ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected");
    });
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) Test(org.junit.jupiter.api.Test)

Example 5 with OnChunkLoaded

use of org.terasology.engine.world.chunks.event.OnChunkLoaded in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testLoadSingleChunkWithBlockLifecycle.

@Test
void testLoadSingleChunkWithBlockLifecycle() throws InterruptedException, ExecutionException, TimeoutException {
    Vector3i chunkPosition = new Vector3i(0, 0, 0);
    Chunk chunk = new ChunkImpl(chunkPosition, blockManager, extraDataManager);
    generator.createChunk(chunk, null);
    storageManager.add(chunk);
    blockAtBlockManager.setLifecycleEventsRequired(true);
    blockAtBlockManager.setEntity(mock(EntityRef.class));
    requestCreatingOrLoadingArea(chunkPosition).get(WAIT_CHUNK_IS_READY_IN_SECONDS, TimeUnit.SECONDS);
    chunkProvider.update();
    Assertions.assertTrue(((TestChunkStore) storageManager.loadChunkStore(chunkPosition)).isEntityRestored(), "Entities must be restored by loading");
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(worldEntity, atLeast(1)).send(eventArgumentCaptor.capture());
    Event mustBeOnLoadedEvent = eventArgumentCaptor.getAllValues().get(0);
    Assertions.assertTrue(mustBeOnLoadedEvent instanceof OnChunkLoaded, "Second world event must be OnChunkLoaded");
    Assertions.assertEquals(chunkPosition, ((OnChunkLoaded) mustBeOnLoadedEvent).getChunkPos(), "Chunk position at event not expected");
    // TODO, it is not clear if the activate/addedBlocks event logic is correct.
    // See https://github.com/MovingBlocks/Terasology/issues/3244
    final ArgumentCaptor<Event> blockEventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockAtBlockManager.getEntity(), atLeast(2)).send(blockEventCaptor.capture());
    Assertions.assertAll("Block events not valid", () -> {
        Event mustBeOnAddedBlocks = blockEventCaptor.getAllValues().get(0);
        Assertions.assertTrue(mustBeOnAddedBlocks instanceof OnAddedBlocks, "First block event must be OnAddedBlocks");
        Assertions.assertTrue(((OnAddedBlocks) mustBeOnAddedBlocks).blockCount() > 0, "Block count on activate must be non zero");
    }, () -> {
        Event mustBeOnActivatedBlocks = blockEventCaptor.getAllValues().get(1);
        Assertions.assertTrue(mustBeOnActivatedBlocks instanceof OnActivatedBlocks, "First block event must be OnActivatedBlocks");
        Assertions.assertTrue(((OnActivatedBlocks) mustBeOnActivatedBlocks).blockCount() > 0, "Block count on activate must be non zero");
    });
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) OnAddedBlocks(org.terasology.engine.world.block.OnAddedBlocks) Chunk(org.terasology.engine.world.chunks.Chunk) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Aggregations

OnChunkLoaded (org.terasology.engine.world.chunks.event.OnChunkLoaded)7 Vector3i (org.joml.Vector3i)6 Test (org.junit.jupiter.api.Test)4 Event (org.terasology.engine.entitySystem.event.Event)4 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)3 OnActivatedBlocks (org.terasology.engine.world.block.OnActivatedBlocks)3 Chunk (org.terasology.engine.world.chunks.Chunk)3 OnChunkGenerated (org.terasology.engine.world.chunks.event.OnChunkGenerated)3 OnAddedBlocks (org.terasology.engine.world.block.OnAddedBlocks)2 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)2 TIntList (gnu.trove.list.TIntList)1 Vector3ic (org.joml.Vector3ic)1 BaseEntityRef (org.terasology.engine.entitySystem.entity.internal.BaseEntityRef)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1 ChunkStore (org.terasology.engine.persistence.ChunkStore)1