Search in sources :

Example 1 with PendingEvent

use of org.terasology.engine.entitySystem.event.PendingEvent in project Terasology by MovingBlocks.

the class EventSystemImpl method send.

@Override
public void send(EntityRef entity, Event event) {
    if (Thread.currentThread() != mainThread) {
        pendingEvents.offer(new PendingEvent(entity, event));
    } else {
        Set<EventHandlerInfo> selectedHandlersSet = selectEventHandlers(event.getClass(), entity);
        List<EventHandlerInfo> selectedHandlers = Lists.newArrayList(selectedHandlersSet);
        selectedHandlers.sort(priorityComparator);
        if (event instanceof ConsumableEvent) {
            sendConsumableEvent(entity, event, selectedHandlers);
        } else {
            sendStandardEvent(entity, event, selectedHandlers);
        }
    }
}
Also used : PendingEvent(org.terasology.engine.entitySystem.event.PendingEvent) AbstractConsumableEvent(org.terasology.engine.entitySystem.event.AbstractConsumableEvent) ConsumableEvent(org.terasology.engine.entitySystem.event.ConsumableEvent)

Example 2 with PendingEvent

use of org.terasology.engine.entitySystem.event.PendingEvent 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 PendingEvent

use of org.terasology.engine.entitySystem.event.PendingEvent in project Terasology by MovingBlocks.

the class EventSystemReplayImpl method originalSend.

// send method of EventSystemImpl
private void originalSend(EntityRef entity, Event event, Component component) {
    if (Thread.currentThread() != mainThread) {
        pendingEvents.offer(new PendingEvent(entity, event, component));
    } else {
        SetMultimap<Class<? extends Component>, EventSystemReplayImpl.EventHandlerInfo> handlers = componentSpecificHandlers.get(event.getClass());
        if (handlers != null) {
            List<EventSystemReplayImpl.EventHandlerInfo> eventHandlers = Lists.newArrayList(handlers.get(component.getClass()));
            eventHandlers.sort(priorityComparator);
            for (EventSystemReplayImpl.EventHandlerInfo eventHandler : eventHandlers) {
                if (eventHandler.isValidFor(entity)) {
                    eventHandler.invoke(entity, event);
                }
            }
        }
    }
}
Also used : PendingEvent(org.terasology.engine.entitySystem.event.PendingEvent) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent)

Example 4 with PendingEvent

use of org.terasology.engine.entitySystem.event.PendingEvent in project Terasology by MovingBlocks.

the class EventSystemReplayImpl method process.

/**
 * Processes recorded and pending events. If recordedEvents is not loaded, load it from RecordedEventStore.
 */
@Override
public void process() {
    processRecordedEvents();
    PendingEvent event = pendingEvents.poll();
    while (event != null) {
        if (event.getComponent() != null) {
            originalSend(event.getEntity(), event.getEvent(), event.getComponent());
        } else {
            originalSend(event.getEntity(), event.getEvent());
        }
        event = pendingEvents.poll();
    }
}
Also used : PendingEvent(org.terasology.engine.entitySystem.event.PendingEvent)

Example 5 with PendingEvent

use of org.terasology.engine.entitySystem.event.PendingEvent in project Terasology by MovingBlocks.

the class EventSystemImpl method send.

@Override
public void send(EntityRef entity, Event event, Component component) {
    if (Thread.currentThread() != mainThread) {
        pendingEvents.offer(new PendingEvent(entity, event, component));
    } else {
        SetMultimap<Class<? extends Component>, EventHandlerInfo> handlers = componentSpecificHandlers.get(event.getClass());
        if (handlers != null) {
            List<EventHandlerInfo> eventHandlers = Lists.newArrayList(handlers.get(component.getClass()));
            eventHandlers.sort(priorityComparator);
            for (EventHandlerInfo eventHandler : eventHandlers) {
                if (eventHandler.isValidFor(entity)) {
                    eventHandler.invoke(entity, event);
                }
            }
        }
    }
}
Also used : PendingEvent(org.terasology.engine.entitySystem.event.PendingEvent) Component(org.terasology.gestalt.entitysystem.component.Component)

Aggregations

PendingEvent (org.terasology.engine.entitySystem.event.PendingEvent)6 AbstractConsumableEvent (org.terasology.engine.entitySystem.event.AbstractConsumableEvent)2 ConsumableEvent (org.terasology.engine.entitySystem.event.ConsumableEvent)2 Component (org.terasology.gestalt.entitysystem.component.Component)2 Event (org.terasology.engine.entitySystem.event.Event)1 NetworkComponent (org.terasology.engine.network.NetworkComponent)1 BlockComponent (org.terasology.engine.world.block.BlockComponent)1