Пример #1
0
  @Override
  public boolean testImpl(L2Character effector, L2Character effected, Skill skill, L2Item item) {
    if ((effector == null) || !effector.isPlayer()) {
      return false;
    }

    final Inventory inv = effector.getInventory();
    // If ConditionUsingItemType is one between Light, Heavy or Magic
    if (_armor) {
      // Get the itemMask of the weared chest (if exists)
      final L2ItemInstance chest = inv.getPaperdollItem(Inventory.PAPERDOLL_CHEST);
      if (chest == null) {
        return false;
      }
      final int chestMask = chest.getItem().getItemMask();

      // If chest armor is different from the condition one return false
      if ((_mask & chestMask) == 0) {
        return false;
      }

      // So from here, chest armor matches conditions

      final int chestBodyPart = chest.getItem().getBodyPart();
      // return True if chest armor is a Full Armor
      if (chestBodyPart == L2Item.SLOT_FULL_ARMOR) {
        return true;
      }
      // check legs armor
      final L2ItemInstance legs = inv.getPaperdollItem(Inventory.PAPERDOLL_LEGS);
      if (legs == null) {
        return false;
      }
      final int legMask = legs.getItem().getItemMask();
      // return true if legs armor matches too
      return (_mask & legMask) != 0;
    }
    return (_mask & inv.getWearedMask()) != 0;
  }
  @Override
  public void runImpl() {
    final L2PcInstance activeChar = getClient().getActiveChar();
    if ((activeChar == null) || !Config.ALLOW_MAIL || !Config.ALLOW_ATTACHMENTS) {
      return;
    }

    if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("cancelpost")) {
      return;
    }

    final Message msg = MailManager.getInstance().getMessage(_msgId);
    if (msg == null) {
      return;
    }
    if (msg.getSenderId() != activeChar.getObjectId()) {
      Util.handleIllegalPlayerAction(
          activeChar,
          "Player " + activeChar.getName() + " tried to cancel not own post!",
          Config.DEFAULT_PUNISH);
      return;
    }

    if (!activeChar.isInsideZone(ZoneId.PEACE)) {
      activeChar.sendPacket(SystemMessageId.YOU_CANNOT_CANCEL_IN_A_NON_PEACE_ZONE_LOCATION);
      return;
    }

    if (activeChar.getActiveTradeList() != null) {
      activeChar.sendPacket(SystemMessageId.YOU_CANNOT_CANCEL_DURING_AN_EXCHANGE);
      return;
    }

    if (activeChar.hasItemRequest()) {
      activeChar.sendPacket(
          SystemMessageId.YOU_CANNOT_CANCEL_DURING_AN_ITEM_ENHANCEMENT_OR_ATTRIBUTE_ENHANCEMENT);
      return;
    }

    if (activeChar.getPrivateStoreType() != PrivateStoreType.NONE) {
      activeChar.sendPacket(
          SystemMessageId.YOU_CANNOT_CANCEL_BECAUSE_THE_PRIVATE_STORE_OR_WORKSHOP_IS_IN_PROGRESS);
      return;
    }

    if (!msg.hasAttachments()) {
      activeChar.sendPacket(
          SystemMessageId.YOU_CANNOT_CANCEL_SENT_MAIL_SINCE_THE_RECIPIENT_RECEIVED_IT);
      return;
    }

    final ItemContainer attachments = msg.getAttachments();
    if ((attachments == null) || (attachments.getSize() == 0)) {
      activeChar.sendPacket(
          SystemMessageId.YOU_CANNOT_CANCEL_SENT_MAIL_SINCE_THE_RECIPIENT_RECEIVED_IT);
      return;
    }

    int weight = 0;
    int slots = 0;

    for (L2ItemInstance item : attachments.getItems()) {
      if (item == null) {
        continue;
      }

      if (item.getOwnerId() != activeChar.getObjectId()) {
        Util.handleIllegalPlayerAction(
            activeChar,
            "Player "
                + activeChar.getName()
                + " tried to get not own item from cancelled attachment!",
            Config.DEFAULT_PUNISH);
        return;
      }

      if (item.getItemLocation() != ItemLocation.MAIL) {
        Util.handleIllegalPlayerAction(
            activeChar,
            "Player " + activeChar.getName() + " tried to get items not from mail !",
            Config.DEFAULT_PUNISH);
        return;
      }

      if (item.getLocationSlot() != msg.getId()) {
        Util.handleIllegalPlayerAction(
            activeChar,
            "Player " + activeChar.getName() + " tried to get items from different attachment!",
            Config.DEFAULT_PUNISH);
        return;
      }

      weight += item.getCount() * item.getItem().getWeight();
      if (!item.isStackable()) {
        slots += item.getCount();
      } else if (activeChar.getInventory().getItemByItemId(item.getId()) == null) {
        slots++;
      }
    }

    if (!activeChar.getInventory().validateCapacity(slots)) {
      activeChar.sendPacket(
          SystemMessageId.YOU_COULD_NOT_CANCEL_RECEIPT_BECAUSE_YOUR_INVENTORY_IS_FULL);
      return;
    }

    if (!activeChar.getInventory().validateWeight(weight)) {
      activeChar.sendPacket(
          SystemMessageId.YOU_COULD_NOT_CANCEL_RECEIPT_BECAUSE_YOUR_INVENTORY_IS_FULL);
      return;
    }

    // Proceed to the transfer
    final InventoryUpdate playerIU = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
    for (L2ItemInstance item : attachments.getItems()) {
      if (item == null) {
        continue;
      }

      final long count = item.getCount();
      final L2ItemInstance newItem =
          attachments.transferItem(
              attachments.getName(),
              item.getObjectId(),
              count,
              activeChar.getInventory(),
              activeChar,
              null);
      if (newItem == null) {
        return;
      }

      if (playerIU != null) {
        if (newItem.getCount() > count) {
          playerIU.addModifiedItem(newItem);
        } else {
          playerIU.addNewItem(newItem);
        }
      }
      final SystemMessage sm =
          SystemMessage.getSystemMessage(SystemMessageId.YOU_HAVE_ACQUIRED_S2_S1);
      sm.addItemName(item.getId());
      sm.addLong(count);
      activeChar.sendPacket(sm);
    }

    msg.removeAttachments();

    // Send updated item list to the player
    if (playerIU != null) {
      activeChar.sendPacket(playerIU);
    } else {
      activeChar.sendPacket(new ItemList(activeChar, false));
    }

    // Update current load status on player
    activeChar.sendPacket(new ExUserInfoInvenWeight(activeChar));

    final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
    if (receiver != null) {
      final SystemMessage sm =
          SystemMessage.getSystemMessage(SystemMessageId.S1_CANCELED_THE_SENT_MAIL);
      sm.addCharName(activeChar);
      receiver.sendPacket(sm);
      receiver.sendPacket(new ExChangePostState(true, _msgId, Message.DELETED));
    }

    MailManager.getInstance().deleteMessageInDb(_msgId);

    activeChar.sendPacket(new ExChangePostState(false, _msgId, Message.DELETED));
    activeChar.sendPacket(SystemMessageId.MAIL_SUCCESSFULLY_CANCELLED);
  }