Search in sources :

Example 11 with Event

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;
    });
}
Also used : KeyEvent(org.terasology.engine.input.events.KeyEvent) Event(org.terasology.engine.entitySystem.event.Event) KeyEvent(org.terasology.engine.input.events.KeyEvent) BindButtonEvent(org.terasology.engine.input.BindButtonEvent)

Example 12 with 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();
}
Also used : SerializationException(org.terasology.persistence.typeHandling.SerializationException) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) ByteString(com.google.protobuf.ByteString) EntityData(org.terasology.protobuf.EntityData) Event(org.terasology.engine.entitySystem.event.Event) ReplicatedFieldMetadata(org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata) Serializer(org.terasology.persistence.typeHandling.Serializer) PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

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