2
0

ItemContainer.java 20 KB

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