Search in sources :

Example 6 with Event

use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.

the class ServerImpl method processEvent.

private void processEvent(NetData.EventMessage message) {
    try {
        Event event = eventSerializer.deserialize(message.getEvent());
        EntityRef target = EntityRef.NULL;
        if (message.hasTargetBlockPos()) {
            target = blockEntityRegistry.getBlockEntityAt(NetMessageUtil.convert(message.getTargetBlockPos()));
        } else if (message.hasTargetId()) {
            target = networkSystem.getEntity(message.getTargetId());
        }
        if (target.exists()) {
            target.send(event);
        } else {
            logger.info("Dropping event {} for unavailable entity {}", event.getClass().getSimpleName(), target);
        }
    } catch (DeserializationException e) {
        logger.error("Failed to deserialize event", e);
    }
}
Also used : Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) DeserializationException(org.terasology.persistence.typeHandling.DeserializationException)

Example 7 with Event

use of org.terasology.engine.entitySystem.event.Event 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 8 with Event

use of org.terasology.engine.entitySystem.event.Event 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)

Example 9 with Event

use of org.terasology.engine.entitySystem.event.Event 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 10 with Event

use of org.terasology.engine.entitySystem.event.Event 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

Event (org.terasology.engine.entitySystem.event.Event)12 Test (org.junit.jupiter.api.Test)7 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)6 Vector3i (org.joml.Vector3i)5 OnChunkLoaded (org.terasology.engine.world.chunks.event.OnChunkLoaded)5 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 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)3 OnAddedBlocks (org.terasology.engine.world.block.OnAddedBlocks)2 DeserializationException (org.terasology.persistence.typeHandling.DeserializationException)2 EntityData (org.terasology.protobuf.EntityData)2 Maps (com.google.common.collect.Maps)1 ByteString (com.google.protobuf.ByteString)1 Duration (java.time.Duration)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1