PcInventory.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.datatables.ItemTable;
  25. import com.l2jserver.gameserver.model.L2ItemInstance;
  26. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  27. import com.l2jserver.gameserver.model.TradeList;
  28. import com.l2jserver.gameserver.model.TradeList.TradeItem;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  31. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  32. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  33. import com.l2jserver.gameserver.templates.item.L2EtcItemType;
  34. import com.l2jserver.gameserver.templates.item.L2Item;
  35. import com.l2jserver.gameserver.util.Util;
  36. public class PcInventory extends Inventory
  37. {
  38. public static final int ADENA_ID = 57;
  39. public static final int ANCIENT_ADENA_ID = 5575;
  40. public static final long MAX_ADENA = 99900000000L;
  41. private final L2PcInstance _owner;
  42. private L2ItemInstance _adena;
  43. private L2ItemInstance _ancientAdena;
  44. private int[] _blockItems = null;
  45. private int _questSlots;
  46. /**
  47. * Block modes:
  48. * <UL>
  49. * <LI>-1 - no block
  50. * <LI>0 - block items from _invItems, allow usage of other items
  51. * <LI>1 - allow usage of items from _invItems, block other items
  52. * </UL>
  53. */
  54. private int _blockMode = -1;
  55. public PcInventory(L2PcInstance owner)
  56. {
  57. _owner = owner;
  58. }
  59. @Override
  60. public L2PcInstance getOwner() { return _owner; }
  61. @Override
  62. protected ItemLocation getBaseLocation() { return ItemLocation.INVENTORY; }
  63. @Override
  64. protected ItemLocation getEquipLocation() { return ItemLocation.PAPERDOLL; }
  65. public L2ItemInstance getAdenaInstance() {return _adena;}
  66. @Override
  67. public long getAdena() {return _adena != null ? _adena.getCount() : 0;}
  68. public L2ItemInstance getAncientAdenaInstance()
  69. {
  70. return _ancientAdena;
  71. }
  72. public long getAncientAdena()
  73. {
  74. return (_ancientAdena != null) ? _ancientAdena.getCount() : 0;
  75. }
  76. /**
  77. * Returns the list of items in inventory available for transaction
  78. * @return L2ItemInstance : items in inventory
  79. */
  80. public L2ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena)
  81. {
  82. return getUniqueItems(allowAdena, allowAncientAdena, true);
  83. }
  84. public L2ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  85. {
  86. FastList<L2ItemInstance> list = FastList.newInstance();
  87. for (L2ItemInstance item : _items)
  88. {
  89. if ((!allowAdena && item.getItemId() == 57))
  90. continue;
  91. if ((!allowAncientAdena && item.getItemId() == 5575))
  92. continue;
  93. boolean isDuplicate = false;
  94. for (L2ItemInstance litem : list)
  95. {
  96. if (item == null)
  97. continue;
  98. if (litem.getItemId() == item.getItemId())
  99. {
  100. isDuplicate = true;
  101. break;
  102. }
  103. }
  104. if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false)))) list.add(item);
  105. }
  106. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  107. FastList.recycle(list);
  108. return result;
  109. }
  110. /**
  111. * Returns the list of items in inventory available for transaction
  112. * Allows an item to appear twice if and only if there is a difference in enchantment level.
  113. * @return L2ItemInstance : items in inventory
  114. */
  115. public L2ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena)
  116. {
  117. return getUniqueItemsByEnchantLevel(allowAdena, allowAncientAdena, true);
  118. }
  119. public L2ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  120. {
  121. FastList<L2ItemInstance> list = FastList.newInstance();
  122. for (L2ItemInstance item : _items)
  123. {
  124. if (item == null)
  125. continue;
  126. if ((!allowAdena && item.getItemId() == 57))
  127. continue;
  128. if ((!allowAncientAdena && item.getItemId() == 5575))
  129. continue;
  130. boolean isDuplicate = false;
  131. for (L2ItemInstance litem : list)
  132. {
  133. if ((litem.getItemId() == item.getItemId()) && (litem.getEnchantLevel() == item.getEnchantLevel()))
  134. {
  135. isDuplicate = true;
  136. break;
  137. }
  138. }
  139. if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false))))
  140. list.add(item);
  141. }
  142. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  143. FastList.recycle(list);
  144. return result;
  145. }
  146. /**
  147. * @see com.l2jserver.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, boolean)
  148. */
  149. public L2ItemInstance[] getAllItemsByItemId(int itemId)
  150. {
  151. return getAllItemsByItemId(itemId, true);
  152. }
  153. /**
  154. * Returns the list of all items in inventory that have a given item id.
  155. * @param itemId : ID of item
  156. * @param includeEquipped : include equipped items
  157. * @return L2ItemInstance[] : matching items from inventory
  158. */
  159. public L2ItemInstance[] getAllItemsByItemId(int itemId, boolean includeEquipped)
  160. {
  161. FastList<L2ItemInstance> list = FastList.newInstance();
  162. for (L2ItemInstance item : _items)
  163. {
  164. if (item == null)
  165. continue;
  166. if (item.getItemId() == itemId && (includeEquipped || !item.isEquipped()))
  167. list.add(item);
  168. }
  169. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  170. FastList.recycle(list);
  171. return result;
  172. }
  173. /**
  174. * @see com.l2jserver.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, int, boolean)
  175. */
  176. public L2ItemInstance[] getAllItemsByItemId(int itemId, int enchantment)
  177. {
  178. return getAllItemsByItemId(itemId, enchantment, true);
  179. }
  180. /**
  181. * Returns the list of all items in inventory that have a given item id AND a given enchantment level.
  182. * @param itemId : ID of item
  183. * @param enchantment : enchant level of item
  184. * @param includeEquipped : include equipped items
  185. * @return L2ItemInstance[] : matching items from inventory
  186. */
  187. public L2ItemInstance[] getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped)
  188. {
  189. FastList<L2ItemInstance> list = FastList.newInstance();
  190. for (L2ItemInstance item : _items)
  191. {
  192. if (item == null)
  193. continue;
  194. if ((item.getItemId() == itemId) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped()))
  195. list.add(item);
  196. }
  197. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  198. FastList.recycle(list);
  199. return result;
  200. }
  201. /**
  202. * Returns the list of items in inventory available for transaction
  203. * @return L2ItemInstance : items in inventory
  204. */
  205. public L2ItemInstance[] getAvailableItems(boolean allowAdena, boolean allowNonTradeable)
  206. {
  207. FastList<L2ItemInstance> list = FastList.newInstance();
  208. for (L2ItemInstance item : _items)
  209. {
  210. if (item != null && item.isAvailable(getOwner(), allowAdena, allowNonTradeable) && canManipulateWithItemId(item.getItemId()))
  211. list.add(item);
  212. }
  213. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  214. FastList.recycle(list);
  215. return result;
  216. }
  217. /**
  218. * Get all augmented items
  219. * @return
  220. */
  221. public L2ItemInstance[] getAugmentedItems()
  222. {
  223. FastList<L2ItemInstance> list = FastList.newInstance();
  224. for (L2ItemInstance item : _items)
  225. {
  226. if (item != null && item.isAugmented())
  227. list.add(item);
  228. }
  229. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  230. FastList.recycle(list);
  231. return result;
  232. }
  233. /**
  234. * Get all element items
  235. * @return
  236. */
  237. public L2ItemInstance[] getElementItems()
  238. {
  239. FastList<L2ItemInstance> list = FastList.newInstance();
  240. for (L2ItemInstance item : _items)
  241. {
  242. if (item != null && item.getElementals() != null)
  243. list.add(item);
  244. }
  245. L2ItemInstance[] result = list.toArray(new L2ItemInstance[list.size()]);
  246. FastList.recycle(list);
  247. return result;
  248. }
  249. /**
  250. * Returns the list of items in inventory available for transaction adjusted by tradeList
  251. * @return L2ItemInstance : items in inventory
  252. */
  253. public TradeList.TradeItem[] getAvailableItems(TradeList tradeList)
  254. {
  255. FastList<TradeList.TradeItem> list = FastList.newInstance();
  256. for (L2ItemInstance item : _items)
  257. {
  258. if (item != null && item.isAvailable(getOwner(), false, false))
  259. {
  260. TradeList.TradeItem adjItem = tradeList.adjustAvailableItem(item);
  261. if (adjItem != null)
  262. list.add(adjItem);
  263. }
  264. }
  265. TradeList.TradeItem[] result = list.toArray(new TradeList.TradeItem[list.size()]);
  266. FastList.recycle(list);
  267. return result;
  268. }
  269. /**
  270. * Adjust TradeItem according his status in inventory
  271. * @param item : L2ItemInstance to be adjusten
  272. * @return TradeItem representing adjusted item
  273. */
  274. public void adjustAvailableItem(TradeItem item)
  275. {
  276. boolean notAllEquipped = false;
  277. for(L2ItemInstance adjItem: getItemsByItemId(item.getItem().getItemId()))
  278. {
  279. if(adjItem.isEquipable())
  280. {
  281. if(!adjItem.isEquipped())
  282. notAllEquipped |= true;
  283. }else{
  284. notAllEquipped |= true;
  285. break;
  286. }
  287. }
  288. if(notAllEquipped)
  289. {
  290. L2ItemInstance adjItem = getItemByItemId(item.getItem().getItemId());
  291. item.setObjectId(adjItem.getObjectId());
  292. item.setEnchant(adjItem.getEnchantLevel());
  293. if (adjItem.getCount() < item.getCount())
  294. item.setCount(adjItem.getCount());
  295. return;
  296. }
  297. item.setCount(0);
  298. }
  299. /**
  300. * Adds adena to PCInventory
  301. * @param process : String Identifier of process triggering this action
  302. * @param count : int Quantity of adena to be added
  303. * @param actor : L2PcInstance Player requesting the item add
  304. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  305. */
  306. public void addAdena(String process, long count, L2PcInstance actor, Object reference)
  307. {
  308. if (count > 0)
  309. addItem(process, ADENA_ID, count, actor, reference);
  310. }
  311. /**
  312. * Removes adena to PCInventory
  313. * @param process : String Identifier of process triggering this action
  314. * @param count : int Quantity of adena to be removed
  315. * @param actor : L2PcInstance Player requesting the item add
  316. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  317. * @return boolean : true if adena was reduced
  318. */
  319. public boolean reduceAdena(String process, long count, L2PcInstance actor, Object reference)
  320. {
  321. if (count > 0)
  322. return destroyItemByItemId(process, ADENA_ID, count, actor, reference) != null;
  323. return false;
  324. }
  325. /**
  326. * Adds specified amount of ancient adena to player inventory.
  327. * @param process : String Identifier of process triggering this action
  328. * @param count : int Quantity of adena to be added
  329. * @param actor : L2PcInstance Player requesting the item add
  330. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  331. */
  332. public void addAncientAdena(String process, long count, L2PcInstance actor, Object reference)
  333. {
  334. if (count > 0)
  335. addItem(process, ANCIENT_ADENA_ID, count, actor, reference);
  336. }
  337. /**
  338. * Removes specified amount of ancient adena from player inventory.
  339. * @param process : String Identifier of process triggering this action
  340. * @param count : int Quantity of adena to be removed
  341. * @param actor : L2PcInstance Player requesting the item add
  342. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  343. * @return boolean : true if adena was reduced
  344. */
  345. public boolean reduceAncientAdena(String process, long count, L2PcInstance actor, Object reference)
  346. {
  347. if (count > 0)
  348. return destroyItemByItemId(process, ANCIENT_ADENA_ID, count, actor, reference) != null;
  349. return false;
  350. }
  351. /**
  352. * Adds item in inventory and checks _adena and _ancientAdena
  353. * @param process : String Identifier of process triggering this action
  354. * @param item : L2ItemInstance to be added
  355. * @param actor : L2PcInstance Player requesting the item add
  356. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  357. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  358. */
  359. @Override
  360. public L2ItemInstance addItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  361. {
  362. item = super.addItem(process, item, actor, reference);
  363. if (item != null && item.getItemId() == ADENA_ID && !item.equals(_adena))
  364. _adena = item;
  365. if (item != null && item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  366. _ancientAdena = item;
  367. return item;
  368. }
  369. /**
  370. * Adds item in inventory and checks _adena and _ancientAdena
  371. * @param process : String Identifier of process triggering this action
  372. * @param itemId : int Item Identifier of the item to be added
  373. * @param count : int Quantity of items to be added
  374. * @param actor : L2PcInstance Player requesting the item creation
  375. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  376. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  377. */
  378. @Override
  379. public L2ItemInstance addItem(String process, int itemId, long count, L2PcInstance actor, Object reference)
  380. {
  381. L2ItemInstance item = super.addItem(process, itemId, count, actor, reference);
  382. if (item != null && item.getItemId() == ADENA_ID && !item.equals(_adena))
  383. _adena = item;
  384. if (item != null && item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  385. _ancientAdena = item;
  386. if (item != null && actor != null)
  387. {
  388. // Send inventory update packet
  389. if (!Config.FORCE_INVENTORY_UPDATE)
  390. {
  391. InventoryUpdate playerIU = new InventoryUpdate();
  392. playerIU.addItem(item);
  393. actor.sendPacket(playerIU);
  394. }
  395. else
  396. actor.sendPacket(new ItemList(actor, false));
  397. // Update current load as well
  398. StatusUpdate su = new StatusUpdate(actor);
  399. su.addAttribute(StatusUpdate.CUR_LOAD, actor.getCurrentLoad());
  400. actor.sendPacket(su);
  401. }
  402. return item;
  403. }
  404. /**
  405. * Transfers item to another inventory and checks _adena and _ancientAdena
  406. * @param process : String Identifier of process triggering this action
  407. * @param itemId : int Item Identifier of the item to be transfered
  408. * @param count : int Quantity of items to be transfered
  409. * @param actor : L2PcInstance Player requesting the item transfer
  410. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  411. * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  412. */
  413. @Override
  414. public L2ItemInstance transferItem(String process, int objectId, long count, ItemContainer target, L2PcInstance actor, Object reference)
  415. {
  416. L2ItemInstance item = super.transferItem(process, objectId, count, target, actor, reference);
  417. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  418. _adena = null;
  419. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  420. _ancientAdena = null;
  421. return item;
  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 : Object 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, L2PcInstance actor, Object reference)
  433. {
  434. return this.destroyItem(process, item, item.getCount(), actor, reference);
  435. }
  436. /**
  437. * Destroy item from inventory and checks _adena and _ancientAdena
  438. * @param process : String Identifier of process triggering this action
  439. * @param item : L2ItemInstance to be destroyed
  440. * @param actor : L2PcInstance Player requesting the item destroy
  441. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  442. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  443. */
  444. @Override
  445. public L2ItemInstance destroyItem(String process, L2ItemInstance item, long count, L2PcInstance actor, Object reference)
  446. {
  447. item = super.destroyItem(process, item, count, actor, reference);
  448. if (_adena != null && _adena.getCount() <= 0)
  449. _adena = null;
  450. if (_ancientAdena != null && _ancientAdena.getCount() <= 0)
  451. _ancientAdena = null;
  452. return item;
  453. }
  454. /**
  455. * Destroys item from inventory and checks _adena and _ancientAdena
  456. * @param process : String Identifier of process triggering this action
  457. * @param objectId : int Item Instance identifier of the item to be destroyed
  458. * @param count : int Quantity of items to be destroyed
  459. * @param actor : L2PcInstance Player requesting the item destroy
  460. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  461. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  462. */
  463. @Override
  464. public L2ItemInstance destroyItem(String process, int objectId, long count, L2PcInstance actor, Object reference)
  465. {
  466. L2ItemInstance item = getItemByObjectId(objectId);
  467. if (item == null)
  468. {
  469. return null;
  470. }
  471. return this.destroyItem(process, item, count, actor, reference);
  472. }
  473. /**
  474. * Destroy item from inventory by using its <B>itemId</B> and checks _adena and _ancientAdena
  475. * @param process : String Identifier of process triggering this action
  476. * @param itemId : int Item identifier of the item to be destroyed
  477. * @param count : int Quantity of items to be destroyed
  478. * @param actor : L2PcInstance Player requesting the item destroy
  479. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  480. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  481. */
  482. @Override
  483. public L2ItemInstance destroyItemByItemId(String process, int itemId, long count, L2PcInstance actor, Object reference)
  484. {
  485. L2ItemInstance item = getItemByItemId(itemId);
  486. if (item == null)
  487. {
  488. return null;
  489. }
  490. return this.destroyItem(process, item, count, actor, reference);
  491. }
  492. /**
  493. * Drop item from inventory and checks _adena and _ancientAdena
  494. * @param process : String Identifier of process triggering this action
  495. * @param item : L2ItemInstance to be dropped
  496. * @param actor : L2PcInstance Player requesting the item drop
  497. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  498. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  499. */
  500. @Override
  501. public L2ItemInstance dropItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  502. {
  503. item = super.dropItem(process, item, actor, reference);
  504. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  505. _adena = null;
  506. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  507. _ancientAdena = null;
  508. return item;
  509. }
  510. /**
  511. * Drop item from inventory by using its <B>objectID</B> and checks _adena and _ancientAdena
  512. * @param process : String Identifier of process triggering this action
  513. * @param objectId : int Item Instance identifier of the item to be dropped
  514. * @param count : int Quantity of items to be dropped
  515. * @param actor : L2PcInstance Player requesting the item drop
  516. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  517. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  518. */
  519. @Override
  520. public L2ItemInstance dropItem(String process, int objectId, long count, L2PcInstance actor, Object reference)
  521. {
  522. L2ItemInstance item = super.dropItem(process, objectId, count, actor, reference);
  523. if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  524. _adena = null;
  525. if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  526. _ancientAdena = null;
  527. return item;
  528. }
  529. /**
  530. * <b>Overloaded</b>, when removes item from inventory, remove also owner shortcuts.
  531. * @param item : L2ItemInstance to be removed from inventory
  532. */
  533. @Override
  534. protected boolean removeItem(L2ItemInstance item)
  535. {
  536. // Removes any reference to the item from Shortcut bar
  537. getOwner().removeItemFromShortCut(item.getObjectId());
  538. // Removes active Enchant Scroll
  539. if(item.equals(getOwner().getActiveEnchantItem()))
  540. getOwner().setActiveEnchantItem(null);
  541. if (item.getItemId() == ADENA_ID)
  542. _adena = null;
  543. else if (item.getItemId() == ANCIENT_ADENA_ID)
  544. _ancientAdena = null;
  545. synchronized(_items)
  546. {
  547. if (item.isQuestItem())
  548. {
  549. _questSlots--;
  550. if (_questSlots < 0)
  551. {
  552. _questSlots = 0;
  553. _log.warning(this + ": QuestInventory size < 0!");
  554. }
  555. }
  556. return super.removeItem(item);
  557. }
  558. }
  559. /**
  560. * Refresh the weight of equipment loaded
  561. */
  562. @Override
  563. public void refreshWeight()
  564. {
  565. super.refreshWeight();
  566. getOwner().refreshOverloaded();
  567. }
  568. /**
  569. * Get back items in inventory from database
  570. */
  571. @Override
  572. public void restore()
  573. {
  574. super.restore();
  575. _adena = getItemByItemId(ADENA_ID);
  576. _ancientAdena = getItemByItemId(ANCIENT_ADENA_ID);
  577. }
  578. public static int[][] restoreVisibleInventory(int objectId)
  579. {
  580. int[][] paperdoll = new int[31][3];
  581. Connection con = null;
  582. try
  583. {
  584. con = L2DatabaseFactory.getInstance().getConnection();
  585. PreparedStatement statement2 = con.prepareStatement(
  586. "SELECT object_id,item_id,loc_data,enchant_level FROM items WHERE owner_id=? AND loc='PAPERDOLL'");
  587. statement2.setInt(1, objectId);
  588. ResultSet invdata = statement2.executeQuery();
  589. while (invdata.next())
  590. {
  591. int slot = invdata.getInt("loc_data");
  592. paperdoll[slot][0] = invdata.getInt("object_id");
  593. paperdoll[slot][1] = invdata.getInt("item_id");
  594. paperdoll[slot][2] = invdata.getInt("enchant_level");
  595. /*if (slot == Inventory.PAPERDOLL_RHAND)
  596. {
  597. paperdoll[Inventory.PAPERDOLL_RHAND][0] = invdata.getInt("object_id");
  598. paperdoll[Inventory.PAPERDOLL_RHAND][1] = invdata.getInt("item_id");
  599. paperdoll[Inventory.PAPERDOLL_RHAND][2] = invdata.getInt("enchant_level");
  600. }*/
  601. }
  602. invdata.close();
  603. statement2.close();
  604. }
  605. catch (Exception e)
  606. {
  607. _log.log(Level.WARNING, "Could not restore inventory: " + e.getMessage(), e);
  608. }
  609. finally
  610. {
  611. L2DatabaseFactory.close(con);
  612. }
  613. return paperdoll;
  614. }
  615. public boolean validateCapacity(L2ItemInstance item)
  616. {
  617. int slots = 0;
  618. if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null) && item.getItemType() != L2EtcItemType.HERB)
  619. slots++;
  620. return validateCapacity(slots, item.isQuestItem());
  621. }
  622. @Deprecated
  623. public boolean validateCapacity(List<L2ItemInstance> items)
  624. {
  625. int slots = 0;
  626. for (L2ItemInstance item : items)
  627. if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null))
  628. slots++;
  629. return validateCapacity(slots);
  630. }
  631. public boolean validateCapacityByItemId(int ItemId)
  632. {
  633. int slots = 0;
  634. L2Item item = ItemTable.getInstance().getTemplate(ItemId);
  635. L2ItemInstance invItem = getItemByItemId(ItemId);
  636. if (!(invItem != null && invItem.isStackable()))
  637. slots++;
  638. return validateCapacity(slots, item.isQuestItem());
  639. }
  640. @Override
  641. public boolean validateCapacity(int slots)
  642. {
  643. return validateCapacity(slots, false);
  644. }
  645. public boolean validateCapacity(int slots, boolean questItem)
  646. {
  647. if (!questItem)
  648. return (_items.size() - _questSlots + slots <= _owner.getInventoryLimit());
  649. else
  650. return _questSlots + slots <= _owner.getQuestInventoryLimit();
  651. }
  652. @Override
  653. public boolean validateWeight(int weight)
  654. {
  655. if (_owner.isGM() && _owner.getAccessLevel().allowTransaction())
  656. return true; // disable weight check for GM
  657. return (_totalWeight + weight <= _owner.getMaxLoad());
  658. }
  659. /**
  660. * Set inventory block for specified IDs<br>
  661. * array reference is used for {@link PcInventory#_blockItems}
  662. * @param items array of Ids to block/allow
  663. * @param mode blocking mode {@link PcInventory#_blockMode}
  664. */
  665. public void setInventoryBlock(int[] items, int mode)
  666. {
  667. _blockMode = mode;
  668. _blockItems = items;
  669. _owner.sendPacket(new ItemList(_owner, false));
  670. }
  671. /**
  672. * Unblock blocked itemIds
  673. */
  674. public void unblock()
  675. {
  676. _blockMode = -1;
  677. _blockItems = null;
  678. _owner.sendPacket(new ItemList(_owner, false));
  679. }
  680. /**
  681. * Check if player inventory is in block mode.
  682. * @return true if some itemIds blocked
  683. */
  684. public boolean hasInventoryBlock()
  685. {
  686. return (_blockMode > -1 && _blockItems != null && _blockItems.length > 0);
  687. }
  688. /**
  689. * Block all player items
  690. */
  691. public void blockAllItems()
  692. {
  693. // temp fix, some id must be sended
  694. setInventoryBlock(new int[] {(ItemTable.getInstance().getArraySize()+2)}, 1);
  695. }
  696. /**
  697. * Return block mode
  698. * @return int {@link PcInventory#_blockMode}
  699. */
  700. public int getBlockMode()
  701. {
  702. return _blockMode;
  703. }
  704. /**
  705. * Return TIntArrayList with blocked item ids
  706. * @return TIntArrayList
  707. */
  708. public int[] getBlockItems()
  709. {
  710. return _blockItems;
  711. }
  712. /**
  713. * Check if player can use item by itemid
  714. * @param itemId int
  715. * @return true if can use
  716. */
  717. public boolean canManipulateWithItemId(int itemId)
  718. {
  719. if ((_blockMode == 0 && Util.contains(_blockItems ,itemId))
  720. || (_blockMode == 1 && !Util.contains(_blockItems ,itemId)))
  721. return false;
  722. return true;
  723. }
  724. @Override
  725. protected void addItem(L2ItemInstance item)
  726. {
  727. synchronized(_items)
  728. {
  729. if (item.isQuestItem())
  730. {
  731. _questSlots++;
  732. }
  733. super.addItem(item);
  734. }
  735. }
  736. public int getSize(boolean quest)
  737. {
  738. if (quest)
  739. return _questSlots;
  740. else
  741. return getSize() - _questSlots;
  742. }
  743. @Override
  744. public String toString()
  745. {
  746. return getClass().getSimpleName()+"["+_owner+"]";
  747. }
  748. }