ItemContainer.java 19 KB

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