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);
}
}
}
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;
}
}
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);
}
}
}
}
}
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();
}
}
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);
}
}
}
}
}
Aggregations