ItemContainer.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.GameTimeController;
  25. import com.l2jserver.gameserver.datatables.ItemTable;
  26. import com.l2jserver.gameserver.model.L2ItemInstance;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.templates.item.L2Item;
  33. import javolution.util.FastList;
  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 : L2Object 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, L2Object 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 : L2Object 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, L2Object 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. * Adds Wear/Try On item to inventory<BR><BR>
  264. *
  265. * @param process : String Identifier of process triggering this action
  266. * @param itemId : int Item Identifier of the item to be added
  267. * @param actor : L2PcInstance Player requesting the item add
  268. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  269. * @return L2ItemInstance corresponding to the new weared item
  270. */
  271. public L2ItemInstance addWearItem(String process, int itemId, L2PcInstance actor, L2Object reference)
  272. {
  273. // Surch the item in the inventory of the player
  274. L2ItemInstance item = getItemByItemId(itemId);
  275. // There is such item already in inventory
  276. if (item != null)
  277. return item;
  278. // Create and Init the L2ItemInstance corresponding to the Item Identifier and quantity
  279. // Add the L2ItemInstance object to _allObjects of L2world
  280. item = ItemTable.getInstance().createItem(process, itemId, 1, actor, reference);
  281. // Set Item Properties
  282. item.setWear(true); // "Try On" Item -> Don't save it in database
  283. item.setOwnerId(getOwnerId());
  284. item.setLocation(getBaseLocation());
  285. item.setLastChange((L2ItemInstance.ADDED));
  286. // Add item in inventory and equip it if necessary (item location defined)
  287. addItem(item);
  288. // Calculate the weight loaded by player
  289. refreshWeight();
  290. return item;
  291. }
  292. /**
  293. * Transfers item to another inventory
  294. * @param process : String Identifier of process triggering this action
  295. * @param itemId : int Item Identifier of the item to be transfered
  296. * @param count : int Quantity of items to be transfered
  297. * @param actor : L2PcInstance Player requesting the item transfer
  298. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  299. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  300. */
  301. public L2ItemInstance transferItem(String process, int objectId, long count, ItemContainer target, L2PcInstance actor, L2Object reference)
  302. {
  303. if (target == null)
  304. {
  305. return null;
  306. }
  307. L2ItemInstance sourceitem = getItemByObjectId(objectId);
  308. if (sourceitem == null)
  309. {
  310. return null;
  311. }
  312. L2ItemInstance targetitem = sourceitem.isStackable() ? target.getItemByItemId(sourceitem.getItemId()) : null;
  313. synchronized (sourceitem)
  314. {
  315. // check if this item still present in this container
  316. if (getItemByObjectId(objectId) != sourceitem)
  317. {
  318. return null;
  319. }
  320. // Check if requested quantity is available
  321. if (count > sourceitem.getCount())
  322. count = sourceitem.getCount();
  323. // If possible, move entire item object
  324. if (sourceitem.getCount() == count && targetitem == null)
  325. {
  326. removeItem(sourceitem);
  327. target.addItem(process, sourceitem, actor, reference);
  328. targetitem = sourceitem;
  329. }
  330. else
  331. {
  332. if (sourceitem.getCount() > count) // If possible, only update counts
  333. {
  334. sourceitem.changeCount(process, -count, actor, reference);
  335. }
  336. else
  337. // Otherwise destroy old item
  338. {
  339. removeItem(sourceitem);
  340. ItemTable.getInstance().destroyItem(process, sourceitem, actor, reference);
  341. }
  342. if (targetitem != null) // If possible, only update counts
  343. {
  344. targetitem.changeCount(process, count, actor, reference);
  345. }
  346. else
  347. // Otherwise add new item
  348. {
  349. targetitem = target.addItem(process, sourceitem.getItemId(), count, actor, reference);
  350. }
  351. }
  352. // Updates database
  353. sourceitem.updateDatabase(true);
  354. if (targetitem != sourceitem && targetitem != null)
  355. targetitem.updateDatabase();
  356. if (sourceitem.isAugmented())
  357. sourceitem.getAugmentation().removeBonus(actor);
  358. refreshWeight();
  359. target.refreshWeight();
  360. }
  361. return targetitem;
  362. }
  363. /**
  364. * Destroy item from inventory and updates database
  365. * @param process : String Identifier of process triggering this action
  366. * @param item : L2ItemInstance to be destroyed
  367. * @param actor : L2PcInstance Player requesting the item destroy
  368. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  369. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  370. */
  371. public L2ItemInstance destroyItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  372. {
  373. return this.destroyItem(process, item, item.getCount(), actor, reference);
  374. }
  375. /**
  376. * Destroy item from inventory and updates database
  377. * @param process : String Identifier of process triggering this action
  378. * @param item : L2ItemInstance to be destroyed
  379. * @param actor : L2PcInstance Player requesting the item destroy
  380. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  381. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  382. */
  383. public L2ItemInstance destroyItem(String process, L2ItemInstance item, long count, L2PcInstance actor, L2Object reference)
  384. {
  385. synchronized (item)
  386. {
  387. // Adjust item quantity
  388. if (item.getCount() > count)
  389. {
  390. item.changeCount(process, -count, actor, reference);
  391. item.setLastChange(L2ItemInstance.MODIFIED);
  392. // don't update often for untraced items
  393. if (process != null || GameTimeController.getGameTicks() % 10 == 0)
  394. {
  395. item.updateDatabase();
  396. }
  397. refreshWeight();
  398. return item;
  399. }
  400. else
  401. {
  402. if (item.getCount() < count)
  403. return null;
  404. boolean removed = this.removeItem(item);
  405. if (!removed)
  406. return null;
  407. ItemTable.getInstance().destroyItem(process, item, actor, reference);
  408. item.updateDatabase();
  409. refreshWeight();
  410. }
  411. }
  412. return item;
  413. }
  414. /**
  415. * Destroy item from inventory by using its <B>objectID</B> and updates database
  416. * @param process : String Identifier of process triggering this action
  417. * @param objectId : int Item Instance identifier of the item to be destroyed
  418. * @param count : int Quantity of items to be destroyed
  419. * @param actor : L2PcInstance Player requesting the item destroy
  420. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  421. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  422. */
  423. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, L2Object reference)
  424. {
  425. L2ItemInstance item = getItemByObjectId(objectId);
  426. if (item == null)
  427. {
  428. return null;
  429. }
  430. return this.destroyItem(process, item, count, actor, reference);
  431. }
  432. /**
  433. * Destroy item from inventory by using its <B>itemId</B> and updates database
  434. * @param process : String Identifier of process triggering this action
  435. * @param itemId : int Item identifier of the item to be destroyed
  436. * @param count : int Quantity of items to be destroyed
  437. * @param actor : L2PcInstance Player requesting the item destroy
  438. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  439. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  440. */
  441. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, L2Object reference)
  442. {
  443. L2ItemInstance item = getItemByItemId(itemId);
  444. if (item == null)
  445. {
  446. return null;
  447. }
  448. return this.destroyItem(process, item, count, actor, reference);
  449. }
  450. /**
  451. * Destroy all items from inventory and updates database
  452. * @param process : String Identifier of process triggering this action
  453. * @param actor : L2PcInstance Player requesting the item destroy
  454. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  455. */
  456. public synchronized void destroyAllItems(String process, L2PcInstance actor, L2Object reference)
  457. {
  458. for (L2ItemInstance item : _items)
  459. {
  460. if (item != null)
  461. destroyItem(process, item, actor, reference);
  462. }
  463. }
  464. /**
  465. * Get warehouse adena
  466. */
  467. public long getAdena()
  468. {
  469. long count = 0;
  470. for (L2ItemInstance item : _items)
  471. {
  472. if (item != null && item.getItemId() == 57)
  473. {
  474. count = item.getCount();
  475. return count;
  476. }
  477. }
  478. return count;
  479. }
  480. /**
  481. * Adds item to inventory for further adjustments.
  482. * @param item : L2ItemInstance to be added from inventory
  483. */
  484. protected void addItem(L2ItemInstance item)
  485. {
  486. synchronized (_items)
  487. {
  488. _items.add(item);
  489. }
  490. }
  491. /**
  492. * Removes item from inventory for further adjustments.
  493. * @param item : L2ItemInstance to be removed from inventory
  494. */
  495. protected boolean removeItem(L2ItemInstance item)
  496. {
  497. synchronized (_items)
  498. {
  499. return _items.remove(item);
  500. }
  501. }
  502. /**
  503. * Refresh the weight of equipment loaded
  504. */
  505. protected void refreshWeight()
  506. {
  507. }
  508. /**
  509. * Delete item object from world
  510. */
  511. public void deleteMe()
  512. {
  513. try
  514. {
  515. updateDatabase();
  516. }
  517. catch (Exception e)
  518. {
  519. _log.log(Level.SEVERE, "deletedMe()", e);
  520. }
  521. List<L2Object> items = new FastList<L2Object>(_items);
  522. _items.clear();
  523. L2World.getInstance().removeObjects(items);
  524. }
  525. /**
  526. * Update database with items in inventory
  527. */
  528. public void updateDatabase()
  529. {
  530. if (getOwner() != null)
  531. {
  532. for (L2ItemInstance item : _items)
  533. {
  534. if (item != null)
  535. {
  536. item.updateDatabase(true);
  537. }
  538. }
  539. }
  540. }
  541. /**
  542. * Get back items in container from database
  543. */
  544. public void restore()
  545. {
  546. Connection con = null;
  547. try
  548. {
  549. con = L2DatabaseFactory.getInstance().getConnection();
  550. 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=?)");
  551. statement.setInt(1, getOwnerId());
  552. statement.setString(2, getBaseLocation().name());
  553. ResultSet inv = statement.executeQuery();
  554. L2ItemInstance item;
  555. while (inv.next())
  556. {
  557. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  558. if (item == null)
  559. continue;
  560. L2World.getInstance().storeObject(item);
  561. L2PcInstance owner = getOwner() == null ? null : getOwner().getActingPlayer();
  562. // If stackable item is found in inventory just add to current quantity
  563. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  564. addItem("Restore", item, owner, null);
  565. else
  566. addItem(item);
  567. }
  568. inv.close();
  569. statement.close();
  570. refreshWeight();
  571. }
  572. catch (Exception e)
  573. {
  574. _log.log(Level.WARNING, "could not restore container:", e);
  575. }
  576. finally
  577. {
  578. L2DatabaseFactory.close(con);
  579. }
  580. }
  581. public boolean validateCapacity(int slots)
  582. {
  583. return true;
  584. }
  585. public boolean validateWeight(int weight)
  586. {
  587. return true;
  588. }
  589. }