Inventory.java 44 KB

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