use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class InputSystemTests method registerEntityKeyCapturing.
private void registerEntityKeyCapturing() {
/*
* KeyUpEvent and KeyDownEvent are singletons with a single instance that gets reset each time.
* Therefore it is not possible to capture multiple events with an ArgumentCaptor
* because previous instances become invalid.
* For this reason, the data of these events is captured to a separate class.
*/
when(clientEntity.send(Mockito.any())).then(invocation -> {
Event event = invocation.getArgument(0);
if (event instanceof KeyEvent) {
clientEntityKeyEvents.add(new CapturedKeyEvent((KeyEvent) event));
}
return event;
});
when(characterEntity.send(Mockito.any())).then(invocation -> {
Event event = invocation.getArgument(0);
if (event instanceof KeyEvent) {
characterEntityKeyEvents.add(new CapturedKeyEvent((KeyEvent) event));
}
return event;
});
}
use of org.terasology.engine.entitySystem.event.Event in project Terasology by MovingBlocks.
the class EventSerializer method serialize.
/**
* Serializes an event.
*
* @param event
* @return The serialized event
* @throws org.terasology.engine.persistence.typeHandling.SerializationException if an error occurs during serialization
*/
public EntityData.Event serialize(Event event) {
EventMetadata<?> eventMetadata = eventLibrary.getMetadata(event.getClass());
if (eventMetadata == null) {
throw new SerializationException("Unregistered event type: " + event.getClass());
} else if (!eventMetadata.isConstructable()) {
throw new SerializationException("Cannot serialize event '" + eventMetadata + "' - lacks default constructor so cannot be deserialized");
}
EntityData.Event.Builder eventData = EntityData.Event.newBuilder();
serializeEventType(event, eventData);
Serializer eventSerializer = typeHandlerLibrary.getSerializerFor(eventMetadata);
ByteString.Output fieldIds = ByteString.newOutput();
for (ReplicatedFieldMetadata field : eventMetadata.getFields()) {
if (field.isReplicated()) {
EntityData.Value serializedValue = ((ProtobufPersistedData) eventSerializer.serialize(field, event, persistedDataSerializer)).getValue();
if (serializedValue != null) {
eventData.addFieldValue(serializedValue);
fieldIds.write(field.getId());
}
}
}
eventData.setFieldIds(fieldIds.toByteString());
return eventData.build();
}
Aggregations