Inventory.java 44 KB

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