コード例 #1
0
ファイル: ItemContainer.java プロジェクト: zografakos/filemon
  /** Get back items in container from database */
  public void restore() {
    Connection con = null;
    try {
      con = L2DatabaseFactory.getInstance().getConnection();
      PreparedStatement statement =
          con.prepareStatement(
              "SELECT object_id, item_id, count, enchant_level, loc, loc_data, custom_type1, custom_type2, mana_left, time FROM items WHERE owner_id=? AND (loc=?)");
      statement.setInt(1, getOwnerId());
      statement.setString(2, getBaseLocation().name());
      ResultSet inv = statement.executeQuery();

      L2ItemInstance item;
      while (inv.next()) {
        item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
        if (item == null) continue;

        L2World.getInstance().storeObject(item);

        L2PcInstance owner = getOwner() == null ? null : getOwner().getActingPlayer();

        // If stackable item is found in inventory just add to current quantity
        if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
          addItem("Restore", item, owner, null);
        else addItem(item);
      }

      inv.close();
      statement.close();
      refreshWeight();
    } catch (Exception e) {
      _log.log(Level.WARNING, "could not restore container:", e);
    } finally {
      L2DatabaseFactory.close(con);
    }
  }
コード例 #2
0
ファイル: ItemContainer.java プロジェクト: zografakos/filemon
  /**
   * Adds item to inventory
   *
   * @param process : String Identifier of process triggering this action
   * @param itemId : int Item Identifier of the item to be added
   * @param count : int Quantity of items to be added
   * @param actor : L2PcInstance Player requesting the item add
   * @param reference : Object Object referencing current action like NPC selling item or previous
   *     item in transformation
   * @return L2ItemInstance corresponding to the new item or the updated item in inventory
   */
  public L2ItemInstance addItem(
      String process, int itemId, long count, L2PcInstance actor, Object reference) {
    L2ItemInstance item = getItemByItemId(itemId);

    // If stackable item is found in inventory just add to current quantity
    if (item != null && item.isStackable()) {
      item.changeCount(process, count, actor, reference);
      item.setLastChange(L2ItemInstance.MODIFIED);
      // Updates database
      if (itemId == 57 && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(57)) {
        // Small adena changes won't be saved to database all the time
        if (GameTimeController.getGameTicks() % 5 == 0) item.updateDatabase();
      } else item.updateDatabase();
    }
    // If item hasn't be found in inventory, create new one
    else {
      for (int i = 0; i < count; i++) {
        L2Item template = ItemTable.getInstance().getTemplate(itemId);
        if (template == null) {
          _log.log(
              Level.WARNING,
              (actor != null ? "[" + actor.getName() + "] " : "") + "Invalid ItemId requested: ",
              itemId);
          return null;
        }

        item =
            ItemTable.getInstance()
                .createItem(process, itemId, template.isStackable() ? count : 1, actor, reference);
        item.setOwnerId(getOwnerId());
        item.setLocation(getBaseLocation());
        item.setLastChange(L2ItemInstance.ADDED);

        // Add item in inventory
        addItem(item);
        // Updates database
        item.updateDatabase();

        // If stackable, end loop as entire count is included in 1 instance of item
        if (template.isStackable() || !Config.MULTIPLE_ITEM_DROP) break;
      }
    }

    refreshWeight();
    return item;
  }
コード例 #3
0
ファイル: PcRefund.java プロジェクト: zografakos/filemon
 @Override
 protected void addItem(L2ItemInstance item) {
   super.addItem(item);
   try {
     if (getSize() > 12) {
       L2ItemInstance removedItem;
       synchronized (_items) {
         removedItem = _items.remove(0);
       }
       if (removedItem != null) {
         ItemTable.getInstance().destroyItem("ClearRefund", removedItem, getOwner(), null);
         removedItem.updateDatabase(true);
       }
     }
   } catch (Exception e) {
     _log.log(Level.SEVERE, "addItem()", e);
   }
 }
コード例 #4
0
ファイル: ItemContainer.java プロジェクト: zografakos/filemon
  /**
   * Adds item to inventory
   *
   * @param process : String Identifier of process triggering this action
   * @param item : L2ItemInstance to be added
   * @param actor : L2PcInstance Player requesting the item add
   * @param reference : Object Object referencing current action like NPC selling item or previous
   *     item in transformation
   * @return L2ItemInstance corresponding to the new item or the updated item in inventory
   */
  public L2ItemInstance addItem(
      String process, L2ItemInstance item, L2PcInstance actor, Object reference) {
    L2ItemInstance olditem = getItemByItemId(item.getItemId());

    // If stackable item is found in inventory just add to current quantity
    if (olditem != null && olditem.isStackable()) {
      long count = item.getCount();
      olditem.changeCount(process, count, actor, reference);
      olditem.setLastChange(L2ItemInstance.MODIFIED);

      // And destroys the item
      ItemTable.getInstance().destroyItem(process, item, actor, reference);
      item.updateDatabase();
      item = olditem;

      // Updates database
      if (item.getItemId() == 57 && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(57)) {
        // Small adena changes won't be saved to database all the time
        if (GameTimeController.getGameTicks() % 5 == 0) item.updateDatabase();
      } else item.updateDatabase();
    }
    // If item hasn't be found in inventory, create new one
    else {
      item.setOwnerId(process, getOwnerId(), actor, reference);
      item.setLocation(getBaseLocation());
      item.setLastChange((L2ItemInstance.ADDED));

      // Add item in inventory
      addItem(item);

      // Updates database
      item.updateDatabase();
    }

    refreshWeight();
    return item;
  }
コード例 #5
0
ファイル: ItemContainer.java プロジェクト: zografakos/filemon
  /**
   * Transfers item to another inventory
   *
   * @param process : String Identifier of process triggering this action
   * @param itemId : int Item Identifier of the item to be transfered
   * @param count : int Quantity of items to be transfered
   * @param actor : L2PcInstance Player requesting the item transfer
   * @param reference : Object Object referencing current action like NPC selling item or previous
   *     item in transformation
   * @return L2ItemInstance corresponding to the new item or the updated item in inventory
   */
  public L2ItemInstance transferItem(
      String process,
      int objectId,
      long count,
      ItemContainer target,
      L2PcInstance actor,
      Object reference) {
    if (target == null) {
      return null;
    }

    L2ItemInstance sourceitem = getItemByObjectId(objectId);
    if (sourceitem == null) {
      return null;
    }
    L2ItemInstance targetitem =
        sourceitem.isStackable() ? target.getItemByItemId(sourceitem.getItemId()) : null;

    synchronized (sourceitem) {
      // check if this item still present in this container
      if (getItemByObjectId(objectId) != sourceitem) {
        return null;
      }

      // Check if requested quantity is available
      if (count > sourceitem.getCount()) count = sourceitem.getCount();

      // If possible, move entire item object
      if (sourceitem.getCount() == count && targetitem == null) {
        removeItem(sourceitem);
        target.addItem(process, sourceitem, actor, reference);
        targetitem = sourceitem;
      } else {
        if (sourceitem.getCount() > count) // If possible, only update counts
        {
          sourceitem.changeCount(process, -count, actor, reference);
        } else
        // Otherwise destroy old item
        {
          removeItem(sourceitem);
          ItemTable.getInstance().destroyItem(process, sourceitem, actor, reference);
        }

        if (targetitem != null) // If possible, only update counts
        {
          targetitem.changeCount(process, count, actor, reference);
        } else
        // Otherwise add new item
        {
          targetitem = target.addItem(process, sourceitem.getItemId(), count, actor, reference);
        }
      }

      // Updates database
      sourceitem.updateDatabase(true);
      if (targetitem != sourceitem && targetitem != null) targetitem.updateDatabase();
      if (sourceitem.isAugmented()) sourceitem.getAugmentation().removeBonus(actor);
      refreshWeight();
      target.refreshWeight();
    }
    return targetitem;
  }