Inventory.java 47 KB

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