Search in sources :

Example 1 with Event

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");
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) Vector3i(org.joml.Vector3i) Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 2 with Event

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

Example 3 with Event

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));
}
Also used : AwaitedLocalCharacterSpawnEvent(org.terasology.engine.core.modes.loadProcesses.AwaitedLocalCharacterSpawnEvent) ArrayList(java.util.ArrayList) AwaitedLocalCharacterSpawnEvent(org.terasology.engine.core.modes.loadProcesses.AwaitedLocalCharacterSpawnEvent) CreateVisualCharacterEvent(org.terasology.engine.logic.characters.events.CreateVisualCharacterEvent) Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) CreateVisualCharacterEvent(org.terasology.engine.logic.characters.events.CreateVisualCharacterEvent) Test(org.junit.jupiter.api.Test)

Example 4 with Event

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);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f) EntityData(org.terasology.protobuf.EntityData) Event(org.terasology.engine.entitySystem.event.Event) Test(org.junit.jupiter.api.Test)

Example 5 with Event

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();
    }
}
Also used : NetData(org.terasology.protobuf.NetData) Event(org.terasology.engine.entitySystem.event.Event) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) DeserializationException(org.terasology.persistence.typeHandling.DeserializationException) PredictionSystem(org.terasology.engine.logic.characters.PredictionSystem)

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