/**
  * Sends a block.
  *
  * @param x X coordinate.
  * @param y Y coordinate.
  * @param z Z coordinate.
  * @param type BlockDefinition type.
  */
 public void sendBlock(int x, int y, int z, byte type) {
   PacketBuilder bldr =
       new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(6));
   bldr.putShort("x", x);
   bldr.putShort("y", y);
   bldr.putShort("z", z);
   bldr.putByte("type", type);
   session.send(bldr.toPacket());
 }
 public void sendSpawn(byte id, String name, int x, int y, int z, byte rotation, byte look) {
   PacketBuilder bldr =
       new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(7));
   bldr.putByte("id", id);
   bldr.putString("name", name);
   bldr.putShort("x", x);
   bldr.putShort("y", y);
   bldr.putShort("z", z);
   bldr.putByte("rotation", rotation);
   bldr.putByte("look", look);
   session.send(bldr.toPacket());
 }
 /**
  * Sends the add entity packet.
  *
  * @param entity The entity being added.
  */
 public void sendAddEntity(Entity entity) {
   PacketBuilder bldr =
       new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(7));
   bldr.putByte("id", entity.getId());
   bldr.putString("name", entity.getColoredName());
   bldr.putShort("x", entity.getPosition().getX());
   bldr.putShort("y", entity.getPosition().getY());
   bldr.putShort("z", entity.getPosition().getZ());
   bldr.putByte("rotation", entity.getRotation().getRotation());
   bldr.putByte("look", entity.getRotation().getLook());
   session.send(bldr.toPacket());
 }
 /**
  * Sends a teleport.
  *
  * @param position The new position.
  * @param rotation The new rotation.
  */
 public void sendTeleport(Position position, Rotation rotation) {
   session.getPlayer().teleportPosition = position.toBlockPos();
   PacketBuilder bldr =
       new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(8));
   bldr.putByte("id", -1);
   bldr.putShort("x", position.getX());
   bldr.putShort("y", position.getY());
   bldr.putShort("z", position.getZ());
   bldr.putByte("rotation", rotation.getRotation());
   bldr.putByte("look", rotation.getLook());
   session.send(bldr.toPacket());
   session.getPlayer().teleporting = true;
 }
  /**
   * Sends the update entity packet.
   *
   * @param entity The entity being updated.
   */
  public void sendUpdateEntity(Entity entity) {
    final Position oldPosition = entity.getOldPosition();
    final Position position = entity.getPosition();

    final Rotation oldRotation = entity.getOldRotation();
    final Rotation rotation = entity.getRotation();

    final int deltaX = -oldPosition.getX() - position.getX();
    final int deltaY = -oldPosition.getY() - position.getY();
    final int deltaZ = -oldPosition.getZ() - position.getZ();

    final int deltaRotation = -oldRotation.getRotation() - rotation.getRotation();
    final int deltaLook = -oldRotation.getLook() - rotation.getLook();
    if (deltaX != 0 || deltaY != 0 || deltaZ != 0) {
      if (deltaX > Byte.MAX_VALUE
          || deltaX < Byte.MIN_VALUE
          || deltaY > Byte.MAX_VALUE
          || deltaY < Byte.MIN_VALUE
          || deltaZ > Byte.MAX_VALUE
          || deltaZ < Byte.MIN_VALUE
          || deltaRotation > Byte.MAX_VALUE
          || deltaRotation < Byte.MIN_VALUE
          || deltaLook > Byte.MAX_VALUE
          || deltaLook < Byte.MIN_VALUE) {
        // teleport
        PacketBuilder bldr =
            new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(8));
        bldr.putByte("id", entity.getId());
        bldr.putShort("x", position.getX());
        bldr.putShort("y", position.getY());
        bldr.putShort("z", position.getZ());
        bldr.putByte("rotation", rotation.getRotation());
        bldr.putByte("look", rotation.getLook());
        session.send(bldr.toPacket());
      } else {
        // send move and rotate packet
        PacketBuilder bldr =
            new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(9));
        bldr.putByte("id", entity.getId());
        bldr.putByte("delta_x", deltaX);
        bldr.putByte("delta_y", deltaY);
        bldr.putByte("delta_z", deltaZ);
        bldr.putByte("delta_rotation", deltaRotation);
        bldr.putByte("delta_look", deltaLook);
        session.send(bldr.toPacket());
      }
    }
  }
 /**
  * Sends a level block/chunk.
  *
  * @param len The length of the chunk.
  * @param chunk The chunk data.
  * @param percent The percentage.
  */
 public void sendLevelBlock(int len, byte[] chunk, int percent) {
   PacketBuilder bldr =
       new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(3));
   bldr.putShort("chunk_length", len);
   bldr.putByteArray("chunk_data", chunk);
   bldr.putByte("percent", percent);
   session.send(bldr.toPacket());
 }