ItemContainer.java 20 KB

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