Esempio n. 1
0
  /**
   * Updates a player.
   *
   * @param packet The packet.
   * @param otherPlayer The other player.
   * @param forceAppearance The force appearance flag.
   */
  public void updatePlayer(PacketBuilder packet, Player otherPlayer, boolean forceAppearance) {
    /*
     * If no update is required and we don't have to force an appearance
     * update, don't write anything.
     */
    if (!otherPlayer.getUpdateFlags().isUpdateRequired() && !forceAppearance) {
      return;
    }

    /*
     * We can used the cached update block!
     */
    synchronized (otherPlayer) {
      /*  if (otherPlayer.hasCachedUpdateBlock() && otherPlayer != player && !forceAppearance) {
      packet.put(otherPlayer.getCachedUpdateBlock().getPayload().flip());
      return;
      }*/
      /*
       * We have to construct and cache our own block.
       */
      PacketBuilder block = new PacketBuilder();

      /*
       * Calculate the bitmask.
       */
      int mask = 0x0;
      final UpdateFlags flags = otherPlayer.getUpdateFlags();
      if (flags.get(UpdateFlag.FACE_ENTITY)) {
        mask |= 0x20;
      }
      if (flags.get(UpdateFlag.GRAPHICS)) {
        mask |= 0x400;
      }
      if (flags.get(UpdateFlag.CHAT)) {
        mask |= 0x8;
      }
      if (flags.get(UpdateFlag.ANIMATION)) {
        mask |= 0x1;
      }
      if (flags.get(UpdateFlag.APPEARANCE) || forceAppearance) {
        mask |= 0x80;
      }
      if (flags.get(UpdateFlag.HIT)) {
        mask |= 0x2;
      }
      if (flags.get(UpdateFlag.HIT_2)) {
        mask |= 0x200;
      } /*
        if (flags.get(UpdateFlag.FACE_COORDINATE)) {
            mask |= 0x40;
        }*/

      /*
       * Check if the bitmask would overflow a byte.
       */
      if (mask >= 0x100) {
        /*
         * Write it as a short and indicate we have done so.
         */
        mask |= 0x10;
        block.put((byte) (mask & 0xFF));
        block.put((byte) (mask >> 8));
      } else {
        /*
         * Write it as a byte.
         */
        block.put((byte) (mask));
      }
      /*
       * Append the appropriate updates.
       */
      if (flags.get(UpdateFlag.FACE_ENTITY)) {
        Entity entity = otherPlayer.getInteractingEntity();
        block.putShort(entity == null ? -1 : entity.getClientIndex());
      }
      if (flags.get(UpdateFlag.GRAPHICS)) {
        appendGraphicsUpdate(block, otherPlayer);
      }
      if (flags.get(UpdateFlag.CHAT)) {
        appendChatUpdate(block, otherPlayer);
      }
      if (flags.get(UpdateFlag.ANIMATION)) {
        appendAnimationUpdate(block, otherPlayer);
      }
      if (flags.get(UpdateFlag.APPEARANCE) || forceAppearance) {
        appendPlayerAppearanceUpdate(block, otherPlayer);
      }
      if (flags.get(UpdateFlag.HIT)) {
        appendHitUpdate(otherPlayer, block);
      }
      if (flags.get(UpdateFlag.HIT_2)) {
        appendHit2Update(otherPlayer, block);
      } /*
        if (flags.get(UpdateFlag.FACE_COORDINATE)) {
            appendFaceCoordinateUpdate(otherPlayer, block);
        }*/
      /*
       * Convert the block builder to a packet.
       */
      Packet blockPacket = block.toPacket();

      /*
       * Now it is over, cache the block if we can.
       */
      if (otherPlayer != player && !forceAppearance) {
        otherPlayer.setCachedUpdateBlock(blockPacket);
      }

      /*
       * And finally append the block at the end.
       */
      packet.put(blockPacket.getPayload());
    }
  }