use of org.terasology.engine.entitySystem.event.Event 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");
}
use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class EventCatcher method addEvent.
/**
* Receives a PendingEvent and add it as a RecordedEvent in the RecordedEventStore if it is an event type that should be
* recorded.
* @param pendingEvent PendingEvent to be checked and added
* @return If the event was added to the RecordedEventStore
*/
public boolean addEvent(PendingEvent pendingEvent) {
if (shouldRecordEvent(pendingEvent)) {
long timestamp = System.currentTimeMillis() - this.startTime;
Event e = this.eventCopier.copyEvent(pendingEvent.getEvent());
PendingEvent newPendingEvent = new PendingEvent(pendingEvent.getEntity(), e);
RecordedEvent recordedEvent;
if (pendingEvent.getComponent() == null) {
recordedEvent = new RecordedEvent(newPendingEvent.getEntity().getId(), newPendingEvent.getEvent(), timestamp, this.eventCounter);
} else {
recordedEvent = new RecordedEvent(newPendingEvent.getEntity().getId(), newPendingEvent.getEvent(), newPendingEvent.getComponent(), timestamp, eventCounter);
}
this.eventCounter++;
return recordedEventStore.add(recordedEvent);
} else {
return false;
}
}
use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class VisualCharacterSystemTest method testSendingOfCreateVisualCharacterEvent.
/**
* This test verifies that:
* <ul>
* <li>No visual character gets created (via event) for the own character (as it is first person)</li>
* <li>That the system can deal with LocalPlayer and characters not being properly linked when
* the character entities get loaded/created</li>
* <li>A visual character gets created (via event) for characters that were already present when the player
* joined</li>
* <li>A visual character gets created (via event) for characters that joins afterwards </li>
* </ul>
*/
@Test
public void testSendingOfCreateVisualCharacterEvent() {
EntityRef clientEntity = mockEntityWithUniqueId();
EntityRef otherClientEntity = mockEntityWithUniqueId();
EntityRef ownCharacterEntity = mockEntityWithUniqueId();
List<Event> ownCharacterEntityEvents = new ArrayList<>();
recordEntityEventsToList(ownCharacterEntity, ownCharacterEntityEvents);
VisualCharacterComponent visualComponentOfOwnCharacter = new VisualCharacterComponent();
Mockito.when(ownCharacterEntity.getComponent(VisualCharacterComponent.class)).thenReturn(visualComponentOfOwnCharacter);
EntityRef otherCharacterEntity = mockEntityWithUniqueId();
List<Event> otherCharacterEntityEvents = new ArrayList<>();
recordEntityEventsToList(otherCharacterEntity, otherCharacterEntityEvents);
VisualCharacterComponent visualComponentOfOtherCharacter = new VisualCharacterComponent();
Mockito.when(otherCharacterEntity.getComponent(VisualCharacterComponent.class)).thenReturn(visualComponentOfOtherCharacter);
clientEntityReturnedByLocalPlayer = EntityRef.NULL;
/*
* Simulate activation before entity is done
* since the character is not properly linked yet nothing should happen
*/
system.onActivatedVisualCharacter(OnActivatedComponent.newInstance(), otherCharacterEntity, visualComponentOfOtherCharacter);
system.onActivatedVisualCharacter(OnActivatedComponent.newInstance(), ownCharacterEntity, visualComponentOfOwnCharacter);
simulateProperLinkingOfLocalPlayerAndCharacterEntities(clientEntity, otherClientEntity, ownCharacterEntity, otherCharacterEntity);
system.onAwaitedLocalCharacterSpawnEvent(new AwaitedLocalCharacterSpawnEvent(), ownCharacterEntity);
assertTypesInListEqual(ownCharacterEntityEvents, Collections.emptyList());
assertTypesInListEqual(otherCharacterEntityEvents, Arrays.asList(CreateVisualCharacterEvent.class));
EntityRef laterJoiningCharacterEntity = mockEntityWithUniqueId();
List<Event> laterJoiningCharacterEntityEvents = new ArrayList<>();
recordEntityEventsToList(laterJoiningCharacterEntity, laterJoiningCharacterEntityEvents);
VisualCharacterComponent visualComponentOfLaterJoiningCharacter = new VisualCharacterComponent();
Mockito.when(laterJoiningCharacterEntity.getComponent(VisualCharacterComponent.class)).thenReturn(visualComponentOfLaterJoiningCharacter);
// Joined player is not properly linked but it should not matter:
Mockito.when(laterJoiningCharacterEntity.getOwner()).thenReturn(EntityRef.NULL);
system.onActivatedVisualCharacter(OnActivatedComponent.newInstance(), laterJoiningCharacterEntity, visualComponentOfLaterJoiningCharacter);
/*
* There is no second AwaitedLocalCharacterSpawnEvent event,
* the system must use the activation to send the event:
*/
assertTypesInListEqual(laterJoiningCharacterEntityEvents, Arrays.asList(CreateVisualCharacterEvent.class));
}
use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class VectorEventSerializer method testEventSerializationConstant.
@Test
public void testEventSerializationConstant() throws IOException {
Vector3fTestEvent a = new Vector3fTestEvent();
a.v1 = new Vector3f(1.0f, 2.0f, 3.0f);
a.v2 = new Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
a.v3 = new Vector2f(1.0f, 2.0f);
a.v1c = new Vector3f(1.0f, 1.0f, 1.0f);
a.v2c = new Vector4f(1.0f, 1.0f, 2.0f, 2.0f);
a.v3c = new Vector2f(1.0f, 1.0f);
EntityData.Event ev = serializer.serialize(a);
Event dev = serializer.deserialize(ev);
assumeTrue(dev instanceof Vector3fTestEvent);
assertEquals(((Vector3fTestEvent) dev).v1, new Vector3f(1.0f, 2.0f, 3.0f), .00001f);
assertEquals(((Vector3fTestEvent) dev).v2, new Vector4f(1.0f, 2.0f, 3.0f, 5.0f), .00001f);
assertEquals(((Vector3fTestEvent) dev).v3, new Vector2f(1.0f, 2.0f), .00001f);
assertEquals(((Vector3fTestEvent) dev).v1c, new Vector3f(1.0f, 1.0f, 1.0f), .00001f);
assertEquals(((Vector3fTestEvent) dev).v2c, new Vector4f(1.0f, 1.0f, 2.0f, 2.0f), .00001f);
assertEquals(((Vector3fTestEvent) dev).v3c, new Vector2f(1.0f, 1.0f), .00001f);
}
use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class NetClient method processEvents.
private void processEvents(NetData.NetMessage message) {
boolean lagCompensated = false;
PredictionSystem predictionSystem = CoreRegistry.get(PredictionSystem.class);
for (NetData.EventMessage eventMessage : message.getEventList()) {
try {
Event event = eventSerializer.deserialize(eventMessage.getEvent());
EventMetadata<?> metadata = eventLibrary.getMetadata(event.getClass());
if (metadata.getNetworkEventType() != NetworkEventType.SERVER) {
logger.warn("Received non-server event '{}' from client '{}'", metadata, getName());
continue;
}
if (!lagCompensated && metadata.isLagCompensated()) {
if (predictionSystem != null) {
predictionSystem.lagCompensate(getEntity(), lastReceivedTime);
}
lagCompensated = true;
}
EntityRef target = EntityRef.NULL;
if (eventMessage.hasTargetId()) {
target = networkSystem.getEntity(eventMessage.getTargetId());
}
if (target.exists()) {
if (Objects.equal(networkSystem.getOwner(target), this)) {
target.send(event);
} else {
logger.warn("Received event {} for non-owned entity {} from {}", event, target, this);
}
}
} catch (DeserializationException e) {
logger.error("Failed to deserialize event", e);
} catch (RuntimeException e) {
logger.error("Error processing event", e);
}
}
if (lagCompensated && predictionSystem != null) {
predictionSystem.restoreToPresent();
}
}
Aggregations