@Override
  public void handle(Player player, Packet packet) {
    final int id = packet.getShort() & 0xFFFF;
    final int slot = packet.getShortA() & 0xFFFF;
    final int interfaceId = packet.getShortA() & 0xFFFF;

    switch (interfaceId) {
      case Inventory.INTERFACE:
        if (slot >= 0 && slot < Inventory.SIZE) {
          final Item item = player.getInventory().get(slot);
          if (item != null && item.getId() == id) {
            final EquipmentType type = Equipment.getType(item);
            Item oldEquip = null;
            final boolean stackable = false;
            if (player.getEquipment().isSlotUsed(type.getSlot()) && !stackable) {
              oldEquip = player.getEquipment().get(type.getSlot());
              player.getEquipment().set(type.getSlot(), null);
            }
            player.getInventory().set(slot, null);
            if (oldEquip != null) {
              player.getInventory().add(oldEquip);
            }
            if (!stackable) {
              player.getEquipment().set(type.getSlot(), item);
            } else {
              player.getEquipment().add(item);
            }
          }
        }
        break;
    }
  }
  @Override
  public void handle(Player player, Packet packet) {
    int button = packet.getShort();
    int interfaceId = packet.getShort();
    packet.getLEShortA();
    if (interfaceId != player.getInterfaceState().getCurrentInterface()) {
      player.getActionSender().sendCloseInterface();
      System.out.println("AMFG HACKERS.");
      return;
    }
    DialogueLoader dl = player.getCurrentDialogueLoader();
    switch (interfaceId) {
        /** DialougeLoader. */
      case 64:
      case 65:
      case 66:
      case 67:
      case 241:
      case 242:
      case 243:
      case 244:
        if (dl == null || player.getNextDialogueIds() == null) {
          /*
           * If the next dialogue ID's are null, we've most likely set
           * them null.
           */
          /*
           * We close all interfaces.
           */
          player.getActionSender().sendCloseInterface();
          /*
           * Check if the Monk of Entrana just searched us.
           */
          Object entranaTraveling = player.getTemporaryAttribute("EntranaShipTraveling");
          if (entranaTraveling != null) {
            if ((Boolean) entranaTraveling) {
              ShipTraveling.travel(player, ShipLocation.PORT_SARIM, ShipLocation.ENTRANA);
            }
            player.removeTemporaryAttribute("EntranaShipTraveling");
          }
          break;
        }
        int nextDialogueId = player.getNextDialogueIds()[0];
        DialogueLoader.getNextDialogue(player, dl, nextDialogueId);
        break;
        /** Options. */
      case 228: // Two options interface.
        if (dl == null) {
          player.getActionSender().sendCloseInterface();
          break;
        }
        switch (button) {
          case 1: // Yes.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[0]);
            return;
          case 2: // NO.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[1]);
            return;
        }
        break;
      case 230: // Three options interface.
        if (dl == null) {
          player.getActionSender().sendCloseInterface();
          break;
        }
        switch (button) {
          case 1: // Option 1.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[0]);
            return;
          case 2: // Option 2.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[1]);
            return;
          case 3: // Option 3.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[2]);
            return;
        }
        break;
      case 232: // Four options interface.
        if (dl == null) {
          player.getActionSender().sendCloseInterface();
          break;
        }
        switch (button) {
          case 1: // Option 1.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[0]);
            return;
          case 2: // Option 2.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[1]);
            return;
          case 3: // Option 3.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[2]);
            return;
          case 4: // Option 4.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[3]);
            return;
        }
        break;
      case 234: // Five options interface.
        if (dl == null) {
          player.getActionSender().sendCloseInterface();
          break;
        }
        switch (button) {
          case 1: // Option 1.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[0]);
            return;
          case 2: // Option 2.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[1]);
            return;
          case 3: // Option 3.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[2]);
            return;
          case 4: // Option 4.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[3]);
            return;
          case 5: // Option 5.
            DialogueLoader.getNextDialogue(player, dl, player.getNextDialogueIds()[4]);
            return;
        }
        break;
        /*
         * Interface with one line of info.
         */
      case 101:
        Object entranaTraveling1 = player.getTemporaryAttribute("EntranaShipTraveling");
        if (entranaTraveling1 != null) {
          boolean canTravel = (Boolean) entranaTraveling1;
          if (canTravel) {
            String[] lines = {"All is satisfactory. You may board the boat now."};
            DialogueLoader.dialogue(
                player,
                player.getInteractingEntity() == null
                    ? new NPC(NPCDefinition.forId(657))
                    : player.getInteractingEntity(),
                Emotes.DEFAULT,
                lines);
            player.setNextDialogueIds(null);
          } else {
            String[] lines = {
              "Grr! I see you brought some illegal items! Get", "out of my sight immediately!"
            };
            DialogueLoader.dialogue(
                player,
                player.getInteractingEntity() == null
                    ? new NPC(NPCDefinition.forId(657))
                    : player.getInteractingEntity(),
                Emotes.ANGER2,
                lines);
            player.setNextDialogueIds(null);
          }
          return;
        }
        player.getActionSender().sendCloseInterface();
        break;
        /*
         * Only used for Construction at the moment..
         */
      case 233:
        if (player.getConstruction().getBuildingRoom() == null) {
          switch (button) {
              /*
               * Enter your house.
               */
            case 1:
              player.getConstruction().enterHouse(false);
              break;
              /*
               * Enter your house (building mode)
               */
            case 2:
              player.getConstruction().enterHouse(true);
              break;
              /*
               * Enter friends house.
               */
            case 3:
              player.getInterfaceState().openEnterNameInterface(interfaceId);
              break;
          }
        } else {
          switch (button) {
              /*
               * Rotate clockwise
               */
            case 1:
              player
                  .getActionSender()
                  .sendMessage(
                      "I almost had this, but spend 2 days fixing it. Its a delayed feature for now.");
              player.getActionSender().sendCloseInterface();
              // player.getConstruction().spawnRoomForRotation(true);
              break;
              /*
               * Rotate anti clockwise
               */
            case 2:
              player
                  .getActionSender()
                  .sendMessage(
                      "I almost had this, but spend 2 days fixing it. Its a delayed feature for now.");
              player.getActionSender().sendCloseInterface();
              // player.getConstruction().spawnRoomForRotation(false);
              break;
              /*
               * Build.
               */
            case 3:
              for (GameObject object : player.getConstruction().getBuildingRoom().getHotspots()) {
                Location local =
                    Location.create(
                        object.getLocation().getX()
                            + player.getConstruction().getBuildingRoom().getX(),
                        object.getLocation().getY()
                            + player.getConstruction().getBuildingRoom().getY(),
                        object.getLocation().getZ());
                player
                    .getActionSender()
                    .sendDestroyLocalObject(object.getType(), object.getRotation(), local);
              }
              player
                  .getConstruction()
                  .getHouse()
                  .addRoomAndReload(player.getConstruction().getBuildingRoom());
              player.getInventory().remove(new Item(995, player.getConstruction().getRoomPrice()));
              player.getConstruction().setBuildingRoom(null, -1);
              break;
              /*
               * Cancel..
               */
            case 4:
              for (GameObject object : player.getConstruction().getBuildingRoom().getHotspots()) {
                Location local =
                    Location.create(
                        object.getLocation().getX()
                            + player.getConstruction().getBuildingRoom().getX(),
                        object.getLocation().getY()
                            + player.getConstruction().getBuildingRoom().getY(),
                        object.getLocation().getZ());
                player
                    .getActionSender()
                    .sendDestroyLocalObject(object.getType(), object.getRotation(), local);
              }
              player.getConstruction().setBuildingRoom(null, -1);
              player.getActionSender().sendCloseInterface();
              break;
          }
        }
        break;
      case Cooking.COOKING_INTERFACE:
        Cooking.handleInterfaceButtons(player, button);
        break;
        /*
         * Entering the barrows tunnels.
         */
      case 229:
        Object obj = player.getTemporaryAttribute("InterfaceOption");
        if (obj != null) {
          InterfaceOption option = (InterfaceOption) obj;
          switch (option) {
            case BARROWS:
              switch (button) {
                case 1:
                  Barrows.teleportToTunnels(player);
                  player.getActionSender().sendCloseInterface();
                  break;
                case 2:
                  player.getActionSender().sendCloseInterface();
                  break;
              }
              break;
            case CONSTRUCTION:
              switch (button) {
                  /*
                   * Yes
                   */
                case 1:
                  player.getConstruction().remove();
                  player.getActionSender().sendCloseInterface();
                  break;
                  /*
                   * No
                   */
                case 2:
                  player.getActionSender().sendCloseInterface();
                  break;
              }
              break;
          }
        }

        break;
      case 231: // Three options.
        switch (player.getJewellery().getGemType()) {
          case RING_OF_DUELING:
            /*
             * Ring of dueling rubbing.
             */
            switch (button) {
              case 1: // Teleport to Castle Wars.
                player.getJewellery().gemTeleport(player, Location.create(2440, 3089, 0));
                player.getActionSender().sendCloseInterface();
                break;
              case 2: // Teleport to Duel Arena.
                player.getJewellery().gemTeleport(player, Location.create(3316, 3235, 0));
                player.getActionSender().sendCloseInterface();
                break;
              case 3: // Nowhere.
                player.getActionSender().sendCloseInterface();
                break;
            }
            break;
          case GAMES_NECKLACE:
            /*
             * Games necklace rubbing.
             */
            switch (button) {
              case 1: // Burthope games room (Burthope for now)
                player.getJewellery().gemTeleport(player, Location.create(2926, 3559, 0));
                player.getActionSender().sendCloseInterface();
                break;
              case 2: // Barbarian outpost.
                player.getJewellery().gemTeleport(player, Location.create(2541, 3546, 0));
                player.getActionSender().sendCloseInterface();
                break;
              case 3: // Nowhere.
                player.getActionSender().sendCloseInterface();
                break;
            }
            break;
        }

        break;

        /*
         * Glory teleporting.
         */
      case 238:
        switch (button) {
            /*
             * Edgewille.
             */
          case 1:
            player.getJewellery().gemTeleport(player, Location.create(3089, 3496, 0));
            player.getActionSender().sendCloseInterface();
            break;
            /*
             * Karamja.
             */
          case 2:
            player.getJewellery().gemTeleport(player, Location.create(2918, 3176, 0));
            player.getActionSender().sendCloseInterface();
            break;
            /*
             * Draynor Village
             */
          case 3:
            player.getJewellery().gemTeleport(player, Location.create(3105, 3249, 0));
            player.getActionSender().sendCloseInterface();
            break;
            /*
             * Al-Kharid
             */
          case 4:
            player.getJewellery().gemTeleport(player, Location.create(3293, 3163, 0));
            player.getActionSender().sendCloseInterface();
            break;
          case 5:
            player.getActionSender().sendCloseInterface();
            break;
        }
        break;
        /*
         * Hard leather body interface.
         */
      case 309:
        if (Crafting.hardLeatherBodies(player, button)) {
          return;
        }
        break;
        /*
         * All Dragon leathers are configured in this interface. Fletching is
         * done in this interface as well.
         */
      case 304:
        if (player.getCraftingVariables().isCrafting()) {
          Crafting.dragonLeatherCrafting(player, button);
        } else if (player.getFletchingVariables().isFletching()) {
          Fletching.handleChatboxInterface(304, player, button);
        }
        break;
      case 303:
        Fletching.handleChatboxInterface(303, player, button);
        break;
        /*
         * Destroy items.
         */
      case 94:
        switch (button) {
          case 4:
            player
                .getInventory()
                .remove(player.getDestroyingItem()[1], new Item(player.getDestroyingItem()[0]));
            player.getActionSender().sendCloseInterface();
            break;
          case 5:
            player.getActionSender().sendCloseInterface();
            break;
        }
        break;
      default:
        System.out.println("Unhandled chatbox interface: " + interfaceId + " " + button);
        player.getActionSender().sendCloseInterface();
    }
  }
  /**
   * Appends an appearance update.
   *
   * @param packet The packet.
   * @param otherPlayer The player.
   */
  private void appendPlayerAppearanceUpdate(PacketBuilder packet, Player otherPlayer) {
    Appearance app = otherPlayer.getAppearance();
    Container eq = otherPlayer.getEquipment();

    PacketBuilder playerProps = new PacketBuilder();
    playerProps.put((byte) app.getGender()); // gender
    if ((app.getGender() & 0x2) == 2) {
      playerProps.put((byte) 0);
      playerProps.put((byte) 0);
    }
    playerProps.put((byte) -1); // skull icon
    playerProps.put((byte) -1); // prayer icon

    if (!otherPlayer.getAppearance().isNpc()) {
      for (int i = 0; i < 4; i++) {
        if (eq.isSlotUsed(i)) {
          playerProps.putShort((short) 32768 + eq.get(i).getDefinition().getEquipId());
        } else {
          playerProps.put((byte) 0);
        }
      }
      if (eq.isSlotUsed(Equipment.SLOT_CHEST)) {
        playerProps.putShort(
            (short) 32768 + eq.get(Equipment.SLOT_CHEST).getDefinition().getEquipId());
      } else {
        playerProps.putShort((short) 0x100 + app.getChest()); // chest
      }
      if (eq.isSlotUsed(Equipment.SLOT_SHIELD)) {
        playerProps.putShort(
            (short) 32768 + eq.get(Equipment.SLOT_SHIELD).getDefinition().getEquipId());
      } else {
        playerProps.put((byte) 0);
      }
      final Item chest = eq.get(Equipment.SLOT_CHEST);
      if (chest != null) {
        if (!Equipment.is(EquipmentType.PLATEBODY, chest)) {
          playerProps.putShort((short) 0x100 + app.getArms());
        } else {
          playerProps.putShort((short) 32768 + chest.getDefinition().getEquipId());
        }
      } else {
        playerProps.putShort((short) 0x100 + app.getArms());
      }
      if (eq.isSlotUsed(Equipment.SLOT_BOTTOMS)) {
        playerProps.putShort(
            (short) 32768 + eq.get(Equipment.SLOT_BOTTOMS).getDefinition().getEquipId());
      } else {
        playerProps.putShort((short) 0x100 + app.getLegs());
      }
      final Item helm = eq.get(Equipment.SLOT_HELM);
      if (helm != null) {
        if (!Equipment.is(EquipmentType.FULL_HELM, helm)
            && !Equipment.is(EquipmentType.FULL_MASK, helm)) {
          playerProps.putShort((short) 0x100 + app.getHead());
        } else {
          playerProps.put((byte) 0);
        }
      } else {
        playerProps.putShort((short) 0x100 + app.getHead());
      }
      if (eq.isSlotUsed(Equipment.SLOT_GLOVES)) {
        playerProps.putShort(
            (short) 32768 + eq.get(Equipment.SLOT_GLOVES).getDefinition().getEquipId());
      } else {
        playerProps.putShort((short) 0x100 + app.getHands());
      }
      if (eq.isSlotUsed(Equipment.SLOT_BOOTS)) {
        playerProps.putShort(
            (short) 32768 + eq.get(Equipment.SLOT_BOOTS).getDefinition().getEquipId());
      } else {
        playerProps.putShort((short) 0x100 + app.getFeet());
      }
      boolean fullHelm = false;
      if (helm != null) {
        fullHelm = !Equipment.is(EquipmentType.FULL_HELM, helm);
      }
      if (fullHelm || app.getGender() == 1) {
        playerProps.put((byte) 0);
      } else {
        playerProps.putShort((short) 0x100 + app.getBeard());
      }
    } else {
      playerProps.putShort(-1);
      playerProps.putShort(otherPlayer.getAppearance().getNpcIndex());
    }

    playerProps.put((byte) app.getHairColour()); // hairc
    playerProps.put((byte) app.getTorsoColour()); // torsoc
    playerProps.put((byte) app.getLegColour()); // legc
    playerProps.put((byte) app.getFeetColour()); // feetc
    playerProps.put((byte) app.getSkinColour()); // skinc

    Item weapon = eq.get(Equipment.SLOT_WEAPON);

    playerProps.putShort((short) Equipment.standAnimation(weapon)); // stand
    playerProps.putShort((short) 0x337); // stand turn
    playerProps.putShort((short) Equipment.walkAnimation(weapon)); // walk
    playerProps.putShort((short) 0x334); // turn 180
    playerProps.putShort((short) 0x335); // turn 90 cw
    playerProps.putShort((short) 0x336); // turn 90 ccw
    playerProps.putShort((short) Equipment.runAnimation(weapon)); // run

    playerProps.putLong(otherPlayer.getNameAsLong()); // player name
    playerProps.put((byte) otherPlayer.getSkills().getCombatLevel()); // combat level
    playerProps.putShort(
        0); // (skill-level instead of combat-level) otherPlayer.getSkills().getTotalLevel()); //
            // total level
    Packet propsPacket = playerProps.toPacket();
    packet.put((byte) (propsPacket.getLength() & 0xff));
    packet.putBytes(propsPacket.getPayload(), 0, propsPacket.getLength());
  }
  /**
   * 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());
    }
  }