PcInventory.java 24 KB

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