Search in sources :

Example 1 with RelativeEntityPositionMessage

use of net.glowstone.net.message.play.entity.RelativeEntityPositionMessage in project Glowstone by GlowstoneMC.

the class GlowEntity method createUpdateMessage.

/**
 * Creates a {@link Message} which can be sent to a client to update this entity.
 *
 * @param session Session to update this entity for
 * @return A message which can update this entity.
 */
public List<Message> createUpdateMessage(GlowSession session) {
    double x = location.getX();
    double y = location.getY();
    double z = location.getZ();
    double dx = x * 32 - previousLocation.getX() * 32;
    double dy = y * 32 - previousLocation.getY() * 32;
    double dz = z * 32 - previousLocation.getZ() * 32;
    dx *= 128;
    dy *= 128;
    dz *= 128;
    boolean teleport = dx > Short.MAX_VALUE || dy > Short.MAX_VALUE || dz > Short.MAX_VALUE || dx < Short.MIN_VALUE || dy < Short.MIN_VALUE || dz < Short.MIN_VALUE;
    List<Message> result = new LinkedList<>();
    boolean moved = hasMoved();
    boolean rotated = hasRotated();
    if (teleported || moved && teleport) {
        result.add(new EntityTeleportMessage(entityId, location));
    } else if (rotated) {
        int yaw = Position.getIntYaw(location);
        int pitch = Position.getIntPitch(location);
        if (moved) {
            result.add(new RelativeEntityPositionRotationMessage(entityId, (short) dx, (short) dy, (short) dz, yaw, pitch));
        } else {
            result.add(new EntityRotationMessage(entityId, yaw, pitch));
        }
    } else if (moved) {
        result.add(new RelativeEntityPositionMessage(entityId, (short) dx, (short) dy, (short) dz));
    }
    // send changed metadata
    List<Entry> changes = metadata.getChanges();
    if (!changes.isEmpty()) {
        result.add(new EntityMetadataMessage(entityId, changes));
    }
    // send velocity if needed
    if (velocityChanged) {
        result.add(new EntityVelocityMessage(entityId, velocity));
    }
    if (passengerChanged) {
        // A player can be a passenger of any arbitrary entity, e.g. a boat
        // In case the current session belongs to this player passenger
        // We need to send the self_id
        List<Integer> passengerIds = new ArrayList<>();
        getPassengers().forEach(e -> passengerIds.add(e.getEntityId()));
        result.add(new SetPassengerMessage(getEntityId(), passengerIds.stream().mapToInt(Integer::intValue).toArray()));
        passengerChanged = false;
    }
    if (leashHolderChanged) {
        int attached = isLeashed() && session.getPlayer().getEntityId() == leashHolder.getEntityId() ? 0 : this.getEntityId();
        int holder = !isLeashed() ? -1 : leashHolder.getEntityId();
        // createAfterSpawnMessage()
        if (!isLeashed() || session.getPlayer().canSeeEntity(leashHolder)) {
            result.add(new AttachEntityMessage(attached, holder));
        }
    }
    return result;
}
Also used : EntityRotationMessage(net.glowstone.net.message.play.entity.EntityRotationMessage) EntityStatusMessage(net.glowstone.net.message.play.entity.EntityStatusMessage) InteractEntityMessage(net.glowstone.net.message.play.player.InteractEntityMessage) EntityMetadataMessage(net.glowstone.net.message.play.entity.EntityMetadataMessage) EntityTeleportMessage(net.glowstone.net.message.play.entity.EntityTeleportMessage) SetPassengerMessage(net.glowstone.net.message.play.entity.SetPassengerMessage) Message(com.flowpowered.network.Message) TextMessage(net.glowstone.util.TextMessage) RelativeEntityPositionMessage(net.glowstone.net.message.play.entity.RelativeEntityPositionMessage) EntityVelocityMessage(net.glowstone.net.message.play.entity.EntityVelocityMessage) AttachEntityMessage(net.glowstone.net.message.play.entity.AttachEntityMessage) RelativeEntityPositionRotationMessage(net.glowstone.net.message.play.entity.RelativeEntityPositionRotationMessage) SetPassengerMessage(net.glowstone.net.message.play.entity.SetPassengerMessage) EntityVelocityMessage(net.glowstone.net.message.play.entity.EntityVelocityMessage) ArrayList(java.util.ArrayList) EntityRotationMessage(net.glowstone.net.message.play.entity.EntityRotationMessage) LinkedList(java.util.LinkedList) RelativeEntityPositionRotationMessage(net.glowstone.net.message.play.entity.RelativeEntityPositionRotationMessage) RelativeEntityPositionMessage(net.glowstone.net.message.play.entity.RelativeEntityPositionMessage) Entry(net.glowstone.entity.meta.MetadataMap.Entry) AttachEntityMessage(net.glowstone.net.message.play.entity.AttachEntityMessage) EntityTeleportMessage(net.glowstone.net.message.play.entity.EntityTeleportMessage) EntityMetadataMessage(net.glowstone.net.message.play.entity.EntityMetadataMessage)

Aggregations

Message (com.flowpowered.network.Message)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Entry (net.glowstone.entity.meta.MetadataMap.Entry)1 AttachEntityMessage (net.glowstone.net.message.play.entity.AttachEntityMessage)1 EntityMetadataMessage (net.glowstone.net.message.play.entity.EntityMetadataMessage)1 EntityRotationMessage (net.glowstone.net.message.play.entity.EntityRotationMessage)1 EntityStatusMessage (net.glowstone.net.message.play.entity.EntityStatusMessage)1 EntityTeleportMessage (net.glowstone.net.message.play.entity.EntityTeleportMessage)1 EntityVelocityMessage (net.glowstone.net.message.play.entity.EntityVelocityMessage)1 RelativeEntityPositionMessage (net.glowstone.net.message.play.entity.RelativeEntityPositionMessage)1 RelativeEntityPositionRotationMessage (net.glowstone.net.message.play.entity.RelativeEntityPositionRotationMessage)1 SetPassengerMessage (net.glowstone.net.message.play.entity.SetPassengerMessage)1 InteractEntityMessage (net.glowstone.net.message.play.player.InteractEntityMessage)1 TextMessage (net.glowstone.util.TextMessage)1