L2Item.java 18 KB

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