ItemContainer.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.itemcontainer;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.GameTimeController;
  26. import com.l2jserver.gameserver.datatables.ItemTable;
  27. import com.l2jserver.gameserver.model.L2ItemInstance;
  28. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.L2World;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.templates.item.L2Item;
  34. /**
  35. * @author Advi
  36. */
  37. public abstract class ItemContainer
  38. {
  39. protected static final Logger _log = Logger.getLogger(ItemContainer.class.getName());
  40. protected final List<L2ItemInstance> _items;
  41. protected ItemContainer()
  42. {
  43. _items = new FastList<L2ItemInstance>().shared();
  44. }
  45. protected abstract L2Character getOwner();
  46. protected abstract ItemLocation getBaseLocation();
  47. public String getName()
  48. {
  49. return "ItemContainer";
  50. }
  51. /**
  52. * Returns the ownerID of the inventory
  53. * @return int
  54. */
  55. public int getOwnerId()
  56. {
  57. return getOwner() == null ? 0 : getOwner().getObjectId();
  58. }
  59. /**
  60. * Returns the quantity of items in the inventory
  61. * @return int
  62. */
  63. public int getSize()
  64. {
  65. return _items.size();
  66. }
  67. /**
  68. * Returns the list of items in inventory
  69. * @return L2ItemInstance : items in inventory
  70. */
  71. public L2ItemInstance[] getItems()
  72. {
  73. return _items.toArray(new L2ItemInstance[_items.size()]);
  74. }
  75. /**
  76. * Returns the item from inventory by using its <B>itemId</B><BR><BR>
  77. *
  78. * @param itemId : int designating the ID of the item
  79. * @return L2ItemInstance designating the item or null if not found in inventory
  80. */
  81. public L2ItemInstance getItemByItemId(int itemId)
  82. {
  83. for (L2ItemInstance item : _items)
  84. if (item != null && item.getItemId() == itemId)
  85. return item;
  86. return null;
  87. }
  88. /**
  89. * Returns the item's list from inventory by using its <B>itemId</B><BR><BR>
  90. *
  91. * @param itemId : int designating the ID of the item
  92. * @return List<L2ItemInstance> designating the items list (empty list if not found)
  93. */
  94. public List<L2ItemInstance> getItemsByItemId(int itemId)
  95. {
  96. List<L2ItemInstance> returnList = new FastList<L2ItemInstance>();
  97. for (L2ItemInstance item : _items)
  98. {
  99. if (item != null && item.getItemId() == itemId)
  100. {
  101. returnList.add(item);
  102. }
  103. }
  104. return returnList;
  105. }
  106. /**
  107. * Returns the item from inventory by using its <B>itemId</B><BR><BR>
  108. *
  109. * @param itemId : int designating the ID of the item
  110. * @param itemToIgnore : used during a loop, to avoid returning the same item
  111. * @return L2ItemInstance designating the item or null if not found in inventory
  112. */
  113. public L2ItemInstance getItemByItemId(int itemId, L2ItemInstance itemToIgnore)
  114. {
  115. for (L2ItemInstance item : _items)
  116. if (item != null && item.getItemId() == itemId && !item.equals(itemToIgnore))
  117. return item;
  118. return null;
  119. }
  120. /**
  121. * Returns item from inventory by using its <B>objectId</B>
  122. * @param objectId : int designating the ID of the object
  123. * @return L2ItemInstance designating the item or null if not found in inventory
  124. */
  125. public L2ItemInstance getItemByObjectId(int objectId)
  126. {
  127. for (L2ItemInstance item : _items)
  128. {
  129. if (item == null)
  130. continue;
  131. if (item.getObjectId() == objectId)
  132. return item;
  133. }
  134. return null;
  135. }
  136. /**
  137. * @param itemId
  138. * @param enchantLevel
  139. * @return
  140. * @see com.l2jserver.gameserver.model.itemcontainer.ItemContainer#getInventoryItemCount(int, int, boolean)
  141. */
  142. public long getInventoryItemCount(int itemId, int enchantLevel)
  143. {
  144. return getInventoryItemCount(itemId, enchantLevel, true);
  145. }
  146. /**
  147. * Gets count of item in the inventory
  148. * @param itemId : Item to look for
  149. * @param enchantLevel : enchant level to match on, or -1 for ANY enchant level
  150. * @param includeEquipped : include equipped items
  151. * @return int corresponding to the number of items matching the above conditions.
  152. */
  153. public long getInventoryItemCount(int itemId, int enchantLevel, boolean includeEquipped)
  154. {
  155. long count = 0;
  156. for (L2ItemInstance item : _items)
  157. if (item.getItemId() == itemId && ((item.getEnchantLevel() == enchantLevel) || (enchantLevel < 0)) && (includeEquipped || !item.isEquipped()))
  158. //if (item.isAvailable((L2PcInstance)getOwner(), true) || item.getItem().getType2() == 3)//available or quest item
  159. if (item.isStackable())
  160. count = item.getCount();
  161. else
  162. count++;
  163. return count;
  164. }
  165. /**
  166. * Adds item to inventory
  167. * @param process : String Identifier of process triggering this action
  168. * @param item : L2ItemInstance to be added
  169. * @param actor : L2PcInstance Player requesting the item add
  170. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  171. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  172. */
  173. public L2ItemInstance addItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  174. {
  175. L2ItemInstance olditem = getItemByItemId(item.getItemId());
  176. // If stackable item is found in inventory just add to current quantity
  177. if (olditem != null && olditem.isStackable())
  178. {
  179. long count = item.getCount();
  180. olditem.changeCount(process, count, actor, reference);
  181. olditem.setLastChange(L2ItemInstance.MODIFIED);
  182. // And destroys the item
  183. ItemTable.getInstance().destroyItem(process, item, actor, reference);
  184. item.updateDatabase();
  185. item = olditem;
  186. // Updates database
  187. if (item.getItemId() == PcInventory.ADENA_ID && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(PcInventory.ADENA_ID))
  188. {
  189. // Small adena changes won't be saved to database all the time
  190. if (GameTimeController.getGameTicks() % 5 == 0)
  191. item.updateDatabase();
  192. }
  193. else
  194. item.updateDatabase();
  195. }
  196. // If item hasn't be found in inventory, create new one
  197. else
  198. {
  199. item.setOwnerId(process, getOwnerId(), actor, reference);
  200. item.setLocation(getBaseLocation());
  201. item.setLastChange((L2ItemInstance.ADDED));
  202. // Add item in inventory
  203. addItem(item);
  204. // Updates database
  205. item.updateDatabase();
  206. }
  207. refreshWeight();
  208. return item;
  209. }
  210. /**
  211. * Adds item to inventory
  212. * @param process : String Identifier of process triggering this action
  213. * @param itemId : int Item Identifier of the item to be added
  214. * @param count : int Quantity of items to be added
  215. * @param actor : L2PcInstance Player requesting the item add
  216. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  217. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  218. */
  219. public L2ItemInstance addItem(String process, int itemId, long count, L2PcInstance actor, Object reference)
  220. {
  221. L2ItemInstance item = getItemByItemId(itemId);
  222. // If stackable item is found in inventory just add to current quantity
  223. if (item != null && item.isStackable())
  224. {
  225. item.changeCount(process, count, actor, reference);
  226. item.setLastChange(L2ItemInstance.MODIFIED);
  227. // Updates database
  228. if (itemId == PcInventory.ADENA_ID && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(PcInventory.ADENA_ID))
  229. {
  230. // Small adena changes won't be saved to database all the time
  231. if (GameTimeController.getGameTicks() % 5 == 0)
  232. item.updateDatabase();
  233. }
  234. else
  235. item.updateDatabase();
  236. }
  237. // If item hasn't be found in inventory, create new one
  238. else
  239. {
  240. for (int i = 0; i < count; i++)
  241. {
  242. L2Item template = ItemTable.getInstance().getTemplate(itemId);
  243. if (template == null)
  244. {
  245. _log.log(Level.WARNING, (actor != null ? "[" + actor.getName() + "] " : "") + "Invalid ItemId requested: ", itemId);
  246. return null;
  247. }
  248. item = ItemTable.getInstance().createItem(process, itemId, template.isStackable() ? count : 1, actor, reference);
  249. item.setOwnerId(getOwnerId());
  250. item.setLocation(getBaseLocation());
  251. item.setLastChange(L2ItemInstance.ADDED);
  252. // Add item in inventory
  253. addItem(item);
  254. // Updates database
  255. item.updateDatabase();
  256. // If stackable, end loop as entire count is included in 1 instance of item
  257. if (template.isStackable() || !Config.MULTIPLE_ITEM_DROP)
  258. break;
  259. }
  260. }
  261. refreshWeight();
  262. return item;
  263. }
  264. /**
  265. * Transfers item to another inventory
  266. * @param process string Identifier of process triggering this action
  267. * @param objectId Item Identifier of the item to be transfered
  268. * @param count Quantity of items to be transfered
  269. * @param target the item container where the item will be moved.
  270. * @param actor Player requesting the item transfer
  271. * @param reference Object Object referencing current action like NPC selling item or previous item in transformation
  272. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  273. */
  274. public L2ItemInstance transferItem(String process, int objectId, long count, ItemContainer target, L2PcInstance actor, Object reference)
  275. {
  276. if (target == null)
  277. {
  278. return null;
  279. }
  280. L2ItemInstance sourceitem = getItemByObjectId(objectId);
  281. if (sourceitem == null)
  282. {
  283. return null;
  284. }
  285. L2ItemInstance targetitem = sourceitem.isStackable() ? target.getItemByItemId(sourceitem.getItemId()) : null;
  286. synchronized (sourceitem)
  287. {
  288. // check if this item still present in this container
  289. if (getItemByObjectId(objectId) != sourceitem)
  290. {
  291. return null;
  292. }
  293. // Check if requested quantity is available
  294. if (count > sourceitem.getCount())
  295. count = sourceitem.getCount();
  296. // If possible, move entire item object
  297. if (sourceitem.getCount() == count && targetitem == null)
  298. {
  299. removeItem(sourceitem);
  300. target.addItem(process, sourceitem, actor, reference);
  301. targetitem = sourceitem;
  302. }
  303. else
  304. {
  305. if (sourceitem.getCount() > count) // If possible, only update counts
  306. {
  307. sourceitem.changeCount(process, -count, actor, reference);
  308. }
  309. else
  310. // Otherwise destroy old item
  311. {
  312. removeItem(sourceitem);
  313. ItemTable.getInstance().destroyItem(process, sourceitem, actor, reference);
  314. }
  315. if (targetitem != null) // If possible, only update counts
  316. {
  317. targetitem.changeCount(process, count, actor, reference);
  318. }
  319. else
  320. // Otherwise add new item
  321. {
  322. targetitem = target.addItem(process, sourceitem.getItemId(), count, actor, reference);
  323. }
  324. }
  325. // Updates database
  326. sourceitem.updateDatabase(true);
  327. if (targetitem != sourceitem && targetitem != null)
  328. targetitem.updateDatabase();
  329. if (sourceitem.isAugmented())
  330. sourceitem.getAugmentation().removeBonus(actor);
  331. refreshWeight();
  332. target.refreshWeight();
  333. }
  334. return targetitem;
  335. }
  336. /**
  337. * Destroy item from inventory and updates database
  338. * @param process : String Identifier of process triggering this action
  339. * @param item : L2ItemInstance to be destroyed
  340. * @param actor : L2PcInstance Player requesting the item destroy
  341. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  342. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  343. */
  344. public L2ItemInstance destroyItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  345. {
  346. return this.destroyItem(process, item, item.getCount(), actor, reference);
  347. }
  348. /**
  349. * Destroy item from inventory and updates database
  350. * @param process : String Identifier of process triggering this action
  351. * @param item : L2ItemInstance to be destroyed
  352. * @param count
  353. * @param actor : L2PcInstance Player requesting the item destroy
  354. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  355. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  356. */
  357. public L2ItemInstance destroyItem(String process, L2ItemInstance item, long count, L2PcInstance actor, Object reference)
  358. {
  359. synchronized (item)
  360. {
  361. // Adjust item quantity
  362. if (item.getCount() > count)
  363. {
  364. item.changeCount(process, -count, actor, reference);
  365. item.setLastChange(L2ItemInstance.MODIFIED);
  366. // don't update often for untraced items
  367. if (process != null || GameTimeController.getGameTicks() % 10 == 0)
  368. {
  369. item.updateDatabase();
  370. }
  371. refreshWeight();
  372. }
  373. else
  374. {
  375. if (item.getCount() < count)
  376. return null;
  377. boolean removed = this.removeItem(item);
  378. if (!removed)
  379. return null;
  380. ItemTable.getInstance().destroyItem(process, item, actor, reference);
  381. item.updateDatabase();
  382. refreshWeight();
  383. }
  384. }
  385. return item;
  386. }
  387. /**
  388. * Destroy item from inventory by using its <B>objectID</B> and updates database
  389. * @param process : String Identifier of process triggering this action
  390. * @param objectId : int Item Instance identifier of the item to be destroyed
  391. * @param count : int Quantity of items to be destroyed
  392. * @param actor : L2PcInstance Player requesting the item destroy
  393. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  394. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  395. */
  396. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, Object reference)
  397. {
  398. L2ItemInstance item = getItemByObjectId(objectId);
  399. if (item == null)
  400. {
  401. return null;
  402. }
  403. return this.destroyItem(process, item, count, actor, reference);
  404. }
  405. /**
  406. * Destroy item from inventory by using its <B>itemId</B> and updates database
  407. * @param process : String Identifier of process triggering this action
  408. * @param itemId : int Item identifier of the item to be destroyed
  409. * @param count : int Quantity of items to be destroyed
  410. * @param actor : L2PcInstance Player requesting the item destroy
  411. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  412. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  413. */
  414. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, Object reference)
  415. {
  416. L2ItemInstance item = getItemByItemId(itemId);
  417. if (item == null)
  418. {
  419. return null;
  420. }
  421. return this.destroyItem(process, item, count, actor, reference);
  422. }
  423. /**
  424. * Destroy all items from inventory and updates database
  425. * @param process : String Identifier of process triggering this action
  426. * @param actor : L2PcInstance Player requesting the item destroy
  427. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  428. */
  429. public void destroyAllItems(String process, L2PcInstance actor, Object reference)
  430. {
  431. for (L2ItemInstance item : _items)
  432. {
  433. if (item != null)
  434. destroyItem(process, item, actor, reference);
  435. }
  436. }
  437. /**
  438. * @return warehouse Adena.
  439. */
  440. public long getAdena()
  441. {
  442. long count = 0;
  443. for (L2ItemInstance item : _items)
  444. {
  445. if (item != null && item.getItemId() == PcInventory.ADENA_ID)
  446. {
  447. count = item.getCount();
  448. return count;
  449. }
  450. }
  451. return count;
  452. }
  453. /**
  454. * Adds item to inventory for further adjustments.
  455. * @param item : L2ItemInstance to be added from inventory
  456. */
  457. protected void addItem(L2ItemInstance item)
  458. {
  459. _items.add(item);
  460. }
  461. /**
  462. * Removes item from inventory for further adjustments.
  463. * @param item : L2ItemInstance to be removed from inventory
  464. * @return
  465. */
  466. protected boolean removeItem(L2ItemInstance item)
  467. {
  468. return _items.remove(item);
  469. }
  470. /**
  471. * Refresh the weight of equipment loaded
  472. */
  473. protected void refreshWeight()
  474. {
  475. }
  476. /**
  477. * Delete item object from world
  478. */
  479. public void deleteMe()
  480. {
  481. try
  482. {
  483. updateDatabase();
  484. }
  485. catch (Exception e)
  486. {
  487. _log.log(Level.SEVERE, "deletedMe()", e);
  488. }
  489. List<L2Object> items = new FastList<L2Object>(_items);
  490. _items.clear();
  491. L2World.getInstance().removeObjects(items);
  492. }
  493. /**
  494. * Update database with items in inventory
  495. */
  496. public void updateDatabase()
  497. {
  498. if (getOwner() != null)
  499. {
  500. for (L2ItemInstance item : _items)
  501. {
  502. if (item != null)
  503. {
  504. item.updateDatabase(true);
  505. }
  506. }
  507. }
  508. }
  509. /**
  510. * Get back items in container from database
  511. */
  512. public void restore()
  513. {
  514. Connection con = null;
  515. try
  516. {
  517. con = L2DatabaseFactory.getInstance().getConnection();
  518. 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=?)");
  519. statement.setInt(1, getOwnerId());
  520. statement.setString(2, getBaseLocation().name());
  521. ResultSet inv = statement.executeQuery();
  522. L2ItemInstance item;
  523. while (inv.next())
  524. {
  525. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  526. if (item == null)
  527. continue;
  528. L2World.getInstance().storeObject(item);
  529. L2PcInstance owner = getOwner() == null ? null : getOwner().getActingPlayer();
  530. // If stackable item is found in inventory just add to current quantity
  531. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  532. addItem("Restore", item, owner, null);
  533. else
  534. addItem(item);
  535. }
  536. inv.close();
  537. statement.close();
  538. refreshWeight();
  539. }
  540. catch (Exception e)
  541. {
  542. _log.log(Level.WARNING, "could not restore container:", e);
  543. }
  544. finally
  545. {
  546. L2DatabaseFactory.close(con);
  547. }
  548. }
  549. public boolean validateCapacity(int slots)
  550. {
  551. return true;
  552. }
  553. public boolean validateWeight(int weight)
  554. {
  555. return true;
  556. }
  557. }