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