ItemContainer.java 19 KB

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