Inventory.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.itemcontainer;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.data.xml.impl.ArmorSetsData;
  30. import com.l2jserver.gameserver.datatables.ItemTable;
  31. import com.l2jserver.gameserver.enums.ItemLocation;
  32. import com.l2jserver.gameserver.enums.PrivateStoreType;
  33. import com.l2jserver.gameserver.model.L2ArmorSet;
  34. import com.l2jserver.gameserver.model.L2World;
  35. import com.l2jserver.gameserver.model.PcCondOverride;
  36. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jserver.gameserver.model.holders.SkillHolder;
  38. import com.l2jserver.gameserver.model.items.L2Item;
  39. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  40. import com.l2jserver.gameserver.model.items.type.EtcItemType;
  41. import com.l2jserver.gameserver.model.items.type.WeaponType;
  42. import com.l2jserver.gameserver.model.skills.Skill;
  43. import com.l2jserver.gameserver.network.serverpackets.SkillCoolTime;
  44. import com.l2jserver.util.StringUtil;
  45. /**
  46. * This class manages inventory
  47. * @version $Revision: 1.13.2.9.2.12 $ $Date: 2005/03/29 23:15:15 $ rewritten 23.2.2006 by Advi
  48. */
  49. public abstract class Inventory extends ItemContainer
  50. {
  51. protected static final Logger _log = Logger.getLogger(Inventory.class.getName());
  52. public interface PaperdollListener
  53. {
  54. public void notifyEquiped(int slot, L2ItemInstance inst, Inventory inventory);
  55. public void notifyUnequiped(int slot, L2ItemInstance inst, Inventory inventory);
  56. }
  57. // Common Items
  58. public static final int ADENA_ID = 57;
  59. public static final int ANCIENT_ADENA_ID = 5575;
  60. public static final long MAX_ADENA = Config.MAX_ADENA;
  61. public static final int PAPERDOLL_UNDER = 0;
  62. public static final int PAPERDOLL_HEAD = 1;
  63. public static final int PAPERDOLL_HAIR = 2;
  64. public static final int PAPERDOLL_HAIR2 = 3;
  65. public static final int PAPERDOLL_NECK = 4;
  66. public static final int PAPERDOLL_RHAND = 5;
  67. public static final int PAPERDOLL_CHEST = 6;
  68. public static final int PAPERDOLL_LHAND = 7;
  69. public static final int PAPERDOLL_REAR = 8;
  70. public static final int PAPERDOLL_LEAR = 9;
  71. public static final int PAPERDOLL_GLOVES = 10;
  72. public static final int PAPERDOLL_LEGS = 11;
  73. public static final int PAPERDOLL_FEET = 12;
  74. public static final int PAPERDOLL_RFINGER = 13;
  75. public static final int PAPERDOLL_LFINGER = 14;
  76. public static final int PAPERDOLL_LBRACELET = 15;
  77. public static final int PAPERDOLL_RBRACELET = 16;
  78. public static final int PAPERDOLL_DECO1 = 17;
  79. public static final int PAPERDOLL_DECO2 = 18;
  80. public static final int PAPERDOLL_DECO3 = 19;
  81. public static final int PAPERDOLL_DECO4 = 20;
  82. public static final int PAPERDOLL_DECO5 = 21;
  83. public static final int PAPERDOLL_DECO6 = 22;
  84. public static final int PAPERDOLL_CLOAK = 23;
  85. public static final int PAPERDOLL_BELT = 24;
  86. public static final int PAPERDOLL_TOTALSLOTS = 25;
  87. // Speed percentage mods
  88. public static final double MAX_ARMOR_WEIGHT = 12000;
  89. private final L2ItemInstance[] _paperdoll;
  90. private final List<PaperdollListener> _paperdollListeners;
  91. // protected to be accessed from child classes only
  92. protected int _totalWeight;
  93. // used to quickly check for using of items of special type
  94. private int _wearedMask;
  95. // Recorder of alterations in inventory
  96. private static final class ChangeRecorder implements PaperdollListener
  97. {
  98. private final Inventory _inventory;
  99. private final List<L2ItemInstance> _changed;
  100. /**
  101. * Constructor of the ChangeRecorder
  102. * @param inventory
  103. */
  104. ChangeRecorder(Inventory inventory)
  105. {
  106. _inventory = inventory;
  107. _changed = new ArrayList<>();
  108. _inventory.addPaperdollListener(this);
  109. }
  110. /**
  111. * Add alteration in inventory when item equipped
  112. * @param slot
  113. * @param item
  114. * @param inventory
  115. */
  116. @Override
  117. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  118. {
  119. if (!_changed.contains(item))
  120. {
  121. _changed.add(item);
  122. }
  123. }
  124. /**
  125. * Add alteration in inventory when item unequipped
  126. * @param slot
  127. * @param item
  128. * @param inventory
  129. */
  130. @Override
  131. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  132. {
  133. if (!_changed.contains(item))
  134. {
  135. _changed.add(item);
  136. }
  137. }
  138. /**
  139. * Returns alterations in inventory
  140. * @return L2ItemInstance[] : array of altered items
  141. */
  142. public L2ItemInstance[] getChangedItems()
  143. {
  144. return _changed.toArray(new L2ItemInstance[_changed.size()]);
  145. }
  146. }
  147. private static final class BowCrossRodListener implements PaperdollListener
  148. {
  149. private static BowCrossRodListener instance = new BowCrossRodListener();
  150. public static BowCrossRodListener getInstance()
  151. {
  152. return instance;
  153. }
  154. @Override
  155. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  156. {
  157. if (slot != PAPERDOLL_RHAND)
  158. {
  159. return;
  160. }
  161. if (item.getItemType() == WeaponType.BOW)
  162. {
  163. L2ItemInstance arrow = inventory.getPaperdollItem(PAPERDOLL_LHAND);
  164. if (arrow != null)
  165. {
  166. inventory.setPaperdollItem(PAPERDOLL_LHAND, null);
  167. }
  168. }
  169. else if (item.getItemType() == WeaponType.CROSSBOW)
  170. {
  171. L2ItemInstance bolts = inventory.getPaperdollItem(PAPERDOLL_LHAND);
  172. if (bolts != null)
  173. {
  174. inventory.setPaperdollItem(PAPERDOLL_LHAND, null);
  175. }
  176. }
  177. else if (item.getItemType() == WeaponType.FISHINGROD)
  178. {
  179. L2ItemInstance lure = inventory.getPaperdollItem(PAPERDOLL_LHAND);
  180. if (lure != null)
  181. {
  182. inventory.setPaperdollItem(PAPERDOLL_LHAND, null);
  183. }
  184. }
  185. }
  186. @Override
  187. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  188. {
  189. if (slot != PAPERDOLL_RHAND)
  190. {
  191. return;
  192. }
  193. if (item.getItemType() == WeaponType.BOW)
  194. {
  195. L2ItemInstance arrow = inventory.findArrowForBow(item.getItem());
  196. if (arrow != null)
  197. {
  198. inventory.setPaperdollItem(PAPERDOLL_LHAND, arrow);
  199. }
  200. }
  201. else if (item.getItemType() == WeaponType.CROSSBOW)
  202. {
  203. L2ItemInstance bolts = inventory.findBoltForCrossBow(item.getItem());
  204. if (bolts != null)
  205. {
  206. inventory.setPaperdollItem(PAPERDOLL_LHAND, bolts);
  207. }
  208. }
  209. }
  210. }
  211. private static final class StatsListener implements PaperdollListener
  212. {
  213. private static StatsListener instance = new StatsListener();
  214. public static StatsListener getInstance()
  215. {
  216. return instance;
  217. }
  218. @Override
  219. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  220. {
  221. inventory.getOwner().removeStatsOwner(item);
  222. }
  223. @Override
  224. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  225. {
  226. inventory.getOwner().addStatFuncs(item.getStatFuncs(inventory.getOwner()));
  227. }
  228. }
  229. private static final class ItemSkillsListener implements PaperdollListener
  230. {
  231. private static ItemSkillsListener instance = new ItemSkillsListener();
  232. public static ItemSkillsListener getInstance()
  233. {
  234. return instance;
  235. }
  236. @Override
  237. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  238. {
  239. if (!(inventory.getOwner() instanceof L2PcInstance))
  240. {
  241. return;
  242. }
  243. final L2PcInstance player = (L2PcInstance) inventory.getOwner();
  244. Skill enchant4Skill, itemSkill;
  245. L2Item it = item.getItem();
  246. boolean update = false;
  247. boolean updateTimeStamp = false;
  248. // Remove augmentation bonuses on unequip
  249. if (item.isAugmented())
  250. {
  251. item.getAugmentation().removeBonus(player);
  252. }
  253. item.removeElementAttrBonus(player);
  254. // Remove skills bestowed from +4 armor
  255. if (item.getEnchantLevel() >= 4)
  256. {
  257. enchant4Skill = it.getEnchant4Skill();
  258. if (enchant4Skill != null)
  259. {
  260. player.removeSkill(enchant4Skill, false, enchant4Skill.isPassive());
  261. update = true;
  262. }
  263. }
  264. item.clearEnchantStats();
  265. final SkillHolder[] skills = it.getSkills();
  266. if (skills != null)
  267. {
  268. for (SkillHolder skillInfo : skills)
  269. {
  270. if (skillInfo == null)
  271. {
  272. continue;
  273. }
  274. itemSkill = skillInfo.getSkill();
  275. if (itemSkill != null)
  276. {
  277. player.removeSkill(itemSkill, false, itemSkill.isPassive());
  278. update = true;
  279. }
  280. else
  281. {
  282. _log.warning("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".");
  283. }
  284. }
  285. }
  286. if (item.isArmor())
  287. {
  288. for (L2ItemInstance itm : inventory.getItems())
  289. {
  290. if (!itm.isEquipped() || (itm.getItem().getSkills() == null) || itm.equals(item))
  291. {
  292. continue;
  293. }
  294. for (SkillHolder sk : itm.getItem().getSkills())
  295. {
  296. if (player.getSkillLevel(sk.getSkillId()) != -1)
  297. {
  298. continue;
  299. }
  300. itemSkill = sk.getSkill();
  301. if (itemSkill != null)
  302. {
  303. player.addSkill(itemSkill, false);
  304. if (itemSkill.isActive())
  305. {
  306. if (!player.hasSkillReuse(itemSkill.getReuseHashCode()))
  307. {
  308. int equipDelay = item.getEquipReuseDelay();
  309. if (equipDelay > 0)
  310. {
  311. player.addTimeStamp(itemSkill, equipDelay);
  312. player.disableSkill(itemSkill, equipDelay);
  313. }
  314. }
  315. updateTimeStamp = true;
  316. }
  317. update = true;
  318. }
  319. }
  320. }
  321. }
  322. // Apply skill, if weapon have "skills on unequip"
  323. Skill unequipSkill = it.getUnequipSkill();
  324. if (unequipSkill != null)
  325. {
  326. L2PcInstance[] targets =
  327. {
  328. player
  329. };
  330. unequipSkill.activateSkill(player, targets);
  331. }
  332. if (update)
  333. {
  334. player.sendSkillList();
  335. if (updateTimeStamp)
  336. {
  337. player.sendPacket(new SkillCoolTime(player));
  338. }
  339. }
  340. }
  341. @Override
  342. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  343. {
  344. if (!(inventory.getOwner() instanceof L2PcInstance))
  345. {
  346. return;
  347. }
  348. final L2PcInstance player = (L2PcInstance) inventory.getOwner();
  349. Skill enchant4Skill, itemSkill;
  350. L2Item it = item.getItem();
  351. boolean update = false;
  352. boolean updateTimeStamp = false;
  353. // Apply augmentation bonuses on equip
  354. if (item.isAugmented())
  355. {
  356. item.getAugmentation().applyBonus(player);
  357. }
  358. item.updateElementAttrBonus(player);
  359. // Add skills bestowed from +4 armor
  360. if (item.getEnchantLevel() >= 4)
  361. {
  362. enchant4Skill = it.getEnchant4Skill();
  363. if (enchant4Skill != null)
  364. {
  365. player.addSkill(enchant4Skill, false);
  366. update = true;
  367. }
  368. }
  369. item.applyEnchantStats();
  370. final SkillHolder[] skills = it.getSkills();
  371. if (skills != null)
  372. {
  373. for (SkillHolder skillInfo : skills)
  374. {
  375. if (skillInfo == null)
  376. {
  377. continue;
  378. }
  379. itemSkill = skillInfo.getSkill();
  380. if (itemSkill != null)
  381. {
  382. itemSkill.setReferenceItemId(item.getId());
  383. player.addSkill(itemSkill, false);
  384. if (itemSkill.isActive())
  385. {
  386. if (!player.hasSkillReuse(itemSkill.getReuseHashCode()))
  387. {
  388. int equipDelay = item.getEquipReuseDelay();
  389. if (equipDelay > 0)
  390. {
  391. player.addTimeStamp(itemSkill, equipDelay);
  392. player.disableSkill(itemSkill, equipDelay);
  393. }
  394. }
  395. updateTimeStamp = true;
  396. }
  397. update = true;
  398. }
  399. else
  400. {
  401. _log.warning("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".");
  402. }
  403. }
  404. }
  405. if (update)
  406. {
  407. player.sendSkillList();
  408. if (updateTimeStamp)
  409. {
  410. player.sendPacket(new SkillCoolTime(player));
  411. }
  412. }
  413. }
  414. }
  415. private static final class ArmorSetListener implements PaperdollListener
  416. {
  417. private static ArmorSetListener instance = new ArmorSetListener();
  418. public static ArmorSetListener getInstance()
  419. {
  420. return instance;
  421. }
  422. @Override
  423. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  424. {
  425. if (!(inventory.getOwner() instanceof L2PcInstance))
  426. {
  427. return;
  428. }
  429. final L2PcInstance player = (L2PcInstance) inventory.getOwner();
  430. // Checks if player is wearing a chest item
  431. final L2ItemInstance chestItem = inventory.getPaperdollItem(PAPERDOLL_CHEST);
  432. if (chestItem == null)
  433. {
  434. return;
  435. }
  436. // Checks for armor set for the equipped chest.
  437. if (!ArmorSetsData.getInstance().isArmorSet(chestItem.getId()))
  438. {
  439. return;
  440. }
  441. final L2ArmorSet armorSet = ArmorSetsData.getInstance().getSet(chestItem.getId());
  442. boolean update = false;
  443. boolean updateTimeStamp = false;
  444. // Checks if equipped item is part of set
  445. if (armorSet.containItem(slot, item.getId()))
  446. {
  447. if (armorSet.containAll(player))
  448. {
  449. Skill itemSkill;
  450. final List<SkillHolder> skills = armorSet.getSkills();
  451. if (skills != null)
  452. {
  453. for (SkillHolder holder : skills)
  454. {
  455. itemSkill = holder.getSkill();
  456. if (itemSkill != null)
  457. {
  458. player.addSkill(itemSkill, false);
  459. if (itemSkill.isActive())
  460. {
  461. if (!player.hasSkillReuse(itemSkill.getReuseHashCode()))
  462. {
  463. int equipDelay = item.getEquipReuseDelay();
  464. if (equipDelay > 0)
  465. {
  466. player.addTimeStamp(itemSkill, equipDelay);
  467. player.disableSkill(itemSkill, equipDelay);
  468. }
  469. }
  470. updateTimeStamp = true;
  471. }
  472. update = true;
  473. }
  474. else
  475. {
  476. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  477. }
  478. }
  479. }
  480. if (armorSet.containShield(player)) // has shield from set
  481. {
  482. for (SkillHolder holder : armorSet.getShieldSkillId())
  483. {
  484. if (holder.getSkill() != null)
  485. {
  486. player.addSkill(holder.getSkill(), false);
  487. update = true;
  488. }
  489. else
  490. {
  491. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  492. }
  493. }
  494. }
  495. if (armorSet.isEnchanted6(player)) // has all parts of set enchanted to 6 or more
  496. {
  497. for (SkillHolder holder : armorSet.getEnchant6skillId())
  498. {
  499. if (holder.getSkill() != null)
  500. {
  501. player.addSkill(holder.getSkill(), false);
  502. update = true;
  503. }
  504. else
  505. {
  506. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  507. }
  508. }
  509. }
  510. }
  511. }
  512. else if (armorSet.containShield(item.getId()))
  513. {
  514. for (SkillHolder holder : armorSet.getShieldSkillId())
  515. {
  516. if (holder.getSkill() != null)
  517. {
  518. player.addSkill(holder.getSkill(), false);
  519. update = true;
  520. }
  521. else
  522. {
  523. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  524. }
  525. }
  526. }
  527. if (update)
  528. {
  529. player.sendSkillList();
  530. if (updateTimeStamp)
  531. {
  532. player.sendPacket(new SkillCoolTime(player));
  533. }
  534. }
  535. }
  536. @Override
  537. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  538. {
  539. if (!(inventory.getOwner() instanceof L2PcInstance))
  540. {
  541. return;
  542. }
  543. final L2PcInstance player = (L2PcInstance) inventory.getOwner();
  544. boolean remove = false;
  545. Skill itemSkill;
  546. List<SkillHolder> skills = null;
  547. List<SkillHolder> shieldSkill = null; // shield skill
  548. List<SkillHolder> skillId6 = null; // enchant +6 skill
  549. if (slot == PAPERDOLL_CHEST)
  550. {
  551. if (!ArmorSetsData.getInstance().isArmorSet(item.getId()))
  552. {
  553. return;
  554. }
  555. final L2ArmorSet armorSet = ArmorSetsData.getInstance().getSet(item.getId());
  556. remove = true;
  557. skills = armorSet.getSkills();
  558. shieldSkill = armorSet.getShieldSkillId();
  559. skillId6 = armorSet.getEnchant6skillId();
  560. }
  561. else
  562. {
  563. L2ItemInstance chestItem = inventory.getPaperdollItem(PAPERDOLL_CHEST);
  564. if (chestItem == null)
  565. {
  566. return;
  567. }
  568. L2ArmorSet armorSet = ArmorSetsData.getInstance().getSet(chestItem.getId());
  569. if (armorSet == null)
  570. {
  571. return;
  572. }
  573. if (armorSet.containItem(slot, item.getId())) // removed part of set
  574. {
  575. remove = true;
  576. skills = armorSet.getSkills();
  577. shieldSkill = armorSet.getShieldSkillId();
  578. skillId6 = armorSet.getEnchant6skillId();
  579. }
  580. else if (armorSet.containShield(item.getId())) // removed shield
  581. {
  582. remove = true;
  583. shieldSkill = armorSet.getShieldSkillId();
  584. }
  585. }
  586. if (remove)
  587. {
  588. if (skills != null)
  589. {
  590. for (SkillHolder holder : skills)
  591. {
  592. itemSkill = holder.getSkill();
  593. if (itemSkill != null)
  594. {
  595. player.removeSkill(itemSkill, false, itemSkill.isPassive());
  596. }
  597. else
  598. {
  599. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  600. }
  601. }
  602. }
  603. if (shieldSkill != null)
  604. {
  605. for (SkillHolder holder : shieldSkill)
  606. {
  607. itemSkill = holder.getSkill();
  608. if (itemSkill != null)
  609. {
  610. player.removeSkill(itemSkill, false, itemSkill.isPassive());
  611. }
  612. else
  613. {
  614. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  615. }
  616. }
  617. }
  618. if (skillId6 != null)
  619. {
  620. for (SkillHolder holder : skillId6)
  621. {
  622. itemSkill = holder.getSkill();
  623. if (itemSkill != null)
  624. {
  625. player.removeSkill(itemSkill, false, itemSkill.isPassive());
  626. }
  627. else
  628. {
  629. _log.warning("Inventory.ArmorSetListener: Incorrect skill: " + holder + ".");
  630. }
  631. }
  632. }
  633. player.checkItemRestriction();
  634. player.sendSkillList();
  635. }
  636. }
  637. }
  638. private static final class BraceletListener implements PaperdollListener
  639. {
  640. private static BraceletListener instance = new BraceletListener();
  641. public static BraceletListener getInstance()
  642. {
  643. return instance;
  644. }
  645. @Override
  646. public void notifyUnequiped(int slot, L2ItemInstance item, Inventory inventory)
  647. {
  648. if (item.getItem().getBodyPart() == L2Item.SLOT_R_BRACELET)
  649. {
  650. inventory.unEquipItemInSlot(PAPERDOLL_DECO1);
  651. inventory.unEquipItemInSlot(PAPERDOLL_DECO2);
  652. inventory.unEquipItemInSlot(PAPERDOLL_DECO3);
  653. inventory.unEquipItemInSlot(PAPERDOLL_DECO4);
  654. inventory.unEquipItemInSlot(PAPERDOLL_DECO5);
  655. inventory.unEquipItemInSlot(PAPERDOLL_DECO6);
  656. }
  657. }
  658. // Note (April 3, 2009): Currently on equip, talismans do not display properly, do we need checks here to fix this?
  659. @Override
  660. public void notifyEquiped(int slot, L2ItemInstance item, Inventory inventory)
  661. {
  662. }
  663. }
  664. /**
  665. * Constructor of the inventory
  666. */
  667. protected Inventory()
  668. {
  669. _paperdoll = new L2ItemInstance[PAPERDOLL_TOTALSLOTS];
  670. _paperdollListeners = new ArrayList<>();
  671. if (this instanceof PcInventory)
  672. {
  673. addPaperdollListener(ArmorSetListener.getInstance());
  674. addPaperdollListener(BowCrossRodListener.getInstance());
  675. addPaperdollListener(ItemSkillsListener.getInstance());
  676. addPaperdollListener(BraceletListener.getInstance());
  677. }
  678. // common
  679. addPaperdollListener(StatsListener.getInstance());
  680. }
  681. protected abstract ItemLocation getEquipLocation();
  682. /**
  683. * Returns the instance of new ChangeRecorder
  684. * @return ChangeRecorder
  685. */
  686. private ChangeRecorder newRecorder()
  687. {
  688. return new ChangeRecorder(this);
  689. }
  690. /**
  691. * Drop item from inventory and updates database
  692. * @param process : String Identifier of process triggering this action
  693. * @param item : L2ItemInstance to be dropped
  694. * @param actor : L2PcInstance Player requesting the item drop
  695. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  696. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  697. */
  698. public L2ItemInstance dropItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  699. {
  700. if (item == null)
  701. {
  702. return null;
  703. }
  704. synchronized (item)
  705. {
  706. if (!_items.contains(item))
  707. {
  708. return null;
  709. }
  710. removeItem(item);
  711. item.setOwnerId(process, 0, actor, reference);
  712. item.setItemLocation(ItemLocation.VOID);
  713. item.setLastChange(L2ItemInstance.REMOVED);
  714. item.updateDatabase();
  715. refreshWeight();
  716. }
  717. return item;
  718. }
  719. /**
  720. * Drop item from inventory by using its <B>objectID</B> and updates database
  721. * @param process : String Identifier of process triggering this action
  722. * @param objectId : int Item Instance identifier of the item to be dropped
  723. * @param count : int Quantity of items to be dropped
  724. * @param actor : L2PcInstance Player requesting the item drop
  725. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  726. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  727. */
  728. public L2ItemInstance dropItem(String process, int objectId, long count, L2PcInstance actor, Object reference)
  729. {
  730. L2ItemInstance item = getItemByObjectId(objectId);
  731. if (item == null)
  732. {
  733. return null;
  734. }
  735. synchronized (item)
  736. {
  737. if (!_items.contains(item))
  738. {
  739. return null;
  740. }
  741. // Adjust item quantity and create new instance to drop
  742. // Directly drop entire item
  743. if (item.getCount() > count)
  744. {
  745. item.changeCount(process, -count, actor, reference);
  746. item.setLastChange(L2ItemInstance.MODIFIED);
  747. item.updateDatabase();
  748. item = ItemTable.getInstance().createItem(process, item.getId(), count, actor, reference);
  749. item.updateDatabase();
  750. refreshWeight();
  751. return item;
  752. }
  753. }
  754. return dropItem(process, item, actor, reference);
  755. }
  756. /**
  757. * Adds item to inventory for further adjustments and Equip it if necessary (itemlocation defined)
  758. * @param item : L2ItemInstance to be added from inventory
  759. */
  760. @Override
  761. protected void addItem(L2ItemInstance item)
  762. {
  763. super.addItem(item);
  764. if (item.isEquipped())
  765. {
  766. equipItem(item);
  767. }
  768. }
  769. /**
  770. * Removes item from inventory for further adjustments.
  771. * @param item : L2ItemInstance to be removed from inventory
  772. */
  773. @Override
  774. protected boolean removeItem(L2ItemInstance item)
  775. {
  776. // Unequip item if equiped
  777. for (int i = 0; i < _paperdoll.length; i++)
  778. {
  779. if (_paperdoll[i] == item)
  780. {
  781. unEquipItemInSlot(i);
  782. }
  783. }
  784. return super.removeItem(item);
  785. }
  786. /**
  787. * @param slot the slot.
  788. * @return the item in the paperdoll slot
  789. */
  790. public L2ItemInstance getPaperdollItem(int slot)
  791. {
  792. return _paperdoll[slot];
  793. }
  794. /**
  795. * @param slot the slot.
  796. * @return {@code true} if specified paperdoll slot is empty, {@code false} otherwise
  797. */
  798. public boolean isPaperdollSlotEmpty(int slot)
  799. {
  800. return _paperdoll[slot] == null;
  801. }
  802. public static int getPaperdollIndex(int slot)
  803. {
  804. switch (slot)
  805. {
  806. case L2Item.SLOT_UNDERWEAR:
  807. return PAPERDOLL_UNDER;
  808. case L2Item.SLOT_R_EAR:
  809. return PAPERDOLL_REAR;
  810. case L2Item.SLOT_LR_EAR:
  811. case L2Item.SLOT_L_EAR:
  812. return PAPERDOLL_LEAR;
  813. case L2Item.SLOT_NECK:
  814. return PAPERDOLL_NECK;
  815. case L2Item.SLOT_R_FINGER:
  816. case L2Item.SLOT_LR_FINGER:
  817. return PAPERDOLL_RFINGER;
  818. case L2Item.SLOT_L_FINGER:
  819. return PAPERDOLL_LFINGER;
  820. case L2Item.SLOT_HEAD:
  821. return PAPERDOLL_HEAD;
  822. case L2Item.SLOT_R_HAND:
  823. case L2Item.SLOT_LR_HAND:
  824. return PAPERDOLL_RHAND;
  825. case L2Item.SLOT_L_HAND:
  826. return PAPERDOLL_LHAND;
  827. case L2Item.SLOT_GLOVES:
  828. return PAPERDOLL_GLOVES;
  829. case L2Item.SLOT_CHEST:
  830. case L2Item.SLOT_FULL_ARMOR:
  831. case L2Item.SLOT_ALLDRESS:
  832. return PAPERDOLL_CHEST;
  833. case L2Item.SLOT_LEGS:
  834. return PAPERDOLL_LEGS;
  835. case L2Item.SLOT_FEET:
  836. return PAPERDOLL_FEET;
  837. case L2Item.SLOT_BACK:
  838. return PAPERDOLL_CLOAK;
  839. case L2Item.SLOT_HAIR:
  840. case L2Item.SLOT_HAIRALL:
  841. return PAPERDOLL_HAIR;
  842. case L2Item.SLOT_HAIR2:
  843. return PAPERDOLL_HAIR2;
  844. case L2Item.SLOT_R_BRACELET:
  845. return PAPERDOLL_RBRACELET;
  846. case L2Item.SLOT_L_BRACELET:
  847. return PAPERDOLL_LBRACELET;
  848. case L2Item.SLOT_DECO:
  849. return PAPERDOLL_DECO1; // return first we deal with it later
  850. case L2Item.SLOT_BELT:
  851. return PAPERDOLL_BELT;
  852. }
  853. return -1;
  854. }
  855. /**
  856. * Returns the item in the paperdoll L2Item slot
  857. * @param slot identifier
  858. * @return L2ItemInstance
  859. */
  860. public L2ItemInstance getPaperdollItemByL2ItemId(int slot)
  861. {
  862. int index = getPaperdollIndex(slot);
  863. if (index == -1)
  864. {
  865. return null;
  866. }
  867. return _paperdoll[index];
  868. }
  869. /**
  870. * Returns the ID of the item in the paperdoll slot
  871. * @param slot : int designating the slot
  872. * @return int designating the ID of the item
  873. */
  874. public int getPaperdollItemId(int slot)
  875. {
  876. L2ItemInstance item = _paperdoll[slot];
  877. if (item != null)
  878. {
  879. return item.getId();
  880. }
  881. return 0;
  882. }
  883. /**
  884. * Returns the ID of the item in the paperdoll slot
  885. * @param slot : int designating the slot
  886. * @return int designating the ID of the item
  887. */
  888. public int getPaperdollItemDisplayId(int slot)
  889. {
  890. final L2ItemInstance item = _paperdoll[slot];
  891. return (item != null) ? item.getDisplayId() : 0;
  892. }
  893. public int getPaperdollAugmentationId(int slot)
  894. {
  895. final L2ItemInstance item = _paperdoll[slot];
  896. return ((item != null) && (item.getAugmentation() != null)) ? item.getAugmentation().getAugmentationId() : 0;
  897. }
  898. /**
  899. * Returns the objectID associated to the item in the paperdoll slot
  900. * @param slot : int pointing out the slot
  901. * @return int designating the objectID
  902. */
  903. public int getPaperdollObjectId(int slot)
  904. {
  905. final L2ItemInstance item = _paperdoll[slot];
  906. return (item != null) ? item.getObjectId() : 0;
  907. }
  908. /**
  909. * Adds new inventory's paperdoll listener.
  910. * @param listener the new listener
  911. */
  912. public synchronized void addPaperdollListener(PaperdollListener listener)
  913. {
  914. assert !_paperdollListeners.contains(listener);
  915. _paperdollListeners.add(listener);
  916. }
  917. /**
  918. * Removes a paperdoll listener.
  919. * @param listener the listener to be deleted
  920. */
  921. public synchronized void removePaperdollListener(PaperdollListener listener)
  922. {
  923. _paperdollListeners.remove(listener);
  924. }
  925. /**
  926. * Equips an item in the given slot of the paperdoll.<br>
  927. * <U><I>Remark :</I></U> The item <B>must be</B> in the inventory already.
  928. * @param slot : int pointing out the slot of the paperdoll
  929. * @param item : L2ItemInstance pointing out the item to add in slot
  930. * @return L2ItemInstance designating the item placed in the slot before
  931. */
  932. public synchronized L2ItemInstance setPaperdollItem(int slot, L2ItemInstance item)
  933. {
  934. L2ItemInstance old = _paperdoll[slot];
  935. if (old != item)
  936. {
  937. if (old != null)
  938. {
  939. _paperdoll[slot] = null;
  940. // Put old item from paperdoll slot to base location
  941. old.setItemLocation(getBaseLocation());
  942. old.setLastChange(L2ItemInstance.MODIFIED);
  943. // Get the mask for paperdoll
  944. int mask = 0;
  945. for (int i = 0; i < PAPERDOLL_TOTALSLOTS; i++)
  946. {
  947. L2ItemInstance pi = _paperdoll[i];
  948. if (pi != null)
  949. {
  950. mask |= pi.getItem().getItemMask();
  951. }
  952. }
  953. _wearedMask = mask;
  954. // Notify all paperdoll listener in order to unequip old item in slot
  955. for (PaperdollListener listener : _paperdollListeners)
  956. {
  957. if (listener == null)
  958. {
  959. continue;
  960. }
  961. listener.notifyUnequiped(slot, old, this);
  962. }
  963. old.updateDatabase();
  964. }
  965. // Add new item in slot of paperdoll
  966. if (item != null)
  967. {
  968. _paperdoll[slot] = item;
  969. item.setItemLocation(getEquipLocation(), slot);
  970. item.setLastChange(L2ItemInstance.MODIFIED);
  971. _wearedMask |= item.getItem().getItemMask();
  972. for (PaperdollListener listener : _paperdollListeners)
  973. {
  974. if (listener == null)
  975. {
  976. continue;
  977. }
  978. listener.notifyEquiped(slot, item, this);
  979. }
  980. item.updateDatabase();
  981. }
  982. }
  983. return old;
  984. }
  985. /**
  986. * @return the mask of wore item
  987. */
  988. public int getWearedMask()
  989. {
  990. return _wearedMask;
  991. }
  992. public int getSlotFromItem(L2ItemInstance item)
  993. {
  994. int slot = -1;
  995. final int location = item.getLocationSlot();
  996. switch (location)
  997. {
  998. case PAPERDOLL_UNDER:
  999. slot = L2Item.SLOT_UNDERWEAR;
  1000. break;
  1001. case PAPERDOLL_LEAR:
  1002. slot = L2Item.SLOT_L_EAR;
  1003. break;
  1004. case PAPERDOLL_REAR:
  1005. slot = L2Item.SLOT_R_EAR;
  1006. break;
  1007. case PAPERDOLL_NECK:
  1008. slot = L2Item.SLOT_NECK;
  1009. break;
  1010. case PAPERDOLL_RFINGER:
  1011. slot = L2Item.SLOT_R_FINGER;
  1012. break;
  1013. case PAPERDOLL_LFINGER:
  1014. slot = L2Item.SLOT_L_FINGER;
  1015. break;
  1016. case PAPERDOLL_HAIR:
  1017. slot = L2Item.SLOT_HAIR;
  1018. break;
  1019. case PAPERDOLL_HAIR2:
  1020. slot = L2Item.SLOT_HAIR2;
  1021. break;
  1022. case PAPERDOLL_HEAD:
  1023. slot = L2Item.SLOT_HEAD;
  1024. break;
  1025. case PAPERDOLL_RHAND:
  1026. slot = L2Item.SLOT_R_HAND;
  1027. break;
  1028. case PAPERDOLL_LHAND:
  1029. slot = L2Item.SLOT_L_HAND;
  1030. break;
  1031. case PAPERDOLL_GLOVES:
  1032. slot = L2Item.SLOT_GLOVES;
  1033. break;
  1034. case PAPERDOLL_CHEST:
  1035. slot = item.getItem().getBodyPart();
  1036. break;
  1037. case PAPERDOLL_LEGS:
  1038. slot = L2Item.SLOT_LEGS;
  1039. break;
  1040. case PAPERDOLL_CLOAK:
  1041. slot = L2Item.SLOT_BACK;
  1042. break;
  1043. case PAPERDOLL_FEET:
  1044. slot = L2Item.SLOT_FEET;
  1045. break;
  1046. case PAPERDOLL_LBRACELET:
  1047. slot = L2Item.SLOT_L_BRACELET;
  1048. break;
  1049. case PAPERDOLL_RBRACELET:
  1050. slot = L2Item.SLOT_R_BRACELET;
  1051. break;
  1052. case PAPERDOLL_DECO1:
  1053. case PAPERDOLL_DECO2:
  1054. case PAPERDOLL_DECO3:
  1055. case PAPERDOLL_DECO4:
  1056. case PAPERDOLL_DECO5:
  1057. case PAPERDOLL_DECO6:
  1058. slot = L2Item.SLOT_DECO;
  1059. break;
  1060. case PAPERDOLL_BELT:
  1061. slot = L2Item.SLOT_BELT;
  1062. break;
  1063. }
  1064. return slot;
  1065. }
  1066. /**
  1067. * Unequips item in body slot and returns alterations.<BR>
  1068. * <B>If you dont need return value use {@link Inventory#unEquipItemInBodySlot(int)} instead</B>
  1069. * @param slot : int designating the slot of the paperdoll
  1070. * @return L2ItemInstance[] : list of changes
  1071. */
  1072. public L2ItemInstance[] unEquipItemInBodySlotAndRecord(int slot)
  1073. {
  1074. Inventory.ChangeRecorder recorder = newRecorder();
  1075. try
  1076. {
  1077. unEquipItemInBodySlot(slot);
  1078. }
  1079. finally
  1080. {
  1081. removePaperdollListener(recorder);
  1082. }
  1083. return recorder.getChangedItems();
  1084. }
  1085. /**
  1086. * Sets item in slot of the paperdoll to null value
  1087. * @param pdollSlot : int designating the slot
  1088. * @return L2ItemInstance designating the item in slot before change
  1089. */
  1090. public L2ItemInstance unEquipItemInSlot(int pdollSlot)
  1091. {
  1092. return setPaperdollItem(pdollSlot, null);
  1093. }
  1094. /**
  1095. * Unequips item in slot and returns alterations<BR>
  1096. * <B>If you dont need return value use {@link Inventory#unEquipItemInSlot(int)} instead</B>
  1097. * @param slot : int designating the slot
  1098. * @return L2ItemInstance[] : list of items altered
  1099. */
  1100. public L2ItemInstance[] unEquipItemInSlotAndRecord(int slot)
  1101. {
  1102. Inventory.ChangeRecorder recorder = newRecorder();
  1103. try
  1104. {
  1105. unEquipItemInSlot(slot);
  1106. if (getOwner() instanceof L2PcInstance)
  1107. {
  1108. ((L2PcInstance) getOwner()).refreshExpertisePenalty();
  1109. }
  1110. }
  1111. finally
  1112. {
  1113. removePaperdollListener(recorder);
  1114. }
  1115. return recorder.getChangedItems();
  1116. }
  1117. /**
  1118. * Unequips item in slot (i.e. equips with default value)
  1119. * @param slot : int designating the slot
  1120. * @return {@link L2ItemInstance} designating the item placed in the slot
  1121. */
  1122. public L2ItemInstance unEquipItemInBodySlot(int slot)
  1123. {
  1124. if (Config.DEBUG)
  1125. {
  1126. _log.info(Inventory.class.getSimpleName() + ": Unequip body slot:" + slot);
  1127. }
  1128. int pdollSlot = -1;
  1129. switch (slot)
  1130. {
  1131. case L2Item.SLOT_L_EAR:
  1132. pdollSlot = PAPERDOLL_LEAR;
  1133. break;
  1134. case L2Item.SLOT_R_EAR:
  1135. pdollSlot = PAPERDOLL_REAR;
  1136. break;
  1137. case L2Item.SLOT_NECK:
  1138. pdollSlot = PAPERDOLL_NECK;
  1139. break;
  1140. case L2Item.SLOT_R_FINGER:
  1141. pdollSlot = PAPERDOLL_RFINGER;
  1142. break;
  1143. case L2Item.SLOT_L_FINGER:
  1144. pdollSlot = PAPERDOLL_LFINGER;
  1145. break;
  1146. case L2Item.SLOT_HAIR:
  1147. pdollSlot = PAPERDOLL_HAIR;
  1148. break;
  1149. case L2Item.SLOT_HAIR2:
  1150. pdollSlot = PAPERDOLL_HAIR2;
  1151. break;
  1152. case L2Item.SLOT_HAIRALL:
  1153. setPaperdollItem(PAPERDOLL_HAIR, null);
  1154. pdollSlot = PAPERDOLL_HAIR;
  1155. break;
  1156. case L2Item.SLOT_HEAD:
  1157. pdollSlot = PAPERDOLL_HEAD;
  1158. break;
  1159. case L2Item.SLOT_R_HAND:
  1160. case L2Item.SLOT_LR_HAND:
  1161. pdollSlot = PAPERDOLL_RHAND;
  1162. break;
  1163. case L2Item.SLOT_L_HAND:
  1164. pdollSlot = PAPERDOLL_LHAND;
  1165. break;
  1166. case L2Item.SLOT_GLOVES:
  1167. pdollSlot = PAPERDOLL_GLOVES;
  1168. break;
  1169. case L2Item.SLOT_CHEST:
  1170. case L2Item.SLOT_ALLDRESS:
  1171. case L2Item.SLOT_FULL_ARMOR:
  1172. pdollSlot = PAPERDOLL_CHEST;
  1173. break;
  1174. case L2Item.SLOT_LEGS:
  1175. pdollSlot = PAPERDOLL_LEGS;
  1176. break;
  1177. case L2Item.SLOT_BACK:
  1178. pdollSlot = PAPERDOLL_CLOAK;
  1179. break;
  1180. case L2Item.SLOT_FEET:
  1181. pdollSlot = PAPERDOLL_FEET;
  1182. break;
  1183. case L2Item.SLOT_UNDERWEAR:
  1184. pdollSlot = PAPERDOLL_UNDER;
  1185. break;
  1186. case L2Item.SLOT_L_BRACELET:
  1187. pdollSlot = PAPERDOLL_LBRACELET;
  1188. break;
  1189. case L2Item.SLOT_R_BRACELET:
  1190. pdollSlot = PAPERDOLL_RBRACELET;
  1191. break;
  1192. case L2Item.SLOT_DECO:
  1193. pdollSlot = PAPERDOLL_DECO1;
  1194. break;
  1195. case L2Item.SLOT_BELT:
  1196. pdollSlot = PAPERDOLL_BELT;
  1197. break;
  1198. default:
  1199. _log.info("Unhandled slot type: " + slot);
  1200. _log.info(StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  1201. }
  1202. if (pdollSlot >= 0)
  1203. {
  1204. L2ItemInstance old = setPaperdollItem(pdollSlot, null);
  1205. if (old != null)
  1206. {
  1207. if (getOwner() instanceof L2PcInstance)
  1208. {
  1209. ((L2PcInstance) getOwner()).refreshExpertisePenalty();
  1210. }
  1211. }
  1212. return old;
  1213. }
  1214. return null;
  1215. }
  1216. /**
  1217. * Equips item and returns list of alterations<BR>
  1218. * <B>If you don't need return value use {@link Inventory#equipItem(L2ItemInstance)} instead</B>
  1219. * @param item : L2ItemInstance corresponding to the item
  1220. * @return L2ItemInstance[] : list of alterations
  1221. */
  1222. public L2ItemInstance[] equipItemAndRecord(L2ItemInstance item)
  1223. {
  1224. Inventory.ChangeRecorder recorder = newRecorder();
  1225. try
  1226. {
  1227. equipItem(item);
  1228. }
  1229. finally
  1230. {
  1231. removePaperdollListener(recorder);
  1232. }
  1233. return recorder.getChangedItems();
  1234. }
  1235. /**
  1236. * Equips item in slot of paperdoll.
  1237. * @param item : L2ItemInstance designating the item and slot used.
  1238. */
  1239. public void equipItem(L2ItemInstance item)
  1240. {
  1241. if ((getOwner() instanceof L2PcInstance) && (((L2PcInstance) getOwner()).getPrivateStoreType() != PrivateStoreType.NONE))
  1242. {
  1243. return;
  1244. }
  1245. if (getOwner() instanceof L2PcInstance)
  1246. {
  1247. L2PcInstance player = (L2PcInstance) getOwner();
  1248. if (!player.canOverrideCond(PcCondOverride.ITEM_CONDITIONS) && !player.isHero() && item.isHeroItem())
  1249. {
  1250. return;
  1251. }
  1252. }
  1253. int targetSlot = item.getItem().getBodyPart();
  1254. // Check if player is using Formal Wear and item isn't Wedding Bouquet.
  1255. L2ItemInstance formal = getPaperdollItem(PAPERDOLL_CHEST);
  1256. if ((item.getId() != 21163) && (formal != null) && (formal.getItem().getBodyPart() == L2Item.SLOT_ALLDRESS))
  1257. {
  1258. // only chest target can pass this
  1259. switch (targetSlot)
  1260. {
  1261. case L2Item.SLOT_LR_HAND:
  1262. case L2Item.SLOT_L_HAND:
  1263. case L2Item.SLOT_R_HAND:
  1264. case L2Item.SLOT_LEGS:
  1265. case L2Item.SLOT_FEET:
  1266. case L2Item.SLOT_GLOVES:
  1267. case L2Item.SLOT_HEAD:
  1268. return;
  1269. }
  1270. }
  1271. switch (targetSlot)
  1272. {
  1273. case L2Item.SLOT_LR_HAND:
  1274. {
  1275. setPaperdollItem(PAPERDOLL_LHAND, null);
  1276. setPaperdollItem(PAPERDOLL_RHAND, item);
  1277. break;
  1278. }
  1279. case L2Item.SLOT_L_HAND:
  1280. {
  1281. L2ItemInstance rh = getPaperdollItem(PAPERDOLL_RHAND);
  1282. if ((rh != null) && (rh.getItem().getBodyPart() == L2Item.SLOT_LR_HAND) && !(((rh.getItemType() == WeaponType.BOW) && (item.getItemType() == EtcItemType.ARROW)) || ((rh.getItemType() == WeaponType.CROSSBOW) && (item.getItemType() == EtcItemType.BOLT)) || ((rh.getItemType() == WeaponType.FISHINGROD) && (item.getItemType() == EtcItemType.LURE))))
  1283. {
  1284. setPaperdollItem(PAPERDOLL_RHAND, null);
  1285. }
  1286. setPaperdollItem(PAPERDOLL_LHAND, item);
  1287. break;
  1288. }
  1289. case L2Item.SLOT_R_HAND:
  1290. {
  1291. // don't care about arrows, listener will unequip them (hopefully)
  1292. setPaperdollItem(PAPERDOLL_RHAND, item);
  1293. break;
  1294. }
  1295. case L2Item.SLOT_L_EAR:
  1296. case L2Item.SLOT_R_EAR:
  1297. case L2Item.SLOT_LR_EAR:
  1298. {
  1299. if (_paperdoll[PAPERDOLL_LEAR] == null)
  1300. {
  1301. setPaperdollItem(PAPERDOLL_LEAR, item);
  1302. }
  1303. else if (_paperdoll[PAPERDOLL_REAR] == null)
  1304. {
  1305. setPaperdollItem(PAPERDOLL_REAR, item);
  1306. }
  1307. else
  1308. {
  1309. setPaperdollItem(PAPERDOLL_LEAR, item);
  1310. }
  1311. break;
  1312. }
  1313. case L2Item.SLOT_L_FINGER:
  1314. case L2Item.SLOT_R_FINGER:
  1315. case L2Item.SLOT_LR_FINGER:
  1316. {
  1317. if (_paperdoll[PAPERDOLL_LFINGER] == null)
  1318. {
  1319. setPaperdollItem(PAPERDOLL_LFINGER, item);
  1320. }
  1321. else if (_paperdoll[PAPERDOLL_RFINGER] == null)
  1322. {
  1323. setPaperdollItem(PAPERDOLL_RFINGER, item);
  1324. }
  1325. else
  1326. {
  1327. setPaperdollItem(PAPERDOLL_LFINGER, item);
  1328. }
  1329. break;
  1330. }
  1331. case L2Item.SLOT_NECK:
  1332. setPaperdollItem(PAPERDOLL_NECK, item);
  1333. break;
  1334. case L2Item.SLOT_FULL_ARMOR:
  1335. setPaperdollItem(PAPERDOLL_LEGS, null);
  1336. setPaperdollItem(PAPERDOLL_CHEST, item);
  1337. break;
  1338. case L2Item.SLOT_CHEST:
  1339. setPaperdollItem(PAPERDOLL_CHEST, item);
  1340. break;
  1341. case L2Item.SLOT_LEGS:
  1342. {
  1343. // handle full armor
  1344. L2ItemInstance chest = getPaperdollItem(PAPERDOLL_CHEST);
  1345. if ((chest != null) && (chest.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR))
  1346. {
  1347. setPaperdollItem(PAPERDOLL_CHEST, null);
  1348. }
  1349. setPaperdollItem(PAPERDOLL_LEGS, item);
  1350. break;
  1351. }
  1352. case L2Item.SLOT_FEET:
  1353. setPaperdollItem(PAPERDOLL_FEET, item);
  1354. break;
  1355. case L2Item.SLOT_GLOVES:
  1356. setPaperdollItem(PAPERDOLL_GLOVES, item);
  1357. break;
  1358. case L2Item.SLOT_HEAD:
  1359. setPaperdollItem(PAPERDOLL_HEAD, item);
  1360. break;
  1361. case L2Item.SLOT_HAIR:
  1362. L2ItemInstance hair = getPaperdollItem(PAPERDOLL_HAIR);
  1363. if ((hair != null) && (hair.getItem().getBodyPart() == L2Item.SLOT_HAIRALL))
  1364. {
  1365. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1366. }
  1367. else
  1368. {
  1369. setPaperdollItem(PAPERDOLL_HAIR, null);
  1370. }
  1371. setPaperdollItem(PAPERDOLL_HAIR, item);
  1372. break;
  1373. case L2Item.SLOT_HAIR2:
  1374. L2ItemInstance hair2 = getPaperdollItem(PAPERDOLL_HAIR);
  1375. if ((hair2 != null) && (hair2.getItem().getBodyPart() == L2Item.SLOT_HAIRALL))
  1376. {
  1377. setPaperdollItem(PAPERDOLL_HAIR, null);
  1378. }
  1379. else
  1380. {
  1381. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1382. }
  1383. setPaperdollItem(PAPERDOLL_HAIR2, item);
  1384. break;
  1385. case L2Item.SLOT_HAIRALL:
  1386. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1387. setPaperdollItem(PAPERDOLL_HAIR, item);
  1388. break;
  1389. case L2Item.SLOT_UNDERWEAR:
  1390. setPaperdollItem(PAPERDOLL_UNDER, item);
  1391. break;
  1392. case L2Item.SLOT_BACK:
  1393. setPaperdollItem(PAPERDOLL_CLOAK, item);
  1394. break;
  1395. case L2Item.SLOT_L_BRACELET:
  1396. setPaperdollItem(PAPERDOLL_LBRACELET, item);
  1397. break;
  1398. case L2Item.SLOT_R_BRACELET:
  1399. setPaperdollItem(PAPERDOLL_RBRACELET, item);
  1400. break;
  1401. case L2Item.SLOT_DECO:
  1402. equipTalisman(item);
  1403. break;
  1404. case L2Item.SLOT_BELT:
  1405. setPaperdollItem(PAPERDOLL_BELT, item);
  1406. break;
  1407. case L2Item.SLOT_ALLDRESS:
  1408. // formal dress
  1409. setPaperdollItem(PAPERDOLL_LEGS, null);
  1410. setPaperdollItem(PAPERDOLL_LHAND, null);
  1411. setPaperdollItem(PAPERDOLL_RHAND, null);
  1412. setPaperdollItem(PAPERDOLL_RHAND, null);
  1413. setPaperdollItem(PAPERDOLL_LHAND, null);
  1414. setPaperdollItem(PAPERDOLL_HEAD, null);
  1415. setPaperdollItem(PAPERDOLL_FEET, null);
  1416. setPaperdollItem(PAPERDOLL_GLOVES, null);
  1417. setPaperdollItem(PAPERDOLL_CHEST, item);
  1418. break;
  1419. default:
  1420. _log.warning("Unknown body slot " + targetSlot + " for Item ID:" + item.getId());
  1421. }
  1422. }
  1423. /**
  1424. * Refresh the weight of equipment loaded
  1425. */
  1426. @Override
  1427. protected void refreshWeight()
  1428. {
  1429. long weight = 0;
  1430. for (L2ItemInstance item : _items)
  1431. {
  1432. if ((item != null) && (item.getItem() != null))
  1433. {
  1434. weight += item.getItem().getWeight() * item.getCount();
  1435. }
  1436. }
  1437. _totalWeight = (int) Math.min(weight, Integer.MAX_VALUE);
  1438. }
  1439. /**
  1440. * @return the totalWeight.
  1441. */
  1442. public int getTotalWeight()
  1443. {
  1444. return _totalWeight;
  1445. }
  1446. /**
  1447. * Return the L2ItemInstance of the arrows needed for this bow.
  1448. * @param bow : L2Item designating the bow
  1449. * @return L2ItemInstance pointing out arrows for bow
  1450. */
  1451. public L2ItemInstance findArrowForBow(L2Item bow)
  1452. {
  1453. if (bow == null)
  1454. {
  1455. return null;
  1456. }
  1457. L2ItemInstance arrow = null;
  1458. for (L2ItemInstance item : getItems())
  1459. {
  1460. if (item.isEtcItem() && (item.getItem().getItemGradeSPlus() == bow.getItemGradeSPlus()) && (item.getEtcItem().getItemType() == EtcItemType.ARROW))
  1461. {
  1462. arrow = item;
  1463. break;
  1464. }
  1465. }
  1466. // Get the L2ItemInstance corresponding to the item identifier and return it
  1467. return arrow;
  1468. }
  1469. /**
  1470. * Return the L2ItemInstance of the bolts needed for this crossbow.
  1471. * @param crossbow : L2Item designating the crossbow
  1472. * @return L2ItemInstance pointing out bolts for crossbow
  1473. */
  1474. public L2ItemInstance findBoltForCrossBow(L2Item crossbow)
  1475. {
  1476. L2ItemInstance bolt = null;
  1477. for (L2ItemInstance item : getItems())
  1478. {
  1479. if (item.isEtcItem() && (item.getItem().getItemGradeSPlus() == crossbow.getItemGradeSPlus()) && (item.getEtcItem().getItemType() == EtcItemType.BOLT))
  1480. {
  1481. bolt = item;
  1482. break;
  1483. }
  1484. }
  1485. // Get the L2ItemInstance corresponding to the item identifier and return it
  1486. return bolt;
  1487. }
  1488. /**
  1489. * Get back items in inventory from database
  1490. */
  1491. @Override
  1492. public void restore()
  1493. {
  1494. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1495. PreparedStatement statement = con.prepareStatement("SELECT object_id, item_id, count, enchant_level, loc, loc_data, custom_type1, custom_type2, mana_left, time FROM items WHERE owner_id=? AND (loc=? OR loc=?) ORDER BY loc_data"))
  1496. {
  1497. statement.setInt(1, getOwnerId());
  1498. statement.setString(2, getBaseLocation().name());
  1499. statement.setString(3, getEquipLocation().name());
  1500. try (ResultSet inv = statement.executeQuery())
  1501. {
  1502. L2ItemInstance item;
  1503. while (inv.next())
  1504. {
  1505. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  1506. if (item == null)
  1507. {
  1508. continue;
  1509. }
  1510. if (getOwner() instanceof L2PcInstance)
  1511. {
  1512. L2PcInstance player = (L2PcInstance) getOwner();
  1513. if (!player.canOverrideCond(PcCondOverride.ITEM_CONDITIONS) && !player.isHero() && item.isHeroItem())
  1514. {
  1515. item.setItemLocation(ItemLocation.INVENTORY);
  1516. }
  1517. }
  1518. L2World.getInstance().storeObject(item);
  1519. // If stackable item is found in inventory just add to current quantity
  1520. if (item.isStackable() && (getItemByItemId(item.getId()) != null))
  1521. {
  1522. addItem("Restore", item, getOwner().getActingPlayer(), null);
  1523. }
  1524. else
  1525. {
  1526. addItem(item);
  1527. }
  1528. }
  1529. }
  1530. refreshWeight();
  1531. }
  1532. catch (Exception e)
  1533. {
  1534. _log.log(Level.WARNING, "Could not restore inventory: " + e.getMessage(), e);
  1535. }
  1536. }
  1537. public int getTalismanSlots()
  1538. {
  1539. return getOwner().getActingPlayer().getStat().getTalismanSlots();
  1540. }
  1541. private void equipTalisman(L2ItemInstance item)
  1542. {
  1543. if (getTalismanSlots() == 0)
  1544. {
  1545. return;
  1546. }
  1547. // find same (or incompatible) talisman type
  1548. for (int i = PAPERDOLL_DECO1; i < (PAPERDOLL_DECO1 + getTalismanSlots()); i++)
  1549. {
  1550. if (_paperdoll[i] != null)
  1551. {
  1552. if (getPaperdollItemId(i) == item.getId())
  1553. {
  1554. // overwtite
  1555. setPaperdollItem(i, item);
  1556. return;
  1557. }
  1558. }
  1559. }
  1560. // no free slot found - put on first free
  1561. for (int i = PAPERDOLL_DECO1; i < (PAPERDOLL_DECO1 + getTalismanSlots()); i++)
  1562. {
  1563. if (_paperdoll[i] == null)
  1564. {
  1565. setPaperdollItem(i, item);
  1566. return;
  1567. }
  1568. }
  1569. // no free slots - put on first
  1570. setPaperdollItem(PAPERDOLL_DECO1, item);
  1571. }
  1572. public boolean canEquipCloak()
  1573. {
  1574. return getOwner().getActingPlayer().getStat().canEquipCloak();
  1575. }
  1576. /**
  1577. * Re-notify to paperdoll listeners every equipped item
  1578. */
  1579. public void reloadEquippedItems()
  1580. {
  1581. int slot;
  1582. for (L2ItemInstance item : _paperdoll)
  1583. {
  1584. if (item == null)
  1585. {
  1586. continue;
  1587. }
  1588. slot = item.getLocationSlot();
  1589. for (PaperdollListener listener : _paperdollListeners)
  1590. {
  1591. if (listener == null)
  1592. {
  1593. continue;
  1594. }
  1595. listener.notifyUnequiped(slot, item, this);
  1596. listener.notifyEquiped(slot, item, this);
  1597. }
  1598. }
  1599. }
  1600. }