Search in sources :

Example 1 with BeforeDeactivateBlocks

use of org.terasology.engine.world.block.BeforeDeactivateBlocks in project Terasology by MovingBlocks.

the class LocalChunkProvider method deactivateBlocks.

private void deactivateBlocks() {
    List<TShortObjectMap<TIntList>> deactivatedBlockSets = Lists.newArrayListWithExpectedSize(deactivateBlocksQueue.size());
    deactivateBlocksQueue.drainTo(deactivatedBlockSets);
    for (TShortObjectMap<TIntList> deactivatedBlockSet : deactivatedBlockSets) {
        deactivatedBlockSet.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new BeforeDeactivateBlocks(positions, registry));
            }
            return true;
        });
    }
}
Also used : TShortObjectMap(gnu.trove.map.TShortObjectMap) TIntList(gnu.trove.list.TIntList) BeforeDeactivateBlocks(org.terasology.engine.world.block.BeforeDeactivateBlocks)

Example 2 with BeforeDeactivateBlocks

use of org.terasology.engine.world.block.BeforeDeactivateBlocks in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testUnloadChunkAndDeactivationBlock.

@Test
void testUnloadChunkAndDeactivationBlock() throws InterruptedException, TimeoutException, ExecutionException {
    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);
    // Wait BeforeDeactivateBlocks event
    Assertions.assertTimeoutPreemptively(Duration.of(WAIT_CHUNK_IS_READY_IN_SECONDS, ChronoUnit.SECONDS), () -> {
        ArgumentCaptor<Event> blockEventCaptor = ArgumentCaptor.forClass(Event.class);
        while (!blockEventCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeDeactivateBlocks).map((e) -> (BeforeDeactivateBlocks) e).findFirst().isPresent()) {
            chunkProvider.update();
            blockEventCaptor = ArgumentCaptor.forClass(Event.class);
            verify(blockAtBlockManager.getEntity(), atLeast(1)).send(blockEventCaptor.capture());
        }
    });
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(worldEntity, atLeast(1)).send(eventArgumentCaptor.capture());
    Optional<BeforeChunkUnload> beforeChunkUnload = eventArgumentCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeChunkUnload).map((e) -> (BeforeChunkUnload) e).findFirst();
    Assertions.assertTrue(beforeChunkUnload.isPresent(), "World events must have BeforeChunkUnload event when chunk was unload");
    Assertions.assertEquals(chunkPosition, beforeChunkUnload.get().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());
    Optional<BeforeDeactivateBlocks> beforeDeactivateBlocks = blockEventCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeDeactivateBlocks).map((e) -> (BeforeDeactivateBlocks) e).findFirst();
    Assertions.assertTrue(beforeDeactivateBlocks.isPresent(), "World events must have BeforeDeactivateBlocks event when chunk with lifecycle blocks was unload");
    Assertions.assertTrue(beforeDeactivateBlocks.get().blockCount() > 0, "BeforeDeactivateBlocks must have block count more then zero");
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) BeforeEach(org.junit.jupiter.api.BeforeEach) TimeoutException(java.util.concurrent.TimeoutException) Block(org.terasology.engine.world.block.Block) OnAddedBlocks(org.terasology.engine.world.block.OnAddedBlocks) Future(java.util.concurrent.Future) Vector3ic(org.joml.Vector3ic) ArgumentCaptor(org.mockito.ArgumentCaptor) Event(org.terasology.engine.entitySystem.event.Event) Vector3i(org.joml.Vector3i) Chunk(org.terasology.engine.world.chunks.Chunk) Duration(java.time.Duration) Map(java.util.Map) Mockito.atLeast(org.mockito.Mockito.atLeast) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) BlockRegion(org.terasology.engine.world.block.BlockRegion) TestWorldGenerator(org.terasology.fixtures.TestWorldGenerator) TestBlockManager(org.terasology.fixtures.TestBlockManager) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) BeforeDeactivateBlocks(org.terasology.engine.world.block.BeforeDeactivateBlocks) OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) Maps(com.google.common.collect.Maps) TestStorageManager(org.terasology.fixtures.TestStorageManager) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) BeforeChunkUnload(org.terasology.engine.world.chunks.event.BeforeChunkUnload) ChronoUnit(java.time.temporal.ChronoUnit) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BlockManager(org.terasology.engine.world.block.BlockManager) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) TestChunkStore(org.terasology.fixtures.TestChunkStore) Mockito.mock(org.mockito.Mockito.mock) BeforeChunkUnload(org.terasology.engine.world.chunks.event.BeforeChunkUnload) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BeforeDeactivateBlocks(org.terasology.engine.world.block.BeforeDeactivateBlocks) Test(org.junit.jupiter.api.Test)

Aggregations

BeforeDeactivateBlocks (org.terasology.engine.world.block.BeforeDeactivateBlocks)2 Maps (com.google.common.collect.Maps)1 TIntList (gnu.trove.list.TIntList)1 TShortObjectMap (gnu.trove.map.TShortObjectMap)1 Duration (java.time.Duration)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Vector3i (org.joml.Vector3i)1 Vector3ic (org.joml.Vector3ic)1 AfterEach (org.junit.jupiter.api.AfterEach)1 Assertions (org.junit.jupiter.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Mockito.atLeast (org.mockito.Mockito.atLeast)1