2
0

L2Item.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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.templates.item;
  16. import java.util.List;
  17. import javolution.util.FastList;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.model.L2Character;
  20. import net.sf.l2j.gameserver.model.L2Effect;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Skill;
  23. import net.sf.l2j.gameserver.skills.Env;
  24. import net.sf.l2j.gameserver.skills.effects.EffectTemplate;
  25. import net.sf.l2j.gameserver.skills.funcs.Func;
  26. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  27. import net.sf.l2j.gameserver.templates.StatsSet;
  28. /**
  29. * This class contains all informations concerning the item (weapon, armor, etc).<BR>
  30. * Mother class of :
  31. * <LI>L2Armor</LI>
  32. * <LI>L2EtcItem</LI>
  33. * <LI>L2Weapon</LI>
  34. * @version $Revision: 1.7.2.2.2.5 $ $Date: 2005/04/06 18:25:18 $
  35. */
  36. public abstract class L2Item
  37. {
  38. public static final int TYPE1_WEAPON_RING_EARRING_NECKLACE = 0;
  39. public static final int TYPE1_SHIELD_ARMOR = 1;
  40. public static final int TYPE1_ITEM_QUESTITEM_ADENA = 4;
  41. public static final int TYPE2_WEAPON = 0;
  42. public static final int TYPE2_SHIELD_ARMOR = 1;
  43. public static final int TYPE2_ACCESSORY = 2;
  44. public static final int TYPE2_QUEST = 3;
  45. public static final int TYPE2_MONEY = 4;
  46. public static final int TYPE2_OTHER = 5;
  47. public static final int TYPE2_PET_WOLF = 6;
  48. public static final int TYPE2_PET_HATCHLING = 7;
  49. public static final int TYPE2_PET_STRIDER = 8;
  50. public static final int TYPE2_PET_BABY = 9;
  51. public static final int TYPE2_PET_GREATWOLF = 10;
  52. public static final int SLOT_NONE = 0x0000;
  53. public static final int SLOT_UNDERWEAR = 0x0001;
  54. public static final int SLOT_R_EAR = 0x0002;
  55. public static final int SLOT_L_EAR = 0x0004;
  56. public static final int SLOT_LR_EAR = 0x00006;
  57. public static final int SLOT_NECK = 0x0008;
  58. public static final int SLOT_R_FINGER = 0x0010;
  59. public static final int SLOT_L_FINGER = 0x0020;
  60. public static final int SLOT_LR_FINGER = 0x0030;
  61. public static final int SLOT_HEAD = 0x0040;
  62. public static final int SLOT_R_HAND = 0x0080;
  63. public static final int SLOT_L_HAND = 0x0100;
  64. public static final int SLOT_GLOVES = 0x0200;
  65. public static final int SLOT_CHEST = 0x0400;
  66. public static final int SLOT_LEGS = 0x0800;
  67. public static final int SLOT_FEET = 0x1000;
  68. public static final int SLOT_BACK = 0x2000;
  69. public static final int SLOT_LR_HAND = 0x4000;
  70. public static final int SLOT_FULL_ARMOR = 0x8000;
  71. public static final int SLOT_HAIR = 0x010000;
  72. public static final int SLOT_ALLDRESS = 0x020000;
  73. public static final int SLOT_HAIR2 = 0x040000;
  74. public static final int SLOT_HAIRALL = 0x080000;
  75. public static final int SLOT_R_BRACELET = 0x100000;
  76. public static final int SLOT_L_BRACELET = 0x200000;
  77. public static final int SLOT_DECO = 0x400000;
  78. public static final int SLOT_WOLF = -100;
  79. public static final int SLOT_HATCHLING = -101;
  80. public static final int SLOT_STRIDER = -102;
  81. public static final int SLOT_BABYPET = -103;
  82. public static final int SLOT_GREATWOLF = -104;
  83. public static final int MATERIAL_STEEL = 0x00; // ??
  84. public static final int MATERIAL_FINE_STEEL = 0x01; // ??
  85. public static final int MATERIAL_BLOOD_STEEL = 0x02; // ??
  86. public static final int MATERIAL_BRONZE = 0x03; // ??
  87. public static final int MATERIAL_SILVER = 0x04; // ??
  88. public static final int MATERIAL_GOLD = 0x05; // ??
  89. public static final int MATERIAL_MITHRIL = 0x06; // ??
  90. public static final int MATERIAL_ORIHARUKON = 0x07; // ??
  91. public static final int MATERIAL_PAPER = 0x08; // ??
  92. public static final int MATERIAL_WOOD = 0x09; // ??
  93. public static final int MATERIAL_CLOTH = 0x0a; // ??
  94. public static final int MATERIAL_LEATHER = 0x0b; // ??
  95. public static final int MATERIAL_BONE = 0x0c; // ??
  96. public static final int MATERIAL_HORN = 0x0d; // ??
  97. public static final int MATERIAL_DAMASCUS = 0x0e; // ??
  98. public static final int MATERIAL_ADAMANTAITE = 0x0f; // ??
  99. public static final int MATERIAL_CHRYSOLITE = 0x10; // ??
  100. public static final int MATERIAL_CRYSTAL = 0x11; // ??
  101. public static final int MATERIAL_LIQUID = 0x12; // ??
  102. public static final int MATERIAL_SCALE_OF_DRAGON = 0x13; // ??
  103. public static final int MATERIAL_DYESTUFF = 0x14; // ??
  104. public static final int MATERIAL_COBWEB = 0x15; // ??
  105. public static final int MATERIAL_SEED = 0x15; // ??
  106. public static final int CRYSTAL_NONE = 0x00; // ??
  107. public static final int CRYSTAL_D = 0x01; // ??
  108. public static final int CRYSTAL_C = 0x02; // ??
  109. public static final int CRYSTAL_B = 0x03; // ??
  110. public static final int CRYSTAL_A = 0x04; // ??
  111. public static final int CRYSTAL_S = 0x05; // ??
  112. public static final int CRYSTAL_S80 = 0x06; // ??
  113. private static final int[] crystalItemId =
  114. {
  115. 0, 1458, 1459, 1460, 1461, 1462, 1462
  116. };
  117. private static final int[] crystalEnchantBonusArmor =
  118. {
  119. 0, 11, 6, 11, 19, 25, 25
  120. };
  121. private static final int[] crystalEnchantBonusWeapon =
  122. {
  123. 0, 90, 45, 67, 144, 250, 250
  124. };
  125. private final int _itemId;
  126. private final String _name;
  127. private final int _type1; // needed for item list (inventory)
  128. private final int _type2; // different lists for armor, weapon, etc
  129. private final int _weight;
  130. private final boolean _crystallizable;
  131. private final boolean _stackable;
  132. private final int _materialType;
  133. private final int _crystalType; // default to none-grade
  134. private final int _duration;
  135. private final int _bodyPart;
  136. private final int _referencePrice;
  137. private final int _crystalCount;
  138. private final boolean _sellable;
  139. private final boolean _dropable;
  140. private final boolean _destroyable;
  141. private final boolean _tradeable;
  142. @SuppressWarnings("unchecked")
  143. protected final Enum _type;
  144. protected FuncTemplate[] _funcTemplates;
  145. protected EffectTemplate[] _effectTemplates;
  146. protected L2Skill[] _skills;
  147. private static final Func[] _emptyFunctionSet = new Func[0];
  148. protected static final L2Effect[] _emptyEffectSet = new L2Effect[0];
  149. /**
  150. * Constructor of the L2Item that fill class variables.<BR><BR>
  151. * <U><I>Variables filled :</I></U><BR>
  152. * <LI>type</LI>
  153. * <LI>_itemId</LI>
  154. * <LI>_name</LI>
  155. * <LI>_type1 & _type2</LI>
  156. * <LI>_weight</LI>
  157. * <LI>_crystallizable</LI>
  158. * <LI>_stackable</LI>
  159. * <LI>_materialType & _crystalType & _crystlaCount</LI>
  160. * <LI>_duration</LI>
  161. * <LI>_bodypart</LI>
  162. * <LI>_referencePrice</LI>
  163. * <LI>_sellable</LI>
  164. * @param type : Enum designating the type of the item
  165. * @param set : StatsSet corresponding to a set of couples (key,value) for description of the item
  166. */
  167. protected L2Item(Enum<?> type, StatsSet set)
  168. {
  169. _type = type;
  170. _itemId = set.getInteger("item_id");
  171. _name = set.getString("name");
  172. _type1 = set.getInteger("type1"); // needed for item list (inventory)
  173. _type2 = set.getInteger("type2"); // different lists for armor, weapon, etc
  174. _weight = set.getInteger("weight");
  175. _crystallizable = set.getBool("crystallizable");
  176. _stackable = set.getBool("stackable", false);
  177. _materialType = set.getInteger("material");
  178. _crystalType = set.getInteger("crystal_type", CRYSTAL_NONE); // default to none-grade
  179. _duration = set.getInteger("duration");
  180. _bodyPart = set.getInteger("bodypart");
  181. _referencePrice = set.getInteger("price");
  182. _crystalCount = set.getInteger("crystal_count", 0);
  183. _sellable = set.getBool("sellable", true);
  184. _dropable = set.getBool("dropable", true);
  185. _destroyable = set.getBool("destroyable", true);
  186. _tradeable = set.getBool("tradeable", true);
  187. }
  188. /**
  189. * Returns the itemType.
  190. * @return Enum
  191. */
  192. @SuppressWarnings("unchecked")
  193. public Enum getItemType()
  194. {
  195. return _type;
  196. }
  197. /**
  198. * Returns the duration of the item
  199. * @return int
  200. */
  201. public final int getDuration()
  202. {
  203. return _duration;
  204. }
  205. /**
  206. * Returns the ID of the iden
  207. * @return int
  208. */
  209. public final int getItemId()
  210. {
  211. return _itemId;
  212. }
  213. public abstract int getItemMask();
  214. /**
  215. * Return the type of material of the item
  216. * @return int
  217. */
  218. public final int getMaterialType()
  219. {
  220. return _materialType;
  221. }
  222. /**
  223. * Returns the type 2 of the item
  224. * @return int
  225. */
  226. public final int getType2()
  227. {
  228. return _type2;
  229. }
  230. /**
  231. * Returns the weight of the item
  232. * @return int
  233. */
  234. public final int getWeight()
  235. {
  236. return _weight;
  237. }
  238. /**
  239. * Returns if the item is crystallizable
  240. * @return boolean
  241. */
  242. public final boolean isCrystallizable()
  243. {
  244. return _crystallizable;
  245. }
  246. /**
  247. * Return the type of crystal if item is crystallizable
  248. * @return int
  249. */
  250. public final int getCrystalType()
  251. {
  252. return _crystalType;
  253. }
  254. /**
  255. * Return the type of crystal if item is crystallizable
  256. * @return int
  257. */
  258. public final int getCrystalItemId()
  259. {
  260. return crystalItemId[_crystalType];
  261. }
  262. /**
  263. * Returns the grade of the item.<BR><BR>
  264. * <U><I>Concept :</I></U><BR>
  265. * In fact, this fucntion returns the type of crystal of the item.
  266. * @return int
  267. */
  268. public final int getItemGrade()
  269. {
  270. return getCrystalType();
  271. }
  272. /**
  273. * Returns the quantity of crystals for crystallization
  274. * @return int
  275. */
  276. public final int getCrystalCount()
  277. {
  278. return _crystalCount;
  279. }
  280. /**
  281. * Returns the quantity of crystals for crystallization on specific enchant level
  282. * @return int
  283. */
  284. public final int getCrystalCount(int enchantLevel)
  285. {
  286. if (enchantLevel > 3)
  287. switch (_type2)
  288. {
  289. case TYPE2_SHIELD_ARMOR:
  290. case TYPE2_ACCESSORY:
  291. return _crystalCount + crystalEnchantBonusArmor[getCrystalType()] * (3 * enchantLevel - 6);
  292. case TYPE2_WEAPON:
  293. return _crystalCount + crystalEnchantBonusWeapon[getCrystalType()] * (2 * enchantLevel - 3);
  294. default:
  295. return _crystalCount;
  296. }
  297. else if (enchantLevel > 0)
  298. switch (_type2)
  299. {
  300. case TYPE2_SHIELD_ARMOR:
  301. case TYPE2_ACCESSORY:
  302. return _crystalCount + crystalEnchantBonusArmor[getCrystalType()] * enchantLevel;
  303. case TYPE2_WEAPON:
  304. return _crystalCount + crystalEnchantBonusWeapon[getCrystalType()] * enchantLevel;
  305. default:
  306. return _crystalCount;
  307. }
  308. else
  309. return _crystalCount;
  310. }
  311. /**
  312. * Returns the name of the item
  313. * @return String
  314. */
  315. public final String getName()
  316. {
  317. return _name;
  318. }
  319. /**
  320. * Return the part of the body used with the item.
  321. * @return int
  322. */
  323. public final int getBodyPart()
  324. {
  325. return _bodyPart;
  326. }
  327. /**
  328. * Returns the type 1 of the item
  329. * @return int
  330. */
  331. public final int getType1()
  332. {
  333. return _type1;
  334. }
  335. /**
  336. * Returns if the item is stackable
  337. * @return boolean
  338. */
  339. public final boolean isStackable()
  340. {
  341. return _stackable;
  342. }
  343. /**
  344. * Returns if the item is consumable
  345. * @return boolean
  346. */
  347. public boolean isConsumable()
  348. {
  349. return false;
  350. }
  351. public boolean isEquipable()
  352. {
  353. return this.getBodyPart() != 0 && !(this.getItemType() instanceof L2EtcItemType);
  354. }
  355. /**
  356. * Returns the price of reference of the item
  357. * @return int
  358. */
  359. public final int getReferencePrice()
  360. {
  361. return (isConsumable() ? (int) (_referencePrice * Config.RATE_CONSUMABLE_COST) : _referencePrice);
  362. }
  363. /**
  364. * Returns if the item can be sold
  365. * @return boolean
  366. */
  367. public final boolean isSellable()
  368. {
  369. return _sellable;
  370. }
  371. /**
  372. * Returns if the item can dropped
  373. * @return boolean
  374. */
  375. public final boolean isDropable()
  376. {
  377. return _dropable;
  378. }
  379. /**
  380. * Returns if the item can destroy
  381. * @return boolean
  382. */
  383. public final boolean isDestroyable()
  384. {
  385. return _destroyable;
  386. }
  387. /**
  388. * Returns if the item can add to trade
  389. * @return boolean
  390. */
  391. public final boolean isTradeable()
  392. {
  393. return _tradeable;
  394. }
  395. /**
  396. * Returns if item is for hatchling
  397. * @return boolean
  398. */
  399. public boolean isForHatchling()
  400. {
  401. return (_type2 == TYPE2_PET_HATCHLING);
  402. }
  403. /**
  404. * Returns if item is for strider
  405. * @return boolean
  406. */
  407. public boolean isForStrider()
  408. {
  409. return (_type2 == TYPE2_PET_STRIDER);
  410. }
  411. /**
  412. * Returns if item is for wolf
  413. * @return boolean
  414. */
  415. public boolean isForWolf()
  416. {
  417. return (_type2 == TYPE2_PET_WOLF);
  418. }
  419. /**
  420. * Returns if item is for Great wolf
  421. * @return boolean
  422. */
  423. public boolean isForGreatWolf()
  424. {
  425. return (_type2 == TYPE2_PET_GREATWOLF);
  426. }
  427. /**
  428. * Returns if item is for wolf
  429. * @return boolean
  430. */
  431. public boolean isForBabyPet()
  432. {
  433. return (_type2 == TYPE2_PET_BABY);
  434. }
  435. /**
  436. * Returns array of Func objects containing the list of functions used by the item
  437. * @param instance : L2ItemInstance pointing out the item
  438. * @param player : L2Character pointing out the player
  439. * @return Func[] : array of functions
  440. */
  441. public Func[] getStatFuncs(L2ItemInstance instance, L2Character player)
  442. {
  443. if (_funcTemplates == null)
  444. return _emptyFunctionSet;
  445. List<Func> funcs = new FastList<Func>();
  446. for (FuncTemplate t : _funcTemplates)
  447. {
  448. Env env = new Env();
  449. env.player = player;
  450. env.target = player;
  451. env.item = instance;
  452. Func f = t.getFunc(env, this); // skill is owner
  453. if (f != null)
  454. funcs.add(f);
  455. }
  456. if (funcs.size() == 0)
  457. return _emptyFunctionSet;
  458. return funcs.toArray(new Func[funcs.size()]);
  459. }
  460. /**
  461. * Returns the effects associated with the item.
  462. * @param instance : L2ItemInstance pointing out the item
  463. * @param player : L2Character pointing out the player
  464. * @return L2Effect[] : array of effects generated by the item
  465. */
  466. public L2Effect[] getEffects(L2ItemInstance instance, L2Character player)
  467. {
  468. if (_effectTemplates == null)
  469. return _emptyEffectSet;
  470. List<L2Effect> effects = new FastList<L2Effect>();
  471. for (EffectTemplate et : _effectTemplates)
  472. {
  473. Env env = new Env();
  474. env.player = player;
  475. env.target = player;
  476. env.item = instance;
  477. L2Effect e = et.getEffect(env);
  478. if (e != null)
  479. effects.add(e);
  480. }
  481. if (effects.size() == 0)
  482. return _emptyEffectSet;
  483. return effects.toArray(new L2Effect[effects.size()]);
  484. }
  485. /**
  486. * Returns effects of skills associated with the item.
  487. * @param caster : L2Character pointing out the caster
  488. * @param target : L2Character pointing out the target
  489. * @return L2Effect[] : array of effects generated by the skill
  490. */
  491. public L2Effect[] getSkillEffects(L2Character caster, L2Character target)
  492. {
  493. if (_skills == null)
  494. return _emptyEffectSet;
  495. List<L2Effect> effects = new FastList<L2Effect>();
  496. for (L2Skill skill : _skills)
  497. {
  498. if (!skill.checkCondition(caster, target, true))
  499. continue; // Skill condition not met
  500. if (target.getFirstEffect(skill.getId()) != null)
  501. target.removeEffect(target.getFirstEffect(skill.getId()));
  502. for (L2Effect e : skill.getEffects(caster, target))
  503. effects.add(e);
  504. }
  505. if (effects.size() == 0)
  506. return _emptyEffectSet;
  507. return effects.toArray(new L2Effect[effects.size()]);
  508. }
  509. /**
  510. * Add the FuncTemplate f to the list of functions used with the item
  511. * @param f : FuncTemplate to add
  512. */
  513. public void attach(FuncTemplate f)
  514. {
  515. // If _functTemplates is empty, create it and add the FuncTemplate f in it
  516. if (_funcTemplates == null)
  517. {
  518. _funcTemplates = new FuncTemplate[]
  519. {
  520. f
  521. };
  522. }
  523. else
  524. {
  525. int len = _funcTemplates.length;
  526. FuncTemplate[] tmp = new FuncTemplate[len + 1];
  527. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  528. // number of components to be copied)
  529. System.arraycopy(_funcTemplates, 0, tmp, 0, len);
  530. tmp[len] = f;
  531. _funcTemplates = tmp;
  532. }
  533. }
  534. /**
  535. * Add the EffectTemplate effect to the list of effects generated by the item
  536. * @param effect : EffectTemplate
  537. */
  538. public void attach(EffectTemplate effect)
  539. {
  540. if (_effectTemplates == null)
  541. {
  542. _effectTemplates = new EffectTemplate[]
  543. {
  544. effect
  545. };
  546. }
  547. else
  548. {
  549. int len = _effectTemplates.length;
  550. EffectTemplate[] tmp = new EffectTemplate[len + 1];
  551. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  552. // number of components to be copied)
  553. System.arraycopy(_effectTemplates, 0, tmp, 0, len);
  554. tmp[len] = effect;
  555. _effectTemplates = tmp;
  556. }
  557. }
  558. /**
  559. * Add the L2Skill skill to the list of skills generated by the item
  560. * @param skill : L2Skill
  561. */
  562. public void attach(L2Skill skill)
  563. {
  564. if (_skills == null)
  565. {
  566. _skills = new L2Skill[]
  567. {
  568. skill
  569. };
  570. }
  571. else
  572. {
  573. int len = _skills.length;
  574. L2Skill[] tmp = new L2Skill[len + 1];
  575. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  576. // number of components to be copied)
  577. System.arraycopy(_skills, 0, tmp, 0, len);
  578. tmp[len] = skill;
  579. _skills = tmp;
  580. }
  581. }
  582. /**
  583. * Returns the name of the item
  584. * @return String
  585. */
  586. @Override
  587. public String toString()
  588. {
  589. return _name;
  590. }
  591. }