Search in sources :

Example 1 with MessagePlayOutEntityEquipment

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityEquipment 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 MessagePlayOutEntityEquipment

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

the class PlayerEntityProtocol method sendHat.

private void sendHat(EntityProtocolUpdateContext context, TopHat hat) {
    final LanternItemStack paneItem;
    final LanternItemStack blockItem;
    final Optional<DyeColor> dyeColor = hat.getDyeColor();
    if (dyeColor.isPresent()) {
        paneItem = new LanternItemStack(BlockTypes.CARPET);
        paneItem.offer(Keys.DYE_COLOR, dyeColor.get());
        blockItem = new LanternItemStack(BlockTypes.WOOL);
        blockItem.offer(Keys.DYE_COLOR, dyeColor.get());
    } else if (hat == TopHats.GOLD) {
        paneItem = new LanternItemStack(BlockTypes.LIGHT_WEIGHTED_PRESSURE_PLATE);
        blockItem = new LanternItemStack(BlockTypes.GOLD_BLOCK);
    } else if (hat == TopHats.IRON) {
        paneItem = new LanternItemStack(BlockTypes.HEAVY_WEIGHTED_PRESSURE_PLATE);
        blockItem = new LanternItemStack(BlockTypes.IRON_BLOCK);
    } else if (hat == TopHats.WOOD) {
        paneItem = new LanternItemStack(BlockTypes.WOODEN_PRESSURE_PLATE);
        blockItem = new LanternItemStack(BlockTypes.PLANKS);
    } else if (hat == TopHats.STONE) {
        paneItem = new LanternItemStack(BlockTypes.STONE_PRESSURE_PLATE);
        blockItem = new LanternItemStack(BlockTypes.STONE);
    } else if (hat == TopHats.SNOW) {
        paneItem = new LanternItemStack(BlockTypes.SNOW_LAYER);
        blockItem = new LanternItemStack(BlockTypes.SNOW);
    } else {
        throw new IllegalStateException();
    }
    byte flags = 0x20;
    if (getEntity().get(Keys.GLOWING).get()) {
        flags |= 0x40;
    }
    final ParameterList parameterList1 = new ByteBufParameterList(ByteBufferAllocator.unpooled());
    parameterList1.add(EntityParameters.ArmorStand.FLAGS, (byte) (0x08 | 0x10));
    parameterList1.add(EntityParameters.Base.FLAGS, flags);
    final int id1 = this.passengerStack[10];
    context.sendToAll(() -> new MessagePlayOutSpawnObject(id1, UUID.randomUUID(), 78, 0, getEntity().getPosition(), 0, 0, Vector3d.ZERO));
    context.sendToAll(() -> new MessagePlayOutEntityMetadata(id1, parameterList1));
    context.sendToAll(() -> new MessagePlayOutEntityEquipment(id1, 5, paneItem));
    final ParameterList parameterList2 = new ByteBufParameterList(ByteBufferAllocator.unpooled());
    parameterList2.add(EntityParameters.ArmorStand.FLAGS, (byte) (0x08 | 0x10 | 0x01));
    parameterList2.add(EntityParameters.Base.FLAGS, flags);
    final int id2 = this.passengerStack[11];
    context.sendToAll(() -> new MessagePlayOutSpawnObject(id2, UUID.randomUUID(), 78, 0, getEntity().getPosition(), 0, 0, Vector3d.ZERO));
    context.sendToAll(() -> new MessagePlayOutEntityMetadata(id2, parameterList2));
    context.sendToAll(() -> new MessagePlayOutEntityEquipment(id2, 5, blockItem));
    sendPassengers(context, 8, 10);
    sendPassengers(context, 9, 11);
/*
        final LanternItemStack boneItem = new LanternItemStack(ItemTypes.BONE);

        float angleA = 250f;
        float angleB = 250f;
        for (int i = 12; i <= 14; i++) {
            final ParameterList parameterList3 = new ByteBufParameterList(ByteBufferAllocator.unpooled());
            parameterList3.add(EntityParameters.ArmorStand.FLAGS, (byte) (0x08 | 0x10 | 0x01 | 0x04));
            parameterList3.add(EntityParameters.ArmorStand.LEFT_ARM_ROTATION, new Vector3f(50f, 360f - angleA, angleB));
            parameterList3.add(EntityParameters.ArmorStand.RIGHT_ARM_ROTATION, new Vector3f(50f, angleA, 360f - angleB));
            parameterList3.add(EntityParameters.Base.FLAGS, flags);
            final int id3 = this.passengerStack[i];

            context.sendToAll(() -> new MessagePlayOutSpawnObject(id3, UUID.randomUUID(), 78, 0,
                    getEntity().getPosition(), 0, 0, Vector3d.ZERO));
            context.sendToAll(() -> new MessagePlayOutEntityMetadata(id3, parameterList3));
            context.sendToAll(() -> new MessagePlayOutEntityEquipment(id3, 0, boneItem));
            context.sendToAll(() -> new MessagePlayOutEntityEquipment(id3, 1, boneItem));

            angleA -= 15f;
            angleB -= 15f;
        }

        sendPassengers(context, 7, 9, 12, 13, 14);
        */
}
Also used : ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) ParameterList(org.lanternpowered.server.network.entity.parameter.ParameterList) ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) MessagePlayOutEntityMetadata(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata) MessagePlayOutEntityEquipment(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityEquipment) DyeColor(org.spongepowered.api.data.type.DyeColor) MessagePlayOutSpawnObject(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnObject) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 3 with MessagePlayOutEntityEquipment

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

the class EntityProtocol method spawnWithEquipment.

protected void spawnWithEquipment(EntityProtocolUpdateContext context) {
    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 int slotIndex = i;
            if (itemStack != null) {
                context.sendToAllExceptSelf(() -> new MessagePlayOutEntityEquipment(getRootEntityId(), slotIndex, itemStack));
            }
        }
    }
}
Also used : Carrier(org.spongepowered.api.item.inventory.Carrier) MessagePlayOutEntityEquipment(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityEquipment) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Aggregations

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