Beispiel #1
0
  /**
   * Updates the player.
   *
   * @param player the player to update.
   */
  public static void update(Player player) {
    // XXX: The buffer sizes may need to be tuned.
    PacketBuffer.WriteBuffer out = PacketBuffer.newWriteBuffer(16384); // 8192
    PacketBuffer.WriteBuffer block = PacketBuffer.newWriteBuffer(8192); // 4096

    /** Initialize the update packet. */
    out.writeVariableShortPacketHeader(81);
    out.setAccessType(PacketBuffer.AccessType.BIT_ACCESS);

    /** Update this player. */
    PlayerUpdate.updateLocalPlayerMovement(player, out);

    if (player.getFlags().isUpdateRequired()) {
      PlayerUpdate.updateState(player, block, false, true);
    }

    /** Update other local players. */
    out.writeBits(8, player.getPlayers().size());
    for (Iterator<Player> i = player.getPlayers().iterator(); i.hasNext(); ) {
      Player other = i.next();
      if (other.getPosition().isViewableFrom(player.getPosition())
          && other.getSession().getStage() == Session.Stage.LOGGED_IN
          && !other.isNeedsPlacement()
          && other.isVisible()) {
        PlayerUpdate.updateOtherPlayerMovement(other, out);
        if (other.getFlags().isUpdateRequired()) {
          PlayerUpdate.updateState(other, block, false, false);
        }
      } else {
        out.writeBit(true);
        out.writeBits(2, 3);
        i.remove();
      }
    }

    int added = 0;

    /** Update the local player list. */
    for (int i = 0; i < World.getPlayers().getCapacity(); i++) {
      if (added == 15 || player.getPlayers().size() >= 220) {

        /** Player limit has been reached. */
        break;
      }
      Player other = World.getPlayers().get(i);
      if (other == null
          || other == player
          || other.getSession().getStage() != Session.Stage.LOGGED_IN
          || !other.isVisible()) {
        continue;
      }
      if (!player.getPlayers().contains(other)
          && other.getPosition().isViewableFrom(player.getPosition())) {
        added++;
        player.getPlayers().add(other);
        PlayerUpdate.addPlayer(out, player, other);
        PlayerUpdate.updateState(other, block, true, false);
      }
    }

    /** Append the attributes block to the main packet. */
    if (block.getBuffer().position() > 0) {
      out.writeBits(11, 2047);
      out.setAccessType(PacketBuffer.AccessType.BYTE_ACCESS);
      out.writeBytes(block.getBuffer());
    } else {
      out.setAccessType(PacketBuffer.AccessType.BYTE_ACCESS);
    }

    /** Finish the packet and send it. */
    out.finishVariableShortPacketHeader();
    player.getSession().encode(out);
  }
Beispiel #2
0
  /**
   * Appends the state of a player's appearance to a buffer.
   *
   * @param player the player.
   * @param out the buffer.
   */
  public static void appendAppearance(Player player, PacketBuffer.WriteBuffer out) {
    PacketBuffer.WriteBuffer block = PacketBuffer.newWriteBuffer(128);

    /** Gender. */
    block.writeByte(player.getGender()); // Gender

    /** Head icon. */
    block.writeByte(player.getHeadIcon());

    /** Skull icon. */
    block.writeByte(player.getSkullIcon());
    if (player.getNpcAppearanceId() == -1) {

      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD));
      } else {
        block.writeByte(0);
      }

      /** Cape. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CAPE) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CAPE));
      } else {
        block.writeByte(0);
      }

      /** Amulet. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_AMULET) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_AMULET));
      } else {
        block.writeByte(0);
      }

      /** Weapon. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_WEAPON) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_WEAPON));
      } else {
        block.writeByte(0);
      }

      /** Chest. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CHEST) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CHEST));
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_CHEST]);
      }

      /** Shield. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_SHIELD) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_SHIELD));
      } else {
        block.writeByte(0);
      }

      /** Arms. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CHEST) > 1) {

        if (!Misc.getIsPlatebody()[
            player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_CHEST)]) {
          block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_ARMS]);
        } else {
          block.writeByte(0);
        }
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_ARMS]);
      }

      /** Legs. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_LEGS) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_LEGS));
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_LEGS]);
      }

      /** Head. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD) > 1
          && Misc.getIsFullHelm()[
              player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD)]) {
        block.writeByte(0);
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_HEAD]);
      }

      /** Hands. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HANDS) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HANDS));
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_HANDS]);
      }

      /** Feet. */
      if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_FEET) > 1) {
        block.writeShort(
            0x200 + player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_FEET));
      } else {
        block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_FEET]);
      }

      /** Beard. */
      if (player.getGender() == Misc.GENDER_MALE) {
        if (player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD) > 1
                && !Misc.getIsFullHelm()[
                    player.getEquipment().getContainer().getItemId(Misc.EQUIPMENT_SLOT_HEAD)]
            || player.getEquipment().getContainer().isSlotFree(Misc.EQUIPMENT_SLOT_HEAD)) {
          block.writeShort(0x100 + player.getAppearance()[Misc.APPEARANCE_SLOT_BEARD]);
        } else {
          block.writeByte(0);
        }
      }

    } else {
      block.writeShort(-1);
      block.writeShort(player.getNpcAppearanceId());
    }

    /** Player colors */
    block.writeByte(player.getColors()[0]);
    block.writeByte(player.getColors()[1]);
    block.writeByte(player.getColors()[2]);
    block.writeByte(player.getColors()[3]);
    block.writeByte(player.getColors()[4]);

    /** Movement animations */
    block.writeShort(
        player.getUpdateAnimation().getStandingAnimation() == -1
            ? PlayerAnimation.getStandEmote()
            : player.getUpdateAnimation().getStandingAnimation()); // stand
    block.writeShort(PlayerAnimation.getStandTurnEmote()); // stand turn
    block.writeShort(
        player.getUpdateAnimation().getWalkingAnimation() == -1
            ? PlayerAnimation.getWalkEmote()
            : player.getUpdateAnimation().getWalkingAnimation()); // walk
    block.writeShort(PlayerAnimation.getTurn180Emote()); // turn 180
    block.writeShort(PlayerAnimation.getTurn90CWEmote()); // turn 90 cw
    block.writeShort(PlayerAnimation.getTurn90CCWEmote()); // turn 90 ccw
    block.writeShort(
        player.getUpdateAnimation().getRunningAnimation() == -1
            ? PlayerAnimation.getRunEmote()
            : player.getUpdateAnimation().getRunningAnimation()); // run

    /** Player context menus */
    block.writeLong(Misc.nameToLong(player.getUsername()));
    block.writeByte(player.getCombatLevel());
    block.writeShort(0);

    /** Append the block length and the block to the packet. */
    out.writeByte(block.getBuffer().position(), PacketBuffer.ValueType.C);
    out.writeBytes(block.getBuffer());
  }