Search in sources :

Example 1 with OnChunkGenerated

use of org.terasology.engine.world.chunks.event.OnChunkGenerated 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 OnChunkGenerated

use of org.terasology.engine.world.chunks.event.OnChunkGenerated 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 3 with OnChunkGenerated

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

the class LocalChunkProvider method processReadyChunk.

private void processReadyChunk(final Chunk chunk) {
    Vector3ic chunkPos = chunk.getPosition();
    if (chunkCache.get(chunkPos) != null) {
        // TODO move it in pipeline;
        return;
    }
    chunkCache.put(new Vector3i(chunkPos), chunk);
    chunk.markReady();
    // TODO, it is not clear if the activate/addedBlocks event logic is correct.
    // See https://github.com/MovingBlocks/Terasology/issues/3244
    ChunkStore store = this.storageManager.loadChunkStore(chunkPos);
    TShortObjectMap<TIntList> mappings = createBatchBlockEventMappings(chunk);
    if (store != null) {
        store.restoreEntities();
        PerformanceMonitor.startActivity("Sending OnAddedBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnAddedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
        // send on activate
        PerformanceMonitor.startActivity("Sending OnActivateBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnActivatedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
    } else {
        PerformanceMonitor.startActivity("Generating queued Entities");
        generateQueuedEntities.remove(chunkPos).forEach(this::generateQueuedEntities);
        PerformanceMonitor.endActivity();
        // send on activate
        PerformanceMonitor.startActivity("Sending OnActivateBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnActivatedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
        worldEntity.send(new OnChunkGenerated(chunkPos));
    }
    worldEntity.send(new OnChunkLoaded(chunkPos));
}
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) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) OnAddedBlocks(org.terasology.engine.world.block.OnAddedBlocks) TIntList(gnu.trove.list.TIntList) ChunkStore(org.terasology.engine.persistence.ChunkStore)

Aggregations

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