ItemContainer.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Copyright (C) 2004-2014 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.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastList;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.GameTimeController;
  31. import com.l2jserver.gameserver.datatables.ItemTable;
  32. import com.l2jserver.gameserver.enums.ItemLocation;
  33. import com.l2jserver.gameserver.model.L2World;
  34. import com.l2jserver.gameserver.model.actor.L2Character;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.items.L2Item;
  37. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  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 = new FastList<L2ItemInstance>().shared();
  45. protected ItemContainer()
  46. {
  47. }
  48. protected abstract L2Character getOwner();
  49. protected abstract ItemLocation getBaseLocation();
  50. public String getName()
  51. {
  52. return "ItemContainer";
  53. }
  54. /**
  55. * @return int the owner object Id
  56. */
  57. public int getOwnerId()
  58. {
  59. return getOwner() == null ? 0 : getOwner().getObjectId();
  60. }
  61. /**
  62. * @return the quantity of items in the inventory
  63. */
  64. public int getSize()
  65. {
  66. return _items.size();
  67. }
  68. /**
  69. * @return the items in inventory
  70. */
  71. public L2ItemInstance[] getItems()
  72. {
  73. return _items.toArray(new L2ItemInstance[_items.size()]);
  74. }
  75. /**
  76. * @param itemId the item Id
  77. * @return the item from inventory by itemId
  78. */
  79. public L2ItemInstance getItemByItemId(int itemId)
  80. {
  81. for (L2ItemInstance item : _items)
  82. {
  83. if ((item != null) && (item.getId() == itemId))
  84. {
  85. return item;
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * @param itemId the item Id
  92. * @return the items list from inventory by using its itemId
  93. */
  94. public List<L2ItemInstance> getItemsByItemId(int itemId)
  95. {
  96. final List<L2ItemInstance> returnList = new ArrayList<>();
  97. for (L2ItemInstance item : _items)
  98. {
  99. if ((item != null) && (item.getId() == itemId))
  100. {
  101. returnList.add(item);
  102. }
  103. }
  104. return returnList;
  105. }
  106. /**
  107. * @param itemId the item Id
  108. * @param itemToIgnore used during the loop, to avoid returning the same item
  109. * @return the item from inventory by itemId
  110. */
  111. public L2ItemInstance getItemByItemId(int itemId, L2ItemInstance itemToIgnore)
  112. {
  113. for (L2ItemInstance item : _items)
  114. {
  115. if ((item != null) && (item.getId() == itemId) && !item.equals(itemToIgnore))
  116. {
  117. return item;
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * @param objectId the item object Id
  124. * @return item from inventory by objectId
  125. */
  126. public L2ItemInstance getItemByObjectId(int objectId)
  127. {
  128. for (L2ItemInstance item : _items)
  129. {
  130. if ((item != null) && (item.getObjectId() == objectId))
  131. {
  132. return item;
  133. }
  134. }
  135. return null;
  136. }
  137. /**
  138. * Gets the inventory item count by item Id and enchant level including equipped items.
  139. * @param itemId the item Id
  140. * @param enchantLevel the item enchant level, use -1 to match any enchant level
  141. * @return the inventory item count
  142. */
  143. public long getInventoryItemCount(int itemId, int enchantLevel)
  144. {
  145. return getInventoryItemCount(itemId, enchantLevel, true);
  146. }
  147. /**
  148. * Gets the inventory item count by item Id and enchant level, may include equipped items.
  149. * @param itemId the item Id
  150. * @param enchantLevel the item enchant level, use -1 to match any enchant level
  151. * @param includeEquipped if {@code true} includes equipped items in the result
  152. * @return the inventory item count
  153. */
  154. public long getInventoryItemCount(int itemId, int enchantLevel, boolean includeEquipped)
  155. {
  156. long count = 0;
  157. for (L2ItemInstance item : _items)
  158. {
  159. if ((item.getId() == itemId) && ((item.getEnchantLevel() == enchantLevel) || (enchantLevel < 0)) && (includeEquipped || !item.isEquipped()))
  160. {
  161. if (item.isStackable())
  162. {
  163. // FIXME: Zoey76: if there are more than one stacks of the same item Id
  164. // it will return the count of the last one, if is not possible to
  165. // have more than one stacks of the same item Id,
  166. // it will continue iterating over all items
  167. // possible fixes:
  168. // count += item.getCount();
  169. // or
  170. // count = item.getCount();
  171. // break;
  172. count = item.getCount();
  173. }
  174. else
  175. {
  176. count++;
  177. }
  178. }
  179. }
  180. return count;
  181. }
  182. /**
  183. * Adds item to inventory
  184. * @param process : String Identifier of process triggering this action
  185. * @param item : L2ItemInstance to be added
  186. * @param actor : L2PcInstance Player requesting the item add
  187. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  188. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  189. */
  190. public L2ItemInstance addItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  191. {
  192. L2ItemInstance olditem = getItemByItemId(item.getId());
  193. // If stackable item is found in inventory just add to current quantity
  194. if ((olditem != null) && olditem.isStackable())
  195. {
  196. long count = item.getCount();
  197. olditem.changeCount(process, count, actor, reference);
  198. olditem.setLastChange(L2ItemInstance.MODIFIED);
  199. // And destroys the item
  200. ItemTable.getInstance().destroyItem(process, item, actor, reference);
  201. item.updateDatabase();
  202. item = olditem;
  203. // Updates database
  204. float adenaRate = Config.RATE_DROP_AMOUNT_MULTIPLIER.containsKey(Inventory.ADENA_ID) ? Config.RATE_DROP_AMOUNT_MULTIPLIER.get(Inventory.ADENA_ID) : 1;
  205. if ((item.getId() == Inventory.ADENA_ID) && (count < (10000 * adenaRate)))
  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.setItemLocation(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_AMOUNT_MULTIPLIER.containsKey(Inventory.ADENA_ID) ? Config.RATE_DROP_AMOUNT_MULTIPLIER.get(Inventory.ADENA_ID) : 1;
  252. if ((itemId == Inventory.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.setItemLocation(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.getId()) : 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.getId(), 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. item.deleteMe();
  425. }
  426. return item;
  427. }
  428. /**
  429. * Destroy item from inventory by using its <B>objectID</B> and updates database
  430. * @param process : String Identifier of process triggering this action
  431. * @param objectId : int Item Instance identifier of the item to be destroyed
  432. * @param count : int Quantity of items to be destroyed
  433. * @param actor : L2PcInstance Player requesting the item destroy
  434. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  435. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  436. */
  437. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, Object reference)
  438. {
  439. L2ItemInstance item = getItemByObjectId(objectId);
  440. if (item == null)
  441. {
  442. return null;
  443. }
  444. return this.destroyItem(process, item, count, actor, reference);
  445. }
  446. /**
  447. * Destroy item from inventory by using its <B>itemId</B> and updates database
  448. * @param process : String Identifier of process triggering this action
  449. * @param itemId : int Item identifier of the item to be destroyed
  450. * @param count : int Quantity of items to be destroyed
  451. * @param actor : L2PcInstance Player requesting the item destroy
  452. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  453. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  454. */
  455. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, Object reference)
  456. {
  457. L2ItemInstance item = getItemByItemId(itemId);
  458. if (item == null)
  459. {
  460. return null;
  461. }
  462. return destroyItem(process, item, count, actor, reference);
  463. }
  464. /**
  465. * Destroy all items from inventory and updates database
  466. * @param process : String Identifier of process triggering this action
  467. * @param actor : L2PcInstance Player requesting the item destroy
  468. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  469. */
  470. public void destroyAllItems(String process, L2PcInstance actor, Object reference)
  471. {
  472. for (L2ItemInstance item : _items)
  473. {
  474. if (item != null)
  475. {
  476. destroyItem(process, item, actor, reference);
  477. }
  478. }
  479. }
  480. /**
  481. * @return warehouse Adena.
  482. */
  483. public long getAdena()
  484. {
  485. long count = 0;
  486. for (L2ItemInstance item : _items)
  487. {
  488. if ((item != null) && (item.getId() == Inventory.ADENA_ID))
  489. {
  490. count = item.getCount();
  491. return count;
  492. }
  493. }
  494. return count;
  495. }
  496. /**
  497. * Adds item to inventory for further adjustments.
  498. * @param item : L2ItemInstance to be added from inventory
  499. */
  500. protected void addItem(L2ItemInstance item)
  501. {
  502. _items.add(item);
  503. }
  504. /**
  505. * Removes item from inventory for further adjustments.
  506. * @param item : L2ItemInstance to be removed from inventory
  507. * @return
  508. */
  509. protected boolean removeItem(L2ItemInstance item)
  510. {
  511. return _items.remove(item);
  512. }
  513. /**
  514. * Refresh the weight of equipment loaded
  515. */
  516. protected void refreshWeight()
  517. {
  518. }
  519. /**
  520. * Delete item object from world
  521. */
  522. public void deleteMe()
  523. {
  524. if (getOwner() != null)
  525. {
  526. for (L2ItemInstance item : _items)
  527. {
  528. if (item != null)
  529. {
  530. item.updateDatabase(true);
  531. item.deleteMe();
  532. L2World.getInstance().removeObject(item);
  533. }
  534. }
  535. }
  536. _items.clear();
  537. }
  538. /**
  539. * Update database with items in inventory
  540. */
  541. public void updateDatabase()
  542. {
  543. if (getOwner() != null)
  544. {
  545. for (L2ItemInstance item : _items)
  546. {
  547. if (item != null)
  548. {
  549. item.updateDatabase(true);
  550. }
  551. }
  552. }
  553. }
  554. /**
  555. * Get back items in container from database
  556. */
  557. public void restore()
  558. {
  559. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  560. 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=?)"))
  561. {
  562. statement.setInt(1, getOwnerId());
  563. statement.setString(2, getBaseLocation().name());
  564. try (ResultSet inv = statement.executeQuery())
  565. {
  566. L2ItemInstance item;
  567. while (inv.next())
  568. {
  569. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  570. if (item == null)
  571. {
  572. continue;
  573. }
  574. L2World.getInstance().storeObject(item);
  575. L2PcInstance owner = getOwner() == null ? null : getOwner().getActingPlayer();
  576. // If stackable item is found in inventory just add to current quantity
  577. if (item.isStackable() && (getItemByItemId(item.getId()) != null))
  578. {
  579. addItem("Restore", item, owner, null);
  580. }
  581. else
  582. {
  583. addItem(item);
  584. }
  585. }
  586. }
  587. refreshWeight();
  588. }
  589. catch (Exception e)
  590. {
  591. _log.log(Level.WARNING, "could not restore container:", e);
  592. }
  593. }
  594. public boolean validateCapacity(long slots)
  595. {
  596. return true;
  597. }
  598. public boolean validateWeight(long weight)
  599. {
  600. return true;
  601. }
  602. /**
  603. * If the item is stackable validates 1 slot, if the item isn't stackable validates the item count.
  604. * @param itemId the item Id to verify
  605. * @param count amount of item's weight to validate
  606. * @return {@code true} if the item doesn't exists or it validates its slot count
  607. */
  608. public boolean validateCapacityByItemId(int itemId, long count)
  609. {
  610. final L2Item template = ItemTable.getInstance().getTemplate(itemId);
  611. return (template == null) || (template.isStackable() ? validateCapacity(1) : validateCapacity(count));
  612. }
  613. /**
  614. * @param itemId the item Id to verify
  615. * @param count amount of item's weight to validate
  616. * @return {@code true} if the item doesn't exists or it validates its weight
  617. */
  618. public boolean validateWeightByItemId(int itemId, long count)
  619. {
  620. final L2Item template = ItemTable.getInstance().getTemplate(itemId);
  621. return (template == null) || validateWeight(template.getWeight() * count);
  622. }
  623. }