Search in sources :

Example 1 with AttachEntityMessage

use of net.glowstone.net.message.play.entity.AttachEntityMessage 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)

Example 2 with AttachEntityMessage

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

the class GlowEntity method createAfterSpawnMessage.

/**
 * Creates a List of {@link Message} which can be sent to a client directly after the entity is
 * spawned.
 *
 * @param session Session to update this entity for
 * @return A message which can spawn this entity.
 */
public List<Message> createAfterSpawnMessage(GlowSession session) {
    List<Message> result = Lists.newArrayList();
    GlowPlayer player = session.getPlayer();
    if (player == null) {
        // Player disconnected while this task was pending
        return result;
    }
    boolean visible = player.canSeeEntity(this);
    for (GlowEntity leashedEntity : leashedEntities) {
        if (visible && player.canSeeEntity(leashedEntity)) {
            int attached = player.getEntityId() == this.getEntityId() ? 0 : leashedEntity.getEntityId();
            int holder = this.getEntityId();
            result.add(new AttachEntityMessage(attached, holder));
        }
    }
    if (isLeashed() && visible && player.canSeeEntity(leashHolder)) {
        int attached = player.getEntityId() == this.getEntityId() ? 0 : this.getEntityId();
        int holder = leashHolder.getEntityId();
        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) AttachEntityMessage(net.glowstone.net.message.play.entity.AttachEntityMessage)

Aggregations

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