Inventory.java 49 KB

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