L2Item.java 18 KB

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