PcInventory.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 com.l2jserver.Config;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.model.L2ItemInstance;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.TradeList;
  26. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  27. import com.l2jserver.gameserver.model.TradeList.TradeItem;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  30. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  31. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  32. import com.l2jserver.gameserver.templates.item.L2EtcItemType;
  33. import javolution.util.FastList;
  34. public class PcInventory extends Inventory
  35. {
  36. public static final int ADENA_ID = 57;
  37. public static final int ANCIENT_ADENA_ID = 5575;
  38. public static final long MAX_ADENA = 99900000000L;
  39. private final L2PcInstance _owner;
  40. private L2ItemInstance _adena;
  41. private L2ItemInstance _ancientAdena;
  42. public PcInventory(L2PcInstance owner)
  43. {
  44. _owner = owner;
  45. }
  46. @Override
  47. public L2PcInstance getOwner() { return _owner; }
  48. @Override
  49. protected ItemLocation getBaseLocation() { return ItemLocation.INVENTORY; }
  50. @Override
  51. protected ItemLocation getEquipLocation() { return ItemLocation.PAPERDOLL; }
  52. public L2ItemInstance getAdenaInstance() {return _adena;}
  53. @Override
  54. public long getAdena() {return _adena != null ? _adena.getCount() : 0;}
  55. public L2ItemInstance getAncientAdenaInstance()
  56. {
  57. return _ancientAdena;
  58. }
  59. public long getAncientAdena()
  60. {
  61. return (_ancientAdena != null) ? _ancientAdena.getCount() : 0;
  62. }
  63. /**
  64. * Returns the list of items in inventory available for transaction
  65. * @return L2ItemInstance : items in inventory
  66. */
  67. public L2ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena)
  68. {
  69. return getUniqueItems(allowAdena, allowAncientAdena, true);
  70. }
  71. public L2ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  72. {
  73. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  74. for (L2ItemInstance item : _items)
  75. {
  76. if ((!allowAdena && item.getItemId() == 57))
  77. continue;
  78. if ((!allowAncientAdena && item.getItemId() == 5575))
  79. continue;
  80. boolean isDuplicate = false;
  81. for (L2ItemInstance litem : list)
  82. {
  83. if (item == null)
  84. continue;
  85. if (litem.getItemId() == item.getItemId())
  86. {
  87. isDuplicate = true;
  88. break;
  89. }
  90. }
  91. if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false)))) list.add(item);
  92. }
  93. return list.toArray(new L2ItemInstance[list.size()]);
  94. }
  95. /**
  96. * Returns the list of items in inventory available for transaction
  97. * Allows an item to appear twice if and only if there is a difference in enchantment level.
  98. * @return L2ItemInstance : items in inventory
  99. */
  100. public L2ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena)
  101. {
  102. return getUniqueItemsByEnchantLevel(allowAdena, allowAncientAdena, true);
  103. }
  104. public L2ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  105. {
  106. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  107. for (L2ItemInstance item : _items)
  108. {
  109. if (item == null)
  110. continue;
  111. if ((!allowAdena && item.getItemId() == 57))
  112. continue;
  113. if ((!allowAncientAdena && item.getItemId() == 5575))
  114. continue;
  115. boolean isDuplicate = false;
  116. for (L2ItemInstance litem : list)
  117. {
  118. if ((litem.getItemId() == item.getItemId()) && (litem.getEnchantLevel() == item.getEnchantLevel()))
  119. {
  120. isDuplicate = true;
  121. break;
  122. }
  123. }
  124. if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false))))
  125. list.add(item);
  126. }
  127. return list.toArray(new L2ItemInstance[list.size()]);
  128. }
  129. /**
  130. * @see com.l2jserver.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, boolean)
  131. */
  132. public L2ItemInstance[] getAllItemsByItemId(int itemId)
  133. {
  134. return getAllItemsByItemId(itemId, true);
  135. }
  136. /**
  137. * Returns the list of all items in inventory that have a given item id.
  138. * @param itemId : ID of item
  139. * @param includeEquipped : include equipped items
  140. * @return L2ItemInstance[] : matching items from inventory
  141. */
  142. public L2ItemInstance[] getAllItemsByItemId(int itemId, boolean includeEquipped)
  143. {
  144. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  145. for (L2ItemInstance item : _items)
  146. {
  147. if (item == null)
  148. continue;
  149. if (item.getItemId() == itemId && (includeEquipped || !item.isEquipped()))
  150. list.add(item);
  151. }
  152. return list.toArray(new L2ItemInstance[list.size()]);
  153. }
  154. /**
  155. * @see com.l2jserver.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, int, boolean)
  156. */
  157. public L2ItemInstance[] getAllItemsByItemId(int itemId, int enchantment)
  158. {
  159. return getAllItemsByItemId(itemId, enchantment, true);
  160. }
  161. /**
  162. * Returns the list of all items in inventory that have a given item id AND a given enchantment level.
  163. * @param itemId : ID of item
  164. * @param enchantment : enchant level of item
  165. * @param includeEquipped : include equipped items
  166. * @return L2ItemInstance[] : matching items from inventory
  167. */
  168. public L2ItemInstance[] getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped)
  169. {
  170. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  171. for (L2ItemInstance item : _items)
  172. {
  173. if (item == null)
  174. continue;
  175. if ((item.getItemId() == itemId) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped()))
  176. list.add(item);
  177. }
  178. return list.toArray(new L2ItemInstance[list.size()]);
  179. }
  180. /**
  181. * Returns the list of items in inventory available for transaction
  182. * @return L2ItemInstance : items in inventory
  183. */
  184. public L2ItemInstance[] getAvailableItems(boolean allowAdena, boolean allowNonTradeable)
  185. {
  186. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  187. for (L2ItemInstance item : _items)
  188. if (item != null && item.isAvailable(getOwner(), allowAdena, allowNonTradeable)) list.add(item);
  189. return list.toArray(new L2ItemInstance[list.size()]);
  190. }
  191. /**
  192. * Get all augmented items
  193. * @return
  194. */
  195. public L2ItemInstance[] getAugmentedItems()
  196. {
  197. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  198. for (L2ItemInstance item : _items)
  199. if (item != null && item.isAugmented()) list.add(item);
  200. return list.toArray(new L2ItemInstance[list.size()]);
  201. }
  202. /**
  203. * Get all element items
  204. * @return
  205. */
  206. public L2ItemInstance[] getElementItems()
  207. {
  208. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  209. for (L2ItemInstance item : _items)
  210. {
  211. if (item != null && item.getElementals() != null)
  212. list.add(item);
  213. }
  214. return list.toArray(new L2ItemInstance[list.size()]);
  215. }
  216. /**
  217. * Returns the list of items in inventory available for transaction adjusted by tradeList
  218. * @return L2ItemInstance : items in inventory
  219. */
  220. public TradeList.TradeItem[] getAvailableItems(TradeList tradeList)
  221. {
  222. List<TradeList.TradeItem> list = new FastList<TradeList.TradeItem>();
  223. for (L2ItemInstance item : _items)
  224. if (item != null && item.isAvailable(getOwner(), false, false))
  225. {
  226. TradeList.TradeItem adjItem = tradeList.adjustAvailableItem(item);
  227. if (adjItem != null) list.add(adjItem);
  228. }
  229. return list.toArray(new TradeList.TradeItem[list.size()]);
  230. }
  231. /**
  232. * Adjust TradeItem according his status in inventory
  233. * @param item : L2ItemInstance to be adjusten
  234. * @return TradeItem representing adjusted item
  235. */
  236. public void adjustAvailableItem(TradeItem item)
  237. {
  238. boolean notAllEquipped = false;
  239. for(L2ItemInstance adjItem: getItemsByItemId(item.getItem().getItemId()))
  240. {
  241. if(adjItem.isEquipable())
  242. {
  243. if(!adjItem.isEquipped())
  244. notAllEquipped |= true;
  245. }else{
  246. notAllEquipped |= true;
  247. break;
  248. }
  249. }
  250. if(notAllEquipped)
  251. {
  252. L2ItemInstance adjItem = getItemByItemId(item.getItem().getItemId());
  253. item.setObjectId(adjItem.getObjectId());
  254. item.setEnchant(adjItem.getEnchantLevel());
  255. if (adjItem.getCount() < item.getCount())
  256. item.setCount(adjItem.getCount());
  257. return;
  258. }
  259. item.setCount(0);
  260. }
  261. /**
  262. * Adds adena to PCInventory
  263. * @param process : String Identifier of process triggering this action
  264. * @param count : int Quantity of adena to be added
  265. * @param actor : L2PcInstance Player requesting the item add
  266. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  267. */
  268. public void addAdena(String process, long count, L2PcInstance actor, L2Object reference)
  269. {
  270. if (count > 0)
  271. addItem(process, ADENA_ID, count, actor, reference);
  272. }
  273. /**
  274. * Removes adena to PCInventory
  275. * @param process : String Identifier of process triggering this action
  276. * @param count : int Quantity of adena to be removed
  277. * @param actor : L2PcInstance Player requesting the item add
  278. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  279. */
  280. public void reduceAdena(String process, long count, L2PcInstance actor, L2Object reference)
  281. {
  282. if (count > 0)
  283. destroyItemByItemId(process, ADENA_ID, count, actor, reference);
  284. }
  285. /**
  286. * Adds specified amount of ancient adena to player inventory.
  287. * @param process : String Identifier of process triggering this action
  288. * @param count : int Quantity of adena to be added
  289. * @param actor : L2PcInstance Player requesting the item add
  290. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  291. */
  292. public void addAncientAdena(String process, long count, L2PcInstance actor, L2Object reference)
  293. {
  294. if (count > 0)
  295. addItem(process, ANCIENT_ADENA_ID, count, actor, reference);
  296. }
  297. /**
  298. * Removes specified amount of ancient adena from player inventory.
  299. * @param process : String Identifier of process triggering this action
  300. * @param count : int Quantity of adena to be removed
  301. * @param actor : L2PcInstance Player requesting the item add
  302. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  303. */
  304. public void reduceAncientAdena(String process, long count, L2PcInstance actor, L2Object reference)
  305. {
  306. if (count > 0)
  307. destroyItemByItemId(process, ANCIENT_ADENA_ID, count, actor, reference);
  308. }
  309. /**
  310. * Adds item in inventory and checks _adena and _ancientAdena
  311. * @param process : String Identifier of process triggering this action
  312. * @param item : L2ItemInstance to be added
  313. * @param actor : L2PcInstance Player requesting the item add
  314. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  315. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  316. */
  317. @Override
  318. public L2ItemInstance addItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  319. {
  320. item = super.addItem(process, item, actor, reference);
  321. if (item != null && item.getItemId() == ADENA_ID && !item.equals(_adena))
  322. _adena = item;
  323. if (item != null && item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  324. _ancientAdena = item;
  325. return item;
  326. }
  327. /**
  328. * Adds item in inventory and checks _adena and _ancientAdena
  329. * @param process : String Identifier of process triggering this action
  330. * @param itemId : int Item Identifier of the item to be added
  331. * @param count : int Quantity of items to be added
  332. * @param actor : L2PcInstance Player requesting the item creation
  333. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  334. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  335. */
  336. @Override
  337. public L2ItemInstance addItem(String process, int itemId, long count, L2PcInstance actor, L2Object reference)
  338. {
  339. L2ItemInstance item = super.addItem(process, itemId, count, actor, reference);
  340. if (item != null && item.getItemId() == ADENA_ID && !item.equals(_adena))
  341. _adena = item;
  342. if (item != null && item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  343. _ancientAdena = item;
  344. if (item != null && actor != null)
  345. {
  346. // Send inventory update packet
  347. if (!Config.FORCE_INVENTORY_UPDATE)
  348. {
  349. InventoryUpdate playerIU = new InventoryUpdate();
  350. playerIU.addItem(item);
  351. actor.sendPacket(playerIU);
  352. }
  353. else
  354. actor.sendPacket(new ItemList(actor, false));
  355. // Update current load as well
  356. StatusUpdate su = new StatusUpdate(actor.getObjectId());
  357. su.addAttribute(StatusUpdate.CUR_LOAD, actor.getCurrentLoad());
  358. actor.sendPacket(su);
  359. }
  360. return item;
  361. }
  362. /**
  363. * Transfers item to another inventory and checks _adena and _ancientAdena
  364. * @param process : String Identifier of process triggering this action
  365. * @param itemId : int Item Identifier of the item to be transfered
  366. * @param count : int Quantity of items to be transfered
  367. * @param actor : L2PcInstance Player requesting the item transfer
  368. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  369. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  370. */
  371. @Override
  372. public L2ItemInstance transferItem(String process, int objectId, long count, ItemContainer target, L2PcInstance actor, L2Object reference)
  373. {
  374. L2ItemInstance item = super.transferItem(process, objectId, count, target, actor, reference);
  375. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  376. _adena = null;
  377. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  378. _ancientAdena = null;
  379. return item;
  380. }
  381. /**
  382. * Destroy item from inventory and checks _adena and _ancientAdena
  383. * @param process : String Identifier of process triggering this action
  384. * @param item : L2ItemInstance to be destroyed
  385. * @param actor : L2PcInstance Player requesting the item destroy
  386. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  387. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  388. */
  389. @Override
  390. public L2ItemInstance destroyItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  391. {
  392. return this.destroyItem(process, item, item.getCount(), actor, reference);
  393. }
  394. /**
  395. * Destroy item from inventory and checks _adena and _ancientAdena
  396. * @param process : String Identifier of process triggering this action
  397. * @param item : L2ItemInstance to be destroyed
  398. * @param actor : L2PcInstance Player requesting the item destroy
  399. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  400. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  401. */
  402. @Override
  403. public L2ItemInstance destroyItem(String process, L2ItemInstance item, long count, L2PcInstance actor, L2Object reference)
  404. {
  405. item = super.destroyItem(process, item, count, actor, reference);
  406. if (_adena != null && _adena.getCount() <= 0)
  407. _adena = null;
  408. if (_ancientAdena != null && _ancientAdena.getCount() <= 0)
  409. _ancientAdena = null;
  410. return item;
  411. }
  412. /**
  413. * Destroys item from inventory and checks _adena and _ancientAdena
  414. * @param process : String Identifier of process triggering this action
  415. * @param objectId : int Item Instance identifier of the item to be destroyed
  416. * @param count : int Quantity of items to be destroyed
  417. * @param actor : L2PcInstance Player requesting the item destroy
  418. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  419. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  420. */
  421. @Override
  422. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, L2Object reference)
  423. {
  424. L2ItemInstance item = getItemByObjectId(objectId);
  425. if (item == null)
  426. {
  427. return null;
  428. }
  429. return this.destroyItem(process, item, count, actor, reference);
  430. }
  431. /**
  432. * Destroy item from inventory by using its <B>itemId</B> and checks _adena and _ancientAdena
  433. * @param process : String Identifier of process triggering this action
  434. * @param itemId : int Item identifier of the item to be destroyed
  435. * @param count : int Quantity of items to be destroyed
  436. * @param actor : L2PcInstance Player requesting the item destroy
  437. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  438. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  439. */
  440. @Override
  441. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, L2Object reference)
  442. {
  443. L2ItemInstance item = getItemByItemId(itemId);
  444. if (item == null)
  445. {
  446. return null;
  447. }
  448. return this.destroyItem(process, item, count, actor, reference);
  449. }
  450. /**
  451. * Drop item from inventory and checks _adena and _ancientAdena
  452. * @param process : String Identifier of process triggering this action
  453. * @param item : L2ItemInstance to be dropped
  454. * @param actor : L2PcInstance Player requesting the item drop
  455. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  456. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  457. */
  458. @Override
  459. public L2ItemInstance dropItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  460. {
  461. item = super.dropItem(process, item, actor, reference);
  462. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  463. _adena = null;
  464. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  465. _ancientAdena = null;
  466. return item;
  467. }
  468. /**
  469. * Drop item from inventory by using its <B>objectID</B> and checks _adena and _ancientAdena
  470. * @param process : String Identifier of process triggering this action
  471. * @param objectId : int Item Instance identifier of the item to be dropped
  472. * @param count : int Quantity of items to be dropped
  473. * @param actor : L2PcInstance Player requesting the item drop
  474. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  475. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  476. */
  477. @Override
  478. public L2ItemInstance dropItem(String process, int objectId, long count, L2PcInstance actor, L2Object reference)
  479. {
  480. L2ItemInstance item = super.dropItem(process, objectId, count, actor, reference);
  481. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  482. _adena = null;
  483. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  484. _ancientAdena = null;
  485. return item;
  486. }
  487. /**
  488. * <b>Overloaded</b>, when removes item from inventory, remove also owner shortcuts.
  489. * @param item : L2ItemInstance to be removed from inventory
  490. */
  491. @Override
  492. protected boolean removeItem(L2ItemInstance item)
  493. {
  494. // Removes any reference to the item from Shortcut bar
  495. getOwner().removeItemFromShortCut(item.getObjectId());
  496. // Removes active Enchant Scroll
  497. if(item.equals(getOwner().getActiveEnchantItem()))
  498. getOwner().setActiveEnchantItem(null);
  499. if (item.getItemId() == ADENA_ID)
  500. _adena = null;
  501. else if (item.getItemId() == ANCIENT_ADENA_ID)
  502. _ancientAdena = null;
  503. return super.removeItem(item);
  504. }
  505. /**
  506. * Refresh the weight of equipment loaded
  507. */
  508. @Override
  509. public void refreshWeight()
  510. {
  511. super.refreshWeight();
  512. getOwner().refreshOverloaded();
  513. }
  514. /**
  515. * Get back items in inventory from database
  516. */
  517. @Override
  518. public void restore()
  519. {
  520. super.restore();
  521. _adena = getItemByItemId(ADENA_ID);
  522. _ancientAdena = getItemByItemId(ANCIENT_ADENA_ID);
  523. }
  524. public static int[][] restoreVisibleInventory(int objectId)
  525. {
  526. int[][] paperdoll = new int[31][3];
  527. Connection con = null;
  528. try
  529. {
  530. con = L2DatabaseFactory.getInstance().getConnection();
  531. PreparedStatement statement2 = con.prepareStatement(
  532. "SELECT object_id,item_id,loc_data,enchant_level FROM items WHERE owner_id=? AND loc='PAPERDOLL'");
  533. statement2.setInt(1, objectId);
  534. ResultSet invdata = statement2.executeQuery();
  535. while (invdata.next())
  536. {
  537. int slot = invdata.getInt("loc_data");
  538. paperdoll[slot][0] = invdata.getInt("object_id");
  539. paperdoll[slot][1] = invdata.getInt("item_id");
  540. paperdoll[slot][2] = invdata.getInt("enchant_level");
  541. if (slot == Inventory.PAPERDOLL_LRHAND)
  542. {
  543. paperdoll[Inventory.PAPERDOLL_RHAND][0] = invdata.getInt("object_id");
  544. paperdoll[Inventory.PAPERDOLL_RHAND][1] = invdata.getInt("item_id");
  545. paperdoll[Inventory.PAPERDOLL_RHAND][2] = invdata.getInt("enchant_level");
  546. }
  547. }
  548. invdata.close();
  549. statement2.close();
  550. }
  551. catch (Exception e) {
  552. _log.log(Level.WARNING, "could not restore inventory:", e);
  553. }
  554. finally {
  555. try { con.close(); } catch (Exception e) { _log.warning(""); }
  556. }
  557. return paperdoll;
  558. }
  559. public boolean validateCapacity(L2ItemInstance item)
  560. {
  561. int slots = 0;
  562. if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null) && item.getItemType() != L2EtcItemType.HERB)
  563. slots++;
  564. return validateCapacity(slots);
  565. }
  566. public boolean validateCapacity(List<L2ItemInstance> items)
  567. {
  568. int slots = 0;
  569. for (L2ItemInstance item : items)
  570. if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null))
  571. slots++;
  572. return validateCapacity(slots);
  573. }
  574. public boolean validateCapacityByItemId(int ItemId)
  575. {
  576. int slots = 0;
  577. L2ItemInstance invItem = getItemByItemId(ItemId);
  578. if (!(invItem != null && invItem.isStackable()))
  579. slots++;
  580. return validateCapacity(slots);
  581. }
  582. @Override
  583. public boolean validateCapacity(int slots)
  584. {
  585. return (_items.size() + slots <= _owner.getInventoryLimit());
  586. }
  587. @Override
  588. public boolean validateWeight(int weight)
  589. {
  590. return (_totalWeight + weight <= _owner.getMaxLoad());
  591. }
  592. }