Search in sources :

Example 1 with MessagePlayOutEntityVelocity

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity 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
}
Also used : MessagePlayOutEntityLookAndRelativeMove(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityLookAndRelativeMove) Living(org.spongepowered.api.entity.living.Living) LanternLiving(org.lanternpowered.server.entity.LanternLiving) MessagePlayOutEntityEquipment(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityEquipment) MessagePlayOutEntityRelativeMove(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityRelativeMove) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayOutEntityLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityLook) MessagePlayOutEntityVelocity(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity) ParameterList(org.lanternpowered.server.network.entity.parameter.ParameterList) EmptyParameterList(org.lanternpowered.server.network.entity.parameter.EmptyParameterList) ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) MessagePlayOutEntityMetadata(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata) MessagePlayOutEntityTeleport(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityTeleport) Carrier(org.spongepowered.api.item.inventory.Carrier) MessagePlayOutEntityHeadLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 2 with MessagePlayOutEntityVelocity

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity 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);
}
Also used : MessagePlayOutSpawnPlayer(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnPlayer) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayOutEntityVelocity(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity) LanternLiving(org.lanternpowered.server.entity.LanternLiving) MessagePlayOutEntityHeadLook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook)

Example 3 with MessagePlayOutEntityVelocity

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity in project LanternServer by LanternPowered.

the class HandlerPlayInPlayerAbilities method handle.

@Override
public void handle(NetworkContext context, MessagePlayInPlayerAbilities message) {
    final boolean flying = message.isFlying();
    final LanternPlayer player = context.getSession().getPlayer();
    if (!flying || player.get(Keys.CAN_FLY).orElse(false)) {
        player.offer(Keys.IS_FLYING, flying);
    } else {
        // TODO: Just set velocity once it's implemented
        if (player.get(LanternKeys.SUPER_STEVE).orElse(false)) {
            context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), 0, 1.0, 0));
            player.offer(LanternKeys.IS_ELYTRA_FLYING, true);
        } else if (player.get(LanternKeys.CAN_WALL_JUMP).orElse(false)) {
            final Location<World> location = player.getLocation();
            // Get the horizontal direction the player is looking
            final Direction direction = player.getHorizontalDirection(Direction.Division.CARDINAL);
            // Get the block location the player may step against
            final Location<World> location1 = location.add(direction.asOffset().mul(0.6, 0, 0.6));
            SolidSideProperty solidSideProperty = location1.getExtent().getProperty(location1.getBlockPosition(), direction.getOpposite(), SolidSideProperty.class).orElse(null);
            // noinspection ConstantConditions
            if (solidSideProperty != null && solidSideProperty.getValue()) {
                // Push the player a bit back in the other direction,
                // to give a more realistic feeling when pushing off
                // against a wall
                final Vector3d pushBack = direction.asBlockOffset().toDouble().mul(-0.1);
                // Push the player up
                context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), pushBack.getX(), 0.8, pushBack.getZ()));
            } else {
                // Now we try if the player can jump away from the wall
                // Get the block location the player may step against
                final Location<World> location2 = location.add(direction.asOffset().mul(-0.6, 0, -0.6));
                solidSideProperty = location2.getExtent().getProperty(location2.getBlockPosition(), direction, SolidSideProperty.class).orElse(null);
                // noinspection ConstantConditions
                if (solidSideProperty != null && solidSideProperty.getValue()) {
                    // Combine the vectors in the direction of the block face
                    // and the direction the player is looking
                    final Vector3d vector = direction.asBlockOffset().toDouble().mul(0.25).mul(1, 0, 1).add(0, 0.65, 0).add(player.getDirectionVector().mul(0.4, 0.25, 0.4));
                    // Push the player forward and up
                    context.getSession().send(new MessagePlayOutEntityVelocity(player.getNetworkId(), vector.getX(), vector.getY(), vector.getZ()));
                }
            }
        }
        player.triggerEvent(RefreshAbilitiesPlayerEvent.of());
    }
}
Also used : SolidSideProperty(org.lanternpowered.server.block.property.SolidSideProperty) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayOutEntityVelocity(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity) Direction(org.spongepowered.api.util.Direction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Location(org.spongepowered.api.world.Location)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)3 MessagePlayOutEntityVelocity (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityVelocity)3 LanternLiving (org.lanternpowered.server.entity.LanternLiving)2 MessagePlayOutEntityHeadLook (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityHeadLook)2 SolidSideProperty (org.lanternpowered.server.block.property.SolidSideProperty)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)1 ByteBufParameterList (org.lanternpowered.server.network.entity.parameter.ByteBufParameterList)1 EmptyParameterList (org.lanternpowered.server.network.entity.parameter.EmptyParameterList)1 ParameterList (org.lanternpowered.server.network.entity.parameter.ParameterList)1 MessagePlayOutEntityEquipment (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityEquipment)1 MessagePlayOutEntityLook (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityLook)1 MessagePlayOutEntityLookAndRelativeMove (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityLookAndRelativeMove)1 MessagePlayOutEntityMetadata (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata)1 MessagePlayOutEntityRelativeMove (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityRelativeMove)1 MessagePlayOutEntityTeleport (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityTeleport)1 MessagePlayOutSpawnPlayer (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnPlayer)1 Living (org.spongepowered.api.entity.living.Living)1 Carrier (org.spongepowered.api.item.inventory.Carrier)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1