Inventory.java 47 KB

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