ItemContainer.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. target.refreshWeight();
  357. }
  358. return targetitem;
  359. }
  360. /**
  361. * Destroy item from inventory and updates database
  362. * @param process : String Identifier of process triggering this action
  363. * @param item : L2ItemInstance to be destroyed
  364. * @param actor : L2PcInstance Player requesting the item destroy
  365. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  366. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  367. */
  368. public L2ItemInstance destroyItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  369. {
  370. return this.destroyItem(process, item, item.getCount(), actor, reference);
  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 : L2Object 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, long count, L2PcInstance actor, L2Object reference)
  381. {
  382. synchronized (item)
  383. {
  384. // Adjust item quantity
  385. if (item.getCount() > count)
  386. {
  387. item.changeCount(process, -count, actor, reference);
  388. item.setLastChange(L2ItemInstance.MODIFIED);
  389. // don't update often for untraced items
  390. if (process != null || GameTimeController.getGameTicks() % 10 == 0)
  391. {
  392. item.updateDatabase();
  393. }
  394. refreshWeight();
  395. return item;
  396. }
  397. else
  398. {
  399. if (item.getCount() < count)
  400. return null;
  401. boolean removed = this.removeItem(item);
  402. if (!removed)
  403. return null;
  404. ItemTable.getInstance().destroyItem(process, item, actor, reference);
  405. item.updateDatabase();
  406. refreshWeight();
  407. }
  408. }
  409. return item;
  410. }
  411. /**
  412. * Destroy item from inventory by using its <B>objectID</B> and updates database
  413. * @param process : String Identifier of process triggering this action
  414. * @param objectId : int Item Instance identifier of the item to be destroyed
  415. * @param count : int Quantity of items to be destroyed
  416. * @param actor : L2PcInstance Player requesting the item destroy
  417. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  418. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  419. */
  420. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, L2Object reference)
  421. {
  422. L2ItemInstance item = getItemByObjectId(objectId);
  423. if (item == null)
  424. {
  425. return null;
  426. }
  427. return this.destroyItem(process, item, count, actor, reference);
  428. }
  429. /**
  430. * Destroy item from inventory by using its <B>itemId</B> and updates database
  431. * @param process : String Identifier of process triggering this action
  432. * @param itemId : int Item identifier of the item to be destroyed
  433. * @param count : int Quantity of items to be destroyed
  434. * @param actor : L2PcInstance Player requesting the item destroy
  435. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  436. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  437. */
  438. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, L2Object reference)
  439. {
  440. L2ItemInstance item = getItemByItemId(itemId);
  441. if (item == null)
  442. {
  443. return null;
  444. }
  445. return this.destroyItem(process, item, count, actor, reference);
  446. }
  447. /**
  448. * Destroy all items from inventory and updates database
  449. * @param process : String Identifier of process triggering this action
  450. * @param actor : L2PcInstance Player requesting the item destroy
  451. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  452. */
  453. public synchronized void destroyAllItems(String process, L2PcInstance actor, L2Object reference)
  454. {
  455. for (L2ItemInstance item : _items)
  456. {
  457. if (item != null)
  458. destroyItem(process, item, actor, reference);
  459. }
  460. }
  461. /**
  462. * Get warehouse adena
  463. */
  464. public long getAdena()
  465. {
  466. long count = 0;
  467. for (L2ItemInstance item : _items)
  468. {
  469. if (item.getItemId() == 57)
  470. {
  471. count = item.getCount();
  472. return count;
  473. }
  474. }
  475. return count;
  476. }
  477. /**
  478. * Adds item to inventory for further adjustments.
  479. * @param item : L2ItemInstance to be added from inventory
  480. */
  481. protected void addItem(L2ItemInstance item)
  482. {
  483. synchronized (_items)
  484. {
  485. _items.add(item);
  486. }
  487. }
  488. /**
  489. * Removes item from inventory for further adjustments.
  490. * @param item : L2ItemInstance to be removed from inventory
  491. */
  492. protected boolean removeItem(L2ItemInstance item)
  493. {
  494. synchronized (_items)
  495. {
  496. return _items.remove(item);
  497. }
  498. }
  499. /**
  500. * Refresh the weight of equipment loaded
  501. */
  502. protected void refreshWeight()
  503. {
  504. }
  505. /**
  506. * Delete item object from world
  507. */
  508. public void deleteMe()
  509. {
  510. try
  511. {
  512. updateDatabase();
  513. }
  514. catch (Exception e)
  515. {
  516. _log.log(Level.SEVERE, "deletedMe()", e);
  517. }
  518. List<L2Object> items = new FastList<L2Object>(_items);
  519. _items.clear();
  520. L2World.getInstance().removeObjects(items);
  521. }
  522. /**
  523. * Update database with items in inventory
  524. */
  525. public void updateDatabase()
  526. {
  527. if (getOwner() != null)
  528. {
  529. for (L2ItemInstance item : _items)
  530. {
  531. if (item != null)
  532. {
  533. item.updateDatabase(true);
  534. }
  535. }
  536. }
  537. }
  538. /**
  539. * Get back items in container from database
  540. */
  541. public void restore()
  542. {
  543. Connection con = null;
  544. try
  545. {
  546. con = L2DatabaseFactory.getInstance().getConnection();
  547. 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=?)");
  548. statement.setInt(1, getOwnerId());
  549. statement.setString(2, getBaseLocation().name());
  550. ResultSet inv = statement.executeQuery();
  551. L2ItemInstance item;
  552. while (inv.next())
  553. {
  554. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  555. if (item == null)
  556. continue;
  557. L2World.getInstance().storeObject(item);
  558. L2PcInstance owner = getOwner() == null ? null : getOwner().getActingPlayer();
  559. // If stackable item is found in inventory just add to current quantity
  560. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  561. addItem("Restore", item, owner, null);
  562. else
  563. addItem(item);
  564. }
  565. inv.close();
  566. statement.close();
  567. refreshWeight();
  568. }
  569. catch (Exception e)
  570. {
  571. _log.log(Level.WARNING, "could not restore container:", e);
  572. }
  573. finally
  574. {
  575. try
  576. {
  577. con.close();
  578. }
  579. catch (Exception e)
  580. {
  581. }
  582. }
  583. }
  584. public boolean validateCapacity(int slots)
  585. {
  586. return true;
  587. }
  588. public boolean validateWeight(int weight)
  589. {
  590. return true;
  591. }
  592. }