use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook in project LanternServer by LanternPowered.
the class EntityProtocol method update.
@Override
protected void update(EntityProtocolUpdateContext context) {
final Vector3d rot = this.entity.getRotation();
final Vector3d headRot = this.entity instanceof Living ? ((Living) this.entity).getHeadRotation() : null;
final Vector3d pos = this.entity.getPosition();
final double x = pos.getX();
final double y = pos.getY();
final double z = pos.getZ();
final long xu = (long) (x * 4096);
final long yu = (long) (y * 4096);
final long zu = (long) (z * 4096);
final byte yaw = wrapAngle(rot.getY());
// All living entities have a head rotation and changing the pitch
// would only affect the head pitch.
final byte pitch = wrapAngle((headRot != null ? headRot : rot).getX());
boolean dirtyPos = xu != this.lastX || yu != this.lastY || zu != this.lastZ;
boolean dirtyRot = yaw != this.lastYaw || pitch != this.lastPitch;
// TODO: On ground state
final int entityId = getRootEntityId();
final boolean passenger = this.entity.getVehicle().isPresent();
if (dirtyRot) {
this.lastYaw = yaw;
this.lastPitch = pitch;
}
if (dirtyPos) {
final long dxu = xu - this.lastX;
final long dyu = yu - this.lastY;
final long dzu = zu - this.lastZ;
this.lastX = xu;
this.lastY = yu;
this.lastZ = zu;
// rule the world.
if (!passenger) {
if (Math.abs(dxu) <= Short.MAX_VALUE && Math.abs(dyu) <= Short.MAX_VALUE && Math.abs(dzu) <= Short.MAX_VALUE) {
if (dirtyRot) {
context.sendToAllExceptSelf(new MessagePlayOutEntityLookAndRelativeMove(entityId, (int) dxu, (int) dyu, (int) dzu, yaw, pitch, this.entity.isOnGround()));
// The rotation is already send
dirtyRot = false;
} else {
context.sendToAllExceptSelf(new MessagePlayOutEntityRelativeMove(entityId, (int) dxu, (int) dyu, (int) dzu, this.entity.isOnGround()));
}
} else {
context.sendToAllExceptSelf(new MessagePlayOutEntityTeleport(entityId, x, y, z, yaw, pitch, this.entity.isOnGround()));
// The rotation is already send
dirtyRot = false;
}
}
}
if (dirtyRot) {
context.sendToAllExceptSelf(() -> new MessagePlayOutEntityLook(entityId, yaw, pitch, this.entity.isOnGround()));
} else if (!passenger) {
if (headRot != null) {
final byte headYaw = wrapAngle(headRot.getY());
if (headYaw != this.lastHeadYaw) {
context.sendToAllExceptSelf(() -> new MessagePlayOutEntityHeadLook(entityId, headYaw));
this.lastHeadYaw = headYaw;
}
}
}
final Vector3d velocity = this.entity.getVelocity();
final double vx = velocity.getX();
final double vy = velocity.getY();
final double vz = velocity.getZ();
if (vx != this.lastVelX || vy != this.lastVelY || vz != this.lastVelZ) {
context.sendToAll(() -> new MessagePlayOutEntityVelocity(entityId, vx, vy, vz));
this.lastVelX = vx;
this.lastVelY = vy;
this.lastVelZ = vz;
}
final ParameterList parameterList = context == EntityProtocolUpdateContext.empty() ? fillParameters(false, EmptyParameterList.INSTANCE) : fillParameters(false);
// There were parameters applied
if (!parameterList.isEmpty()) {
context.sendToAll(() -> new MessagePlayOutEntityMetadata(entityId, parameterList));
}
if (hasEquipment() && this.entity instanceof Carrier) {
final Inventory inventory = ((Carrier) this.entity).getInventory();
for (int i = 0; i < Holder.EQUIPMENT_TYPES.length; i++) {
final ItemStack itemStack = inventory.query(Holder.EQUIPMENT_QUERIES[i]).first().peek().orElse(null);
final ItemStack oldItemStack = this.lastEquipment.get(i);
if (!LanternItemStack.areSimilar(itemStack, oldItemStack)) {
this.lastEquipment.put(i, itemStack);
final int slotIndex = i;
context.sendToAllExceptSelf(() -> new MessagePlayOutEntityEquipment(getRootEntityId(), slotIndex, itemStack));
}
}
}
// TODO: Update attributes
}
use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook in project LanternServer by LanternPowered.
the class HumanoidEntityProtocol method spawn.
@Override
protected void spawn(EntityProtocolUpdateContext context) {
final int entityId = getRootEntityId();
final Vector3d rot = this.entity.getRotation();
final Vector3d headRot = this.entity instanceof LanternLiving ? ((LanternLiving) this.entity).getHeadRotation() : null;
final Vector3d pos = this.entity.getPosition();
final Vector3d vel = this.entity.getVelocity();
final double yaw = rot.getY();
final double pitch = headRot != null ? headRot.getX() : rot.getX();
context.sendToAllExceptSelf(() -> new MessagePlayOutSpawnPlayer(entityId, this.entity.getUniqueId(), pos, wrapAngle(yaw), wrapAngle(pitch), fillParameters(true)));
if (headRot != null) {
context.sendToAllExceptSelf(() -> new MessagePlayOutEntityHeadLook(entityId, wrapAngle(headRot.getY())));
}
if (!vel.equals(Vector3d.ZERO)) {
context.sendToAllExceptSelf(() -> new MessagePlayOutEntityVelocity(entityId, vel.getX(), vel.getY(), vel.getZ()));
}
spawnWithEquipment(context);
}
use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook in project LanternServer by LanternPowered.
the class PlayerEntityProtocol method update.
@Override
protected void update(EntityProtocolUpdateContext context) {
final GameMode gameMode = this.entity.get(Keys.GAME_MODE).get();
final boolean canFly = canFly();
final float flySpeed = getFlySpeed();
final float fieldOfView = getFovModifier();
if (gameMode != this.lastGameMode) {
context.sendToSelf(() -> new MessagePlayOutSetGameMode((LanternGameMode) gameMode));
context.sendToSelf(() -> new MessagePlayOutPlayerAbilities(this.entity.get(Keys.IS_FLYING).orElse(false), canFly, false, gameMode == GameModes.CREATIVE, flySpeed, fieldOfView));
this.lastGameMode = gameMode;
this.lastCanFly = canFly;
this.lastFlySpeed = flySpeed;
this.lastFieldOfView = fieldOfView;
} else if (canFly != this.lastCanFly || flySpeed != this.lastFlySpeed || fieldOfView != this.lastFieldOfView) {
context.sendToSelf(() -> new MessagePlayOutPlayerAbilities(this.entity.get(Keys.IS_FLYING).orElse(false), canFly, false, gameMode == GameModes.CREATIVE, flySpeed, fieldOfView));
this.lastCanFly = canFly;
this.lastFlySpeed = flySpeed;
this.lastFieldOfView = fieldOfView;
}
final float health = this.entity.get(Keys.HEALTH).get().floatValue();
final int foodLevel = this.entity.get(Keys.FOOD_LEVEL).get();
final float saturation = this.entity.get(Keys.SATURATION).get().floatValue();
if (health != this.lastHealth || foodLevel != this.lastFoodLevel || saturation == 0.0f != this.lastHungry) {
context.sendToSelf(() -> new MessagePlayOutPlayerHealthUpdate(health, foodLevel, saturation));
this.lastHealth = health;
this.lastFoodLevel = foodLevel;
this.lastHungry = saturation == 0.0f;
}
super.update(context);
final TopHat topHat = getTopHat();
if (topHat != this.lastTopHat) {
if (this.lastTopHat == null) {
sendPassengerStack(context);
sendHat(context, topHat);
} else if (topHat == null) {
removePassengerStack(context);
} else {
sendHat(context, topHat);
}
this.lastTopHat = topHat;
}
if (this.lastYaw0 != this.lastYaw || this.lastPitch0 != this.lastPitch || this.lastFlags0 != this.lastFlags) {
for (final int id : this.passengerStack) {
context.sendToSelf(() -> new MessagePlayOutEntityLook(id, this.lastYaw, this.lastPitch, this.entity.isOnGround()));
}
if (this.lastTopHat != null) {
context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[10], this.lastYaw));
context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[11], this.lastYaw));
context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[12], this.lastYaw));
// context.sendToSelf(() -> new MessagePlayOutEntityHeadLook(this.passengerStack[14], this.lastYaw));
if (this.lastFlags0 != this.lastFlags) {
final boolean glow = (this.lastFlags & 0x40) != 0;
final ParameterList parameterList = new ByteBufParameterList(ByteBufferAllocator.unpooled());
parameterList.add(EntityParameters.Base.FLAGS, (byte) (0x20 | (glow ? 0x40 : 0x00)));
context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.passengerStack[10], parameterList));
context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.passengerStack[11], parameterList));
}
}
this.lastYaw0 = this.lastYaw;
this.lastPitch0 = this.lastPitch;
this.lastFlags0 = this.lastFlags;
}
// Some 1.11.2 magic, ultra secret stuff...
final boolean elytraFlying = this.entity.get(LanternKeys.IS_ELYTRA_FLYING).orElse(false);
final boolean elytraSpeedBoost = this.entity.get(LanternKeys.ELYTRA_SPEED_BOOST).orElse(false);
if (this.lastElytraFlying != elytraFlying || this.lastElytraSpeedBoost != elytraSpeedBoost) {
if (this.lastElytraFlying && this.lastElytraSpeedBoost) {
context.sendToAll(() -> new MessagePlayOutDestroyEntities(this.elytraRocketId));
} else if (elytraFlying && elytraSpeedBoost) {
// Create the fireworks data item
final LanternItemStack itemStack = new LanternItemStack(ItemTypes.FIREWORKS);
// Write the item to a parameter list
final ByteBufParameterList parameterList = new ByteBufParameterList(ByteBufferAllocator.unpooled());
parameterList.add(EntityParameters.Fireworks.ITEM, itemStack);
parameterList.add(EntityParameters.Fireworks.ELYTRA_BOOST_PLAYER, getRootEntityId());
context.sendToAll(() -> new MessagePlayOutSpawnObject(this.elytraRocketId, UUID.randomUUID(), 76, 0, this.entity.getPosition(), 0, 0, Vector3d.ZERO));
context.sendToAll(() -> new MessagePlayOutEntityMetadata(this.elytraRocketId, parameterList));
}
this.lastElytraSpeedBoost = elytraSpeedBoost;
this.lastElytraFlying = elytraFlying;
}
}
Aggregations