ItemContainer.java 20 KB

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