L2Item.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. protected final Enum<?> _type;
  131. protected FuncTemplate[] _funcTemplates;
  132. protected EffectTemplate[] _effectTemplates;
  133. protected L2Skill[] _skills;
  134. private static final Func[] _emptyFunctionSet = new Func[0];
  135. protected static final L2Effect[] _emptyEffectSet = new L2Effect[0];
  136. /**
  137. * Constructor of the L2Item that fill class variables.<BR><BR>
  138. * <U><I>Variables filled :</I></U><BR>
  139. * <LI>type</LI>
  140. * <LI>_itemId</LI>
  141. * <LI>_name</LI>
  142. * <LI>_type1 & _type2</LI>
  143. * <LI>_weight</LI>
  144. * <LI>_crystallizable</LI>
  145. * <LI>_stackable</LI>
  146. * <LI>_materialType & _crystalType & _crystlaCount</LI>
  147. * <LI>_duration</LI>
  148. * <LI>_bodypart</LI>
  149. * <LI>_referencePrice</LI>
  150. * <LI>_sellable</LI>
  151. * @param type : Enum designating the type of the item
  152. * @param set : StatsSet corresponding to a set of couples (key,value) for description of the item
  153. */
  154. protected L2Item(Enum<?> type, StatsSet set)
  155. {
  156. _type = type;
  157. _itemId = set.getInteger("item_id");
  158. _name = set.getString("name");
  159. _type1 = set.getInteger("type1"); // needed for item list (inventory)
  160. _type2 = set.getInteger("type2"); // different lists for armor, weapon, etc
  161. _weight = set.getInteger("weight");
  162. _crystallizable = set.getBool("crystallizable");
  163. _stackable = set.getBool("stackable", false);
  164. _materialType = set.getInteger("material");
  165. _crystalType = set.getInteger("crystal_type", CRYSTAL_NONE); // default to none-grade
  166. _duration = set.getInteger("duration");
  167. _bodyPart = set.getInteger("bodypart");
  168. _referencePrice = set.getInteger("price");
  169. _crystalCount = set.getInteger("crystal_count", 0);
  170. _sellable = set.getBool("sellable", true);
  171. _dropable = set.getBool("dropable", true);
  172. _destroyable = set.getBool("destroyable", true);
  173. _tradeable = set.getBool("tradeable", true);
  174. }
  175. /**
  176. * Returns the itemType.
  177. * @return Enum
  178. */
  179. public Enum<?> getItemType()
  180. {
  181. return _type;
  182. }
  183. /**
  184. * Returns the duration of the item
  185. * @return int
  186. */
  187. public final int getDuration()
  188. {
  189. return _duration;
  190. }
  191. /**
  192. * Returns the ID of the iden
  193. * @return int
  194. */
  195. public final int getItemId()
  196. {
  197. return _itemId;
  198. }
  199. public abstract int getItemMask();
  200. /**
  201. * Return the type of material of the item
  202. * @return int
  203. */
  204. public final int getMaterialType()
  205. {
  206. return _materialType;
  207. }
  208. /**
  209. * Returns the type 2 of the item
  210. * @return int
  211. */
  212. public final int getType2()
  213. {
  214. return _type2;
  215. }
  216. /**
  217. * Returns the weight of the item
  218. * @return int
  219. */
  220. public final int getWeight()
  221. {
  222. return _weight;
  223. }
  224. /**
  225. * Returns if the item is crystallizable
  226. * @return boolean
  227. */
  228. public final boolean isCrystallizable()
  229. {
  230. return _crystallizable;
  231. }
  232. /**
  233. * Return the type of crystal if item is crystallizable
  234. * @return int
  235. */
  236. public final int getCrystalType()
  237. {
  238. return _crystalType;
  239. }
  240. /**
  241. * Return the type of crystal if item is crystallizable
  242. * @return int
  243. */
  244. public final int getCrystalItemId()
  245. {
  246. return crystalItemId[_crystalType];
  247. }
  248. /**
  249. * Returns the grade of the item.<BR><BR>
  250. * <U><I>Concept :</I></U><BR>
  251. * In fact, this fucntion returns the type of crystal of the item.
  252. * @return int
  253. */
  254. public final int getItemGrade()
  255. {
  256. return getCrystalType();
  257. }
  258. /**
  259. * Returns the quantity of crystals for crystallization
  260. * @return int
  261. */
  262. public final int getCrystalCount()
  263. {
  264. return _crystalCount;
  265. }
  266. /**
  267. * Returns the quantity of crystals for crystallization on specific enchant level
  268. * @return int
  269. */
  270. public final int getCrystalCount(int enchantLevel)
  271. {
  272. if (enchantLevel > 3) switch(_type2)
  273. {
  274. case TYPE2_SHIELD_ARMOR: case TYPE2_ACCESSORY: return _crystalCount + crystalEnchantBonusArmor[getCrystalType()] * (3*enchantLevel -6);
  275. case TYPE2_WEAPON: return _crystalCount + crystalEnchantBonusWeapon[getCrystalType()] * (2*enchantLevel -3);
  276. default: return _crystalCount;
  277. }
  278. else if (enchantLevel > 0) switch(_type2)
  279. {
  280. case TYPE2_SHIELD_ARMOR: case TYPE2_ACCESSORY: return _crystalCount + crystalEnchantBonusArmor[getCrystalType()] * enchantLevel;
  281. case TYPE2_WEAPON: return _crystalCount + crystalEnchantBonusWeapon[getCrystalType()] * enchantLevel;
  282. default: return _crystalCount;
  283. }
  284. else return _crystalCount;
  285. }
  286. /**
  287. * Returns the name of the item
  288. * @return String
  289. */
  290. public final String getName()
  291. {
  292. return _name;
  293. }
  294. /**
  295. * Return the part of the body used with the item.
  296. * @return int
  297. */
  298. public final int getBodyPart()
  299. {
  300. return _bodyPart;
  301. }
  302. /**
  303. * Returns the type 1 of the item
  304. * @return int
  305. */
  306. public final int getType1()
  307. {
  308. return _type1;
  309. }
  310. /**
  311. * Returns if the item is stackable
  312. * @return boolean
  313. */
  314. public final boolean isStackable()
  315. {
  316. return _stackable;
  317. }
  318. /**
  319. * Returns if the item is consumable
  320. * @return boolean
  321. */
  322. public boolean isConsumable()
  323. {
  324. return false;
  325. }
  326. public boolean isEquipable()
  327. {
  328. return this.getBodyPart() != 0 && !(this.getItemType() instanceof L2EtcItemType);
  329. }
  330. /**
  331. * Returns the price of reference of the item
  332. * @return int
  333. */
  334. public final int getReferencePrice()
  335. {
  336. return (isConsumable() ? (int)(_referencePrice * Config.RATE_CONSUMABLE_COST) : _referencePrice);
  337. }
  338. /**
  339. * Returns if the item can be sold
  340. * @return boolean
  341. */
  342. public final boolean isSellable()
  343. {
  344. return _sellable;
  345. }
  346. /**
  347. * Returns if the item can dropped
  348. * @return boolean
  349. */
  350. public final boolean isDropable()
  351. {
  352. return _dropable;
  353. }
  354. /**
  355. * Returns if the item can destroy
  356. * @return boolean
  357. */
  358. public final boolean isDestroyable()
  359. {
  360. return _destroyable;
  361. }
  362. /**
  363. * Returns if the item can add to trade
  364. * @return boolean
  365. */
  366. public final boolean isTradeable()
  367. {
  368. return _tradeable;
  369. }
  370. /**
  371. * Returns if item is for hatchling
  372. * @return boolean
  373. */
  374. public boolean isForHatchling()
  375. {
  376. return (_type2 == TYPE2_PET_HATCHLING);
  377. }
  378. /**
  379. * Returns if item is for strider
  380. * @return boolean
  381. */
  382. public boolean isForStrider()
  383. {
  384. return (_type2 == TYPE2_PET_STRIDER);
  385. }
  386. /**
  387. * Returns if item is for wolf
  388. * @return boolean
  389. */
  390. public boolean isForWolf()
  391. {
  392. return (_type2 == TYPE2_PET_WOLF);
  393. }
  394. /**
  395. * Returns if item is for wolf
  396. * @return boolean
  397. */
  398. public boolean isForBabyPet()
  399. {
  400. return (_type2 == TYPE2_PET_BABY);
  401. }
  402. /**
  403. * Returns array of Func objects containing the list of functions used by the item
  404. * @param instance : L2ItemInstance pointing out the item
  405. * @param player : L2Character pointing out the player
  406. * @return Func[] : array of functions
  407. */
  408. public Func[] getStatFuncs(L2ItemInstance instance, L2Character player)
  409. {
  410. if (_funcTemplates == null)
  411. return _emptyFunctionSet;
  412. List<Func> funcs = new FastList<Func>();
  413. for (FuncTemplate t : _funcTemplates) {
  414. Env env = new Env();
  415. env.player = player;
  416. env.target = player;
  417. env.item = instance;
  418. Func f = t.getFunc(env, this); // skill is owner
  419. if (f != null)
  420. funcs.add(f);
  421. }
  422. if (funcs.size() == 0)
  423. return _emptyFunctionSet;
  424. return funcs.toArray(new Func[funcs.size()]);
  425. }
  426. /**
  427. * Returns the effects associated with the item.
  428. * @param instance : L2ItemInstance pointing out the item
  429. * @param player : L2Character pointing out the player
  430. * @return L2Effect[] : array of effects generated by the item
  431. */
  432. public L2Effect[] getEffects(L2ItemInstance instance, L2Character player)
  433. {
  434. if (_effectTemplates == null)
  435. return _emptyEffectSet;
  436. List<L2Effect> effects = new FastList<L2Effect>();
  437. for (EffectTemplate et : _effectTemplates) {
  438. Env env = new Env();
  439. env.player = player;
  440. env.target = player;
  441. env.item = instance;
  442. L2Effect e = et.getEffect(env);
  443. if (e != null)
  444. effects.add(e);
  445. }
  446. if (effects.size() == 0)
  447. return _emptyEffectSet;
  448. return effects.toArray(new L2Effect[effects.size()]);
  449. }
  450. /**
  451. * Returns effects of skills associated with the item.
  452. * @param caster : L2Character pointing out the caster
  453. * @param target : L2Character pointing out the target
  454. * @return L2Effect[] : array of effects generated by the skill
  455. */
  456. public L2Effect[] getSkillEffects(L2Character caster, L2Character target)
  457. {
  458. if (_skills == null)
  459. return _emptyEffectSet;
  460. List<L2Effect> effects = new FastList<L2Effect>();
  461. for (L2Skill skill : _skills)
  462. {
  463. if (!skill.checkCondition(caster, target, true))
  464. continue; // Skill condition not met
  465. if (target.getFirstEffect(skill.getId()) != null)
  466. target.removeEffect(target.getFirstEffect(skill.getId()));
  467. for (L2Effect e:skill.getEffects(caster, target))
  468. effects.add(e);
  469. }
  470. if (effects.size() == 0)
  471. return _emptyEffectSet;
  472. return effects.toArray(new L2Effect[effects.size()]);
  473. }
  474. /**
  475. * Add the FuncTemplate f to the list of functions used with the item
  476. * @param f : FuncTemplate to add
  477. */
  478. public void attach(FuncTemplate f)
  479. {
  480. // If _functTemplates is empty, create it and add the FuncTemplate f in it
  481. if (_funcTemplates == null)
  482. {
  483. _funcTemplates = new FuncTemplate[]{f};
  484. }
  485. else
  486. {
  487. int len = _funcTemplates.length;
  488. FuncTemplate[] tmp = new FuncTemplate[len+1];
  489. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  490. // number of components to be copied)
  491. System.arraycopy(_funcTemplates, 0, tmp, 0, len);
  492. tmp[len] = f;
  493. _funcTemplates = tmp;
  494. }
  495. }
  496. /**
  497. * Add the EffectTemplate effect to the list of effects generated by the item
  498. * @param effect : EffectTemplate
  499. */
  500. public void attach(EffectTemplate effect)
  501. {
  502. if (_effectTemplates == null)
  503. {
  504. _effectTemplates = new EffectTemplate[]{effect};
  505. }
  506. else
  507. {
  508. int len = _effectTemplates.length;
  509. EffectTemplate[] tmp = new EffectTemplate[len+1];
  510. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  511. // number of components to be copied)
  512. System.arraycopy(_effectTemplates, 0, tmp, 0, len);
  513. tmp[len] = effect;
  514. _effectTemplates = tmp;
  515. }
  516. }
  517. /**
  518. * Add the L2Skill skill to the list of skills generated by the item
  519. * @param skill : L2Skill
  520. */
  521. public void attach(L2Skill skill)
  522. {
  523. if (_skills == null)
  524. {
  525. _skills = new L2Skill[]{skill};
  526. }
  527. else
  528. {
  529. int len = _skills.length;
  530. L2Skill[] tmp = new L2Skill[len+1];
  531. // Definition : arraycopy(array source, begins copy at this position of source, array destination, begins copy at this position in dest,
  532. // number of components to be copied)
  533. System.arraycopy(_skills, 0, tmp, 0, len);
  534. tmp[len] = skill;
  535. _skills = tmp;
  536. }
  537. }
  538. /**
  539. * Returns the name of the item
  540. * @return String
  541. */
  542. @Override
  543. public String toString()
  544. {
  545. return _name;
  546. }
  547. }