PcInventory.java 25 KB

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