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);
}
}
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");
}
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");
}
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");
});
}
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");
});
}
Aggregations