Inventory.java 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. if (item == null)
  579. return null;
  580. synchronized (item)
  581. {
  582. if (!_items.contains(item))
  583. {
  584. return null;
  585. }
  586. removeItem(item);
  587. item.setOwnerId(process, 0, actor, reference);
  588. item.setLocation(ItemLocation.VOID);
  589. item.setLastChange(L2ItemInstance.REMOVED);
  590. item.updateDatabase();
  591. refreshWeight();
  592. }
  593. return item;
  594. }
  595. /**
  596. * Drop item from inventory by using its <B>objectID</B> and updates database
  597. * @param process : String Identifier of process triggering this action
  598. * @param objectId : int Item Instance identifier of the item to be dropped
  599. * @param count : int Quantity of items to be dropped
  600. * @param actor : L2PcInstance Player requesting the item drop
  601. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  602. * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory
  603. */
  604. public L2ItemInstance dropItem(String process, int objectId, int count, L2PcInstance actor, L2Object reference)
  605. {
  606. L2ItemInstance item = getItemByObjectId(objectId);
  607. if (item == null) return null;
  608. // Adjust item quantity and create new instance to drop
  609. if (item.getCount() > count)
  610. {
  611. item.changeCount(process, -count, actor, reference);
  612. item.setLastChange(L2ItemInstance.MODIFIED);
  613. item.updateDatabase();
  614. item = ItemTable.getInstance().createItem(process, item.getItemId(), count, actor, reference);
  615. item.updateDatabase();
  616. refreshWeight();
  617. return item;
  618. }
  619. // Directly drop entire item
  620. else return dropItem(process, item, actor, reference);
  621. }
  622. /**
  623. * Adds item to inventory for further adjustments and Equip it if necessary (itemlocation defined)<BR><BR>
  624. *
  625. * @param item : L2ItemInstance to be added from inventory
  626. */
  627. @Override
  628. protected void addItem(L2ItemInstance item)
  629. {
  630. super.addItem(item);
  631. if (item.isEquipped())
  632. equipItem(item);
  633. }
  634. /**
  635. * Removes item from inventory for further adjustments.
  636. * @param item : L2ItemInstance to be removed from inventory
  637. */
  638. @Override
  639. protected boolean removeItem(L2ItemInstance item)
  640. {
  641. // Unequip item if equiped
  642. // if (item.isEquipped()) unEquipItemInSlotAndRecord(item.getEquipSlot());
  643. for (int i = 0; i < _paperdoll.length; i++)
  644. if (_paperdoll[i] == item) unEquipItemInSlot(i);
  645. return super.removeItem(item);
  646. }
  647. /**
  648. * Returns the item in the paperdoll slot
  649. * @return L2ItemInstance
  650. */
  651. public L2ItemInstance getPaperdollItem(int slot)
  652. {
  653. return _paperdoll[slot];
  654. }
  655. /**
  656. * Returns the item in the paperdoll L2Item slot
  657. * @param L2Item slot identifier
  658. * @return L2ItemInstance
  659. */
  660. public L2ItemInstance getPaperdollItemByL2ItemId(int slot)
  661. {
  662. switch (slot) {
  663. case 0x01:
  664. return _paperdoll[0];
  665. case 0x02:
  666. return _paperdoll[1];
  667. case 0x04:
  668. return _paperdoll[2];
  669. case 0x06:
  670. return _paperdoll[3];
  671. case 0x08:
  672. return _paperdoll[4];
  673. case 0x10:
  674. return _paperdoll[5];
  675. case 0x20:
  676. return _paperdoll[6];
  677. case 0x30:
  678. return _paperdoll[7];
  679. case 0x040:
  680. return _paperdoll[8];
  681. case 0x080:
  682. return _paperdoll[9];
  683. case 0x0100:
  684. return _paperdoll[10];
  685. case 0x0200:
  686. return _paperdoll[11];
  687. case 0x0400:
  688. return _paperdoll[12];
  689. case 0x0800:
  690. return _paperdoll[13];
  691. case 0x1000:
  692. return _paperdoll[14];
  693. case 0x2000:
  694. return _paperdoll[15];
  695. case 0x4000:
  696. return _paperdoll[16];
  697. case 0x8000:
  698. return _paperdoll[17];
  699. case 0x010000:
  700. return _paperdoll[18];
  701. case 0x020000:
  702. return _paperdoll[19];
  703. case 0x040000:
  704. return _paperdoll[20];
  705. case 0x080000:
  706. return _paperdoll[21];
  707. case 0x100000:
  708. return _paperdoll[22];
  709. case 0x200000:
  710. return _paperdoll[23];
  711. case 0x400000:
  712. return _paperdoll[24];
  713. }
  714. return null;
  715. }
  716. /**
  717. * Returns the ID of the item in the paperdol slot
  718. * @param slot : int designating the slot
  719. * @return int designating the ID of the item
  720. */
  721. public int getPaperdollItemId(int slot)
  722. {
  723. L2ItemInstance item = _paperdoll[slot];
  724. if (item != null)
  725. return item.getItemId();
  726. else if (slot == PAPERDOLL_HAIR)
  727. {
  728. item = _paperdoll[PAPERDOLL_HAIRALL];
  729. if (item != null)
  730. return item.getItemId();
  731. }
  732. return 0;
  733. }
  734. public int getPaperdollAugmentationId(int slot)
  735. {
  736. L2ItemInstance item = _paperdoll[slot];
  737. if (item != null)
  738. {
  739. if (item.getAugmentation() != null)
  740. {
  741. return item.getAugmentation().getAugmentationId();
  742. }
  743. else
  744. {
  745. return 0;
  746. }
  747. }
  748. return 0;
  749. }
  750. /**
  751. * Returns the objectID associated to the item in the paperdoll slot
  752. * @param slot : int pointing out the slot
  753. * @return int designating the objectID
  754. */
  755. public int getPaperdollObjectId(int slot)
  756. {
  757. L2ItemInstance item = _paperdoll[slot];
  758. if (item != null)
  759. return item.getObjectId();
  760. else if (slot == PAPERDOLL_HAIR)
  761. {
  762. item = _paperdoll[PAPERDOLL_HAIRALL];
  763. if (item != null)
  764. return item.getObjectId();
  765. }
  766. return 0;
  767. }
  768. /**
  769. * Adds new inventory's paperdoll listener
  770. * @param PaperdollListener pointing out the listener
  771. */
  772. public synchronized void addPaperdollListener(PaperdollListener listener)
  773. {
  774. if (Config.ASSERT) assert !_paperdollListeners.contains(listener);
  775. _paperdollListeners.add(listener);
  776. }
  777. /**
  778. * Removes a paperdoll listener
  779. * @param PaperdollListener pointing out the listener to be deleted
  780. */
  781. public synchronized void removePaperdollListener(PaperdollListener listener)
  782. {
  783. _paperdollListeners.remove(listener);
  784. }
  785. /**
  786. * Equips an item in the given slot of the paperdoll.
  787. * <U><I>Remark :</I></U> The item <B>HAS TO BE</B> already in the inventory
  788. * @param slot : int pointing out the slot of the paperdoll
  789. * @param item : L2ItemInstance pointing out the item to add in slot
  790. * @return L2ItemInstance designating the item placed in the slot before
  791. */
  792. public L2ItemInstance setPaperdollItem(int slot, L2ItemInstance item)
  793. {
  794. L2ItemInstance old = _paperdoll[slot];
  795. if (old != item)
  796. {
  797. if (old != null)
  798. {
  799. _paperdoll[slot] = null;
  800. // Put old item from paperdoll slot to base location
  801. old.setLocation(getBaseLocation());
  802. old.setLastChange(L2ItemInstance.MODIFIED);
  803. // Get the mask for paperdoll
  804. int mask = 0;
  805. for (int i=0; i < PAPERDOLL_LRHAND; i++)
  806. {
  807. L2ItemInstance pi = _paperdoll[i];
  808. if (pi != null)
  809. mask |= pi.getItem().getItemMask();
  810. }
  811. _wearedMask = mask;
  812. // Notify all paperdoll listener in order to unequip old item in slot
  813. for (PaperdollListener listener : _paperdollListeners)
  814. {
  815. if (listener == null) continue;
  816. listener.notifyUnequiped(slot, old);
  817. }
  818. old.updateDatabase();
  819. }
  820. // Add new item in slot of paperdoll
  821. if (item != null)
  822. {
  823. _paperdoll[slot] = item;
  824. item.setLocation(getEquipLocation(), slot);
  825. item.setLastChange(L2ItemInstance.MODIFIED);
  826. _wearedMask |= item.getItem().getItemMask();
  827. for (PaperdollListener listener : _paperdollListeners)
  828. listener.notifyEquiped(slot, item);
  829. item.updateDatabase();
  830. }
  831. }
  832. return old;
  833. }
  834. /**
  835. * Return the mask of weared item
  836. * @return int
  837. */
  838. public int getWearedMask()
  839. {
  840. return _wearedMask;
  841. }
  842. public int getSlotFromItem(L2ItemInstance item)
  843. {
  844. int slot = -1;
  845. int location = item.getLocationSlot();
  846. switch(location)
  847. {
  848. case PAPERDOLL_UNDER: slot = L2Item.SLOT_UNDERWEAR; break;
  849. case PAPERDOLL_LEAR: slot = L2Item.SLOT_L_EAR; break;
  850. case PAPERDOLL_REAR: slot = L2Item.SLOT_R_EAR; break;
  851. case PAPERDOLL_NECK: slot = L2Item.SLOT_NECK; break;
  852. case PAPERDOLL_RFINGER: slot = L2Item.SLOT_R_FINGER; break;
  853. case PAPERDOLL_LFINGER: slot = L2Item.SLOT_L_FINGER; break;
  854. case PAPERDOLL_HAIR: slot = L2Item.SLOT_HAIR; break;
  855. case PAPERDOLL_HAIR2: slot = L2Item.SLOT_HAIR2; break;
  856. case PAPERDOLL_HAIRALL: slot = L2Item.SLOT_HAIRALL; break;
  857. case PAPERDOLL_HEAD: slot = L2Item.SLOT_HEAD; break;
  858. case PAPERDOLL_RHAND: slot = L2Item.SLOT_R_HAND; break;
  859. case PAPERDOLL_LHAND: slot = L2Item.SLOT_L_HAND; break;
  860. case PAPERDOLL_GLOVES: slot = L2Item.SLOT_GLOVES; break;
  861. case PAPERDOLL_CHEST: slot = item.getItem().getBodyPart(); break;// fall through
  862. case PAPERDOLL_LEGS: slot = L2Item.SLOT_LEGS; break;
  863. case PAPERDOLL_BACK: slot = L2Item.SLOT_BACK; break;
  864. case PAPERDOLL_FEET: slot = L2Item.SLOT_FEET; break;
  865. case PAPERDOLL_LRHAND: slot = L2Item.SLOT_LR_HAND; break;
  866. case PAPERDOLL_LBRACELET: slot = L2Item.SLOT_L_BRACELET; break;
  867. case PAPERDOLL_RBRACELET: slot = L2Item.SLOT_R_BRACELET; break;
  868. }
  869. return slot;
  870. }
  871. /**
  872. * Unequips item in body slot and returns alterations.
  873. * @param slot : int designating the slot of the paperdoll
  874. * @return L2ItemInstance[] : list of changes
  875. */
  876. public synchronized L2ItemInstance[] unEquipItemInBodySlotAndRecord(int slot)
  877. {
  878. Inventory.ChangeRecorder recorder = newRecorder();
  879. try
  880. {
  881. unEquipItemInBodySlot(slot);
  882. if (getOwner() instanceof L2PcInstance)
  883. ((L2PcInstance)getOwner()).refreshExpertisePenalty();
  884. } finally {
  885. removePaperdollListener(recorder);
  886. }
  887. return recorder.getChangedItems();
  888. }
  889. /**
  890. * Sets item in slot of the paperdoll to null value
  891. * @param pdollSlot : int designating the slot
  892. * @return L2ItemInstance designating the item in slot before change
  893. */
  894. public synchronized L2ItemInstance unEquipItemInSlot(int pdollSlot) {
  895. return setPaperdollItem(pdollSlot, null);
  896. }
  897. /**
  898. * Unepquips item in slot and returns alterations
  899. * @param slot : int designating the slot
  900. * @return L2ItemInstance[] : list of items altered
  901. */
  902. public synchronized L2ItemInstance[] unEquipItemInSlotAndRecord(int slot)
  903. {
  904. Inventory.ChangeRecorder recorder = newRecorder();
  905. try
  906. {
  907. unEquipItemInSlot(slot);
  908. if (getOwner() instanceof L2PcInstance)
  909. ((L2PcInstance)getOwner()).refreshExpertisePenalty();
  910. } finally {
  911. removePaperdollListener(recorder);
  912. }
  913. return recorder.getChangedItems();
  914. }
  915. /**
  916. * Unequips item in slot (i.e. equips with default value)
  917. * @param slot : int designating the slot
  918. */
  919. private void unEquipItemInBodySlot(int slot)
  920. {
  921. if (Config.DEBUG) _log.fine("--- unequip body slot:" + slot);
  922. int pdollSlot = -1;
  923. switch (slot)
  924. {
  925. case L2Item.SLOT_L_EAR: pdollSlot = PAPERDOLL_LEAR; break;
  926. case L2Item.SLOT_R_EAR: pdollSlot = PAPERDOLL_REAR; break;
  927. case L2Item.SLOT_NECK: pdollSlot = PAPERDOLL_NECK; break;
  928. case L2Item.SLOT_R_FINGER: pdollSlot = PAPERDOLL_RFINGER; break;
  929. case L2Item.SLOT_L_FINGER: pdollSlot = PAPERDOLL_LFINGER; break;
  930. case L2Item.SLOT_HAIR: pdollSlot = PAPERDOLL_HAIR; break;
  931. case L2Item.SLOT_HAIR2: pdollSlot = PAPERDOLL_HAIR2; break;
  932. case L2Item.SLOT_HAIRALL:
  933. setPaperdollItem(PAPERDOLL_HAIR, null);
  934. setPaperdollItem(PAPERDOLL_HAIR2, null);// this should be the same as in DHAIR
  935. pdollSlot = PAPERDOLL_HAIRALL;
  936. break;
  937. case L2Item.SLOT_HEAD: pdollSlot = PAPERDOLL_HEAD; break;
  938. case L2Item.SLOT_R_HAND: pdollSlot = PAPERDOLL_RHAND; break;
  939. case L2Item.SLOT_L_HAND: pdollSlot = PAPERDOLL_LHAND; break;
  940. case L2Item.SLOT_GLOVES: pdollSlot = PAPERDOLL_GLOVES; break;
  941. case L2Item.SLOT_CHEST: // fall through
  942. case L2Item.SLOT_FULL_ARMOR:pdollSlot = PAPERDOLL_CHEST; break;
  943. case L2Item.SLOT_LEGS: pdollSlot = PAPERDOLL_LEGS; break;
  944. case L2Item.SLOT_BACK: pdollSlot = PAPERDOLL_BACK; break;
  945. case L2Item.SLOT_FEET: pdollSlot = PAPERDOLL_FEET; break;
  946. case L2Item.SLOT_UNDERWEAR: pdollSlot = PAPERDOLL_UNDER;break;
  947. case L2Item.SLOT_LR_HAND:
  948. setPaperdollItem(PAPERDOLL_LHAND, null);
  949. setPaperdollItem(PAPERDOLL_RHAND, null);// this should be the same as in LRHAND
  950. pdollSlot = PAPERDOLL_LRHAND;
  951. break;
  952. case L2Item.SLOT_L_BRACELET: pdollSlot = PAPERDOLL_LBRACELET; break;
  953. case L2Item.SLOT_R_BRACELET: pdollSlot = PAPERDOLL_RBRACELET; break;
  954. }
  955. if (pdollSlot >= 0)
  956. setPaperdollItem(pdollSlot, null);
  957. }
  958. /**
  959. * Equips item and returns list of alterations
  960. * @param item : L2ItemInstance corresponding to the item
  961. * @return L2ItemInstance[] : list of alterations
  962. */
  963. public L2ItemInstance[] equipItemAndRecord(L2ItemInstance item)
  964. {
  965. Inventory.ChangeRecorder recorder = newRecorder();
  966. try {
  967. equipItem(item);
  968. }
  969. finally {
  970. removePaperdollListener(recorder);
  971. }
  972. return recorder.getChangedItems();
  973. }
  974. /**
  975. * Equips item in slot of paperdoll.
  976. * @param item : L2ItemInstance designating the item and slot used.
  977. */
  978. public synchronized void equipItem(L2ItemInstance item)
  979. {
  980. if((getOwner() instanceof L2PcInstance) && ((L2PcInstance)getOwner()).getPrivateStoreType() != 0)
  981. return;
  982. if(getOwner() instanceof L2PcInstance)
  983. {
  984. L2PcInstance player = (L2PcInstance)getOwner();
  985. if(player.getPkKills() > 0 && item.getItemId() >= 7816 && item.getItemId() <= 7831)
  986. {
  987. player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_UNABLE_TO_EQUIP_THIS_ITEM_WHEN_YOUR_PK_COUNT_IS_GREATER_THAN_OR_EQUAL_TO_ONE));
  988. return;
  989. }
  990. if(!player.isGM())
  991. if (!player.isHero())
  992. {
  993. if (item.isHeroItem())
  994. return;
  995. }
  996. }
  997. int targetSlot = item.getItem().getBodyPart();
  998. switch (targetSlot)
  999. {
  1000. case L2Item.SLOT_LR_HAND:
  1001. {
  1002. if (setPaperdollItem(PAPERDOLL_LHAND, null) != null)
  1003. {
  1004. // exchange 2h for 2h
  1005. setPaperdollItem(PAPERDOLL_RHAND, null);
  1006. setPaperdollItem(PAPERDOLL_LHAND, null);
  1007. }
  1008. else
  1009. {
  1010. setPaperdollItem(PAPERDOLL_RHAND, null);
  1011. }
  1012. setPaperdollItem(PAPERDOLL_RHAND, item);
  1013. setPaperdollItem(PAPERDOLL_LRHAND, item);
  1014. break;
  1015. }
  1016. case L2Item.SLOT_L_HAND:
  1017. {
  1018. if (!(item.getItem() instanceof L2EtcItem) || item.getItem().getItemType() != L2EtcItemType.ARROW)
  1019. {
  1020. L2ItemInstance old1 = setPaperdollItem(PAPERDOLL_LRHAND, null);
  1021. if (old1 != null)
  1022. {
  1023. setPaperdollItem(PAPERDOLL_RHAND, null);
  1024. }
  1025. }
  1026. setPaperdollItem(PAPERDOLL_LHAND, null);
  1027. setPaperdollItem(PAPERDOLL_LHAND, item);
  1028. break;
  1029. }
  1030. case L2Item.SLOT_R_HAND:
  1031. {
  1032. if (_paperdoll[PAPERDOLL_LRHAND] != null)
  1033. {
  1034. setPaperdollItem(PAPERDOLL_LRHAND, null);
  1035. setPaperdollItem(PAPERDOLL_LHAND, null);
  1036. setPaperdollItem(PAPERDOLL_RHAND, null);
  1037. }
  1038. else
  1039. {
  1040. setPaperdollItem(PAPERDOLL_RHAND, null);
  1041. }
  1042. setPaperdollItem(PAPERDOLL_RHAND, item);
  1043. break;
  1044. }
  1045. case L2Item.SLOT_L_EAR:
  1046. case L2Item.SLOT_R_EAR:
  1047. case L2Item.SLOT_L_EAR | L2Item.SLOT_R_EAR:
  1048. {
  1049. if (_paperdoll[PAPERDOLL_LEAR] == null)
  1050. {
  1051. setPaperdollItem(PAPERDOLL_LEAR, item);
  1052. }
  1053. else if (_paperdoll[PAPERDOLL_REAR] == null)
  1054. {
  1055. setPaperdollItem(PAPERDOLL_REAR, item);
  1056. }
  1057. else
  1058. {
  1059. setPaperdollItem(PAPERDOLL_LEAR, null);
  1060. setPaperdollItem(PAPERDOLL_LEAR, item);
  1061. }
  1062. break;
  1063. }
  1064. case L2Item.SLOT_L_FINGER:
  1065. case L2Item.SLOT_R_FINGER:
  1066. case L2Item.SLOT_L_FINGER | L2Item.SLOT_R_FINGER:
  1067. {
  1068. if (_paperdoll[PAPERDOLL_LFINGER] == null)
  1069. {
  1070. setPaperdollItem(PAPERDOLL_LFINGER, item);
  1071. }
  1072. else if (_paperdoll[PAPERDOLL_RFINGER] == null)
  1073. {
  1074. setPaperdollItem(PAPERDOLL_RFINGER, item);
  1075. }
  1076. else
  1077. {
  1078. setPaperdollItem(PAPERDOLL_LFINGER, null);
  1079. setPaperdollItem(PAPERDOLL_LFINGER, item);
  1080. }
  1081. break;
  1082. }
  1083. case L2Item.SLOT_NECK:
  1084. setPaperdollItem(PAPERDOLL_NECK, item);
  1085. break;
  1086. case L2Item.SLOT_FULL_ARMOR:
  1087. setPaperdollItem(PAPERDOLL_CHEST, null);
  1088. setPaperdollItem(PAPERDOLL_LEGS, null);
  1089. setPaperdollItem(PAPERDOLL_CHEST, item);
  1090. break;
  1091. case L2Item.SLOT_CHEST:
  1092. setPaperdollItem(PAPERDOLL_CHEST, item);
  1093. break;
  1094. case L2Item.SLOT_LEGS:
  1095. {
  1096. // handle full armor
  1097. L2ItemInstance chest = getPaperdollItem(PAPERDOLL_CHEST);
  1098. if (chest != null && chest.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR)
  1099. {
  1100. setPaperdollItem(PAPERDOLL_CHEST, null);
  1101. }
  1102. setPaperdollItem(PAPERDOLL_LEGS, null);
  1103. setPaperdollItem(PAPERDOLL_LEGS, item);
  1104. break;
  1105. }
  1106. case L2Item.SLOT_FEET:
  1107. setPaperdollItem(PAPERDOLL_FEET, item);
  1108. break;
  1109. case L2Item.SLOT_GLOVES:
  1110. setPaperdollItem(PAPERDOLL_GLOVES, item);
  1111. break;
  1112. case L2Item.SLOT_HEAD:
  1113. setPaperdollItem(PAPERDOLL_HEAD, item);
  1114. break;
  1115. case L2Item.SLOT_HAIR:
  1116. if (setPaperdollItem(PAPERDOLL_HAIRALL, null) != null)
  1117. {
  1118. setPaperdollItem(PAPERDOLL_HAIRALL, null);
  1119. setPaperdollItem(PAPERDOLL_HAIR, null);
  1120. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1121. }
  1122. else
  1123. setPaperdollItem(PAPERDOLL_HAIR, null);
  1124. setPaperdollItem(PAPERDOLL_HAIR, item);
  1125. break;
  1126. case L2Item.SLOT_HAIR2:
  1127. if (setPaperdollItem(PAPERDOLL_HAIRALL, null) != null)
  1128. {
  1129. setPaperdollItem(PAPERDOLL_HAIRALL, null);
  1130. setPaperdollItem(PAPERDOLL_HAIR, null);
  1131. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1132. }
  1133. else
  1134. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1135. setPaperdollItem(PAPERDOLL_HAIR2, item);
  1136. break;
  1137. case L2Item.SLOT_HAIRALL:
  1138. if (setPaperdollItem(PAPERDOLL_HAIR, null) != null)
  1139. {
  1140. setPaperdollItem(PAPERDOLL_HAIR, null);
  1141. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1142. }
  1143. else
  1144. {
  1145. setPaperdollItem(PAPERDOLL_HAIR2, null);
  1146. }
  1147. setPaperdollItem(PAPERDOLL_HAIRALL, item);
  1148. break;
  1149. case L2Item.SLOT_UNDERWEAR:
  1150. setPaperdollItem(PAPERDOLL_UNDER, item);
  1151. break;
  1152. case L2Item.SLOT_BACK:
  1153. setPaperdollItem(PAPERDOLL_BACK, item);
  1154. break;
  1155. case L2Item.SLOT_L_BRACELET:
  1156. setPaperdollItem(PAPERDOLL_LBRACELET, item);
  1157. break;
  1158. case L2Item.SLOT_R_BRACELET:
  1159. setPaperdollItem(PAPERDOLL_RBRACELET, item);
  1160. break;
  1161. default:
  1162. _log.warning("unknown body slot:" + targetSlot);
  1163. }
  1164. }
  1165. /**
  1166. * Refresh the weight of equipment loaded
  1167. */
  1168. @Override
  1169. protected void refreshWeight()
  1170. {
  1171. int weight = 0;
  1172. for (L2ItemInstance item : _items)
  1173. {
  1174. if (item != null && item.getItem() != null)
  1175. weight += item.getItem().getWeight() * item.getCount();
  1176. }
  1177. _totalWeight = weight;
  1178. }
  1179. /**
  1180. * Returns the totalWeight.
  1181. * @return int
  1182. */
  1183. public int getTotalWeight()
  1184. {
  1185. return _totalWeight;
  1186. }
  1187. /**
  1188. * Return the L2ItemInstance of the arrows needed for this bow.<BR><BR>
  1189. * @param bow : L2Item designating the bow
  1190. * @return L2ItemInstance pointing out arrows for bow
  1191. */
  1192. public L2ItemInstance findArrowForBow(L2Item bow)
  1193. {
  1194. int arrowsId = 0;
  1195. switch (bow.getCrystalType())
  1196. {
  1197. default: // broken weapon.csv ??
  1198. case L2Item.CRYSTAL_NONE: arrowsId = 17; break; // Wooden arrow
  1199. case L2Item.CRYSTAL_D: arrowsId = 1341; break; // Bone arrow
  1200. case L2Item.CRYSTAL_C: arrowsId = 1342; break; // Fine steel arrow
  1201. case L2Item.CRYSTAL_B: arrowsId = 1343; break; // Silver arrow
  1202. case L2Item.CRYSTAL_A: arrowsId = 1344; break; // Mithril arrow
  1203. case L2Item.CRYSTAL_S:
  1204. case L2Item.CRYSTAL_S80: arrowsId = 1345; break; // Shining arrow
  1205. }
  1206. // Get the L2ItemInstance corresponding to the item identifier and return it
  1207. return getItemByItemId(arrowsId);
  1208. }
  1209. /**
  1210. * Return the L2ItemInstance of the bolts needed for this crossbow.<BR><BR>
  1211. * @param crossbow : L2Item designating the crossbow
  1212. * @return L2ItemInstance pointing out bolts for crossbow
  1213. */
  1214. public L2ItemInstance findBoltForCrossBow(L2Item crossbow)
  1215. {
  1216. int boltsId = 0;
  1217. switch (crossbow.getCrystalType())
  1218. {
  1219. default:
  1220. case L2Item.CRYSTAL_NONE: boltsId = 9632; break; // Wooden Bolt
  1221. case L2Item.CRYSTAL_D: boltsId = 9633; break; // Bone Bolt
  1222. case L2Item.CRYSTAL_C: boltsId = 9634; break; // Steel Bolt
  1223. case L2Item.CRYSTAL_B: boltsId = 9635; break; // Silver Bolt
  1224. case L2Item.CRYSTAL_A: boltsId = 9636; break; // Mithril Bolt
  1225. case L2Item.CRYSTAL_S:
  1226. case L2Item.CRYSTAL_S80: boltsId = 9637; break; // Shining Bolt
  1227. }
  1228. // Get the L2ItemInstance corresponding to the item identifier and return it
  1229. return getItemByItemId(boltsId);
  1230. }
  1231. /**
  1232. * Get back items in inventory from database
  1233. */
  1234. @Override
  1235. public void restore()
  1236. {
  1237. java.sql.Connection con = null;
  1238. try
  1239. {
  1240. con = L2DatabaseFactory.getInstance().getConnection();
  1241. PreparedStatement statement = con.prepareStatement(
  1242. "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=?) ");
  1243. statement.setInt(1, getOwner().getObjectId());
  1244. statement.setString(2, getBaseLocation().name());
  1245. statement.setString(3, getEquipLocation().name());
  1246. ResultSet inv = statement.executeQuery();
  1247. L2ItemInstance item;
  1248. while (inv.next())
  1249. {
  1250. item = L2ItemInstance.restoreFromDb(getOwner().getObjectId(), inv);
  1251. if (item == null) continue;
  1252. if(getOwner() instanceof L2PcInstance)
  1253. {
  1254. L2PcInstance player = (L2PcInstance)getOwner();
  1255. if(player.getPkKills() > 0 && item.getItemId() >= 7816 && item.getItemId() <= 7831)
  1256. {
  1257. item.setLocation(ItemLocation.INVENTORY);
  1258. }
  1259. if(!player.isGM())
  1260. if (!player.isHero())
  1261. {
  1262. if (item.isHeroItem())
  1263. item.setLocation(ItemLocation.INVENTORY);
  1264. }
  1265. }
  1266. L2World.getInstance().storeObject(item);
  1267. // If stackable item is found in inventory just add to current quantity
  1268. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  1269. addItem("Restore", item, null, getOwner());
  1270. else addItem(item);
  1271. }
  1272. inv.close();
  1273. statement.close();
  1274. refreshWeight();
  1275. }
  1276. catch (Exception e)
  1277. {
  1278. _log.warning("Could not restore inventory : " + e);
  1279. }
  1280. finally
  1281. {
  1282. try { con.close(); } catch (Exception e) {}
  1283. }
  1284. }
  1285. /**
  1286. * Re-notify to paperdoll listeners every equipped item
  1287. */
  1288. public void reloadEquippedItems()
  1289. {
  1290. L2ItemInstance item;
  1291. int slot;
  1292. for (int i = 0; i < _paperdoll.length; i++)
  1293. {
  1294. item = _paperdoll[i];
  1295. if (item == null) continue;
  1296. slot = item.getLocationSlot();
  1297. for (PaperdollListener listener : _paperdollListeners)
  1298. {
  1299. if (listener == null) continue;
  1300. listener.notifyUnequiped(slot, item);
  1301. listener.notifyEquiped(slot, item);
  1302. }
  1303. }
  1304. }
  1305. }