Search in sources :

Example 1 with PredictionSystem

use of org.terasology.logic.characters.PredictionSystem 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.entitySystem.event.Event) EntityRef(org.terasology.entitySystem.entity.EntityRef) DeserializationException(org.terasology.persistence.typeHandling.DeserializationException) PredictionSystem(org.terasology.logic.characters.PredictionSystem)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)1 Event (org.terasology.entitySystem.event.Event)1 PredictionSystem (org.terasology.logic.characters.PredictionSystem)1 DeserializationException (org.terasology.persistence.typeHandling.DeserializationException)1 NetData (org.terasology.protobuf.NetData)1