EnchantItem.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.enchant;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.datatables.ItemTable;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.items.L2Item;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  27. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  28. import com.l2jserver.gameserver.model.items.type.L2ItemType;
  29. import com.l2jserver.gameserver.util.Util;
  30. /**
  31. * @author UnAfraid
  32. */
  33. public class EnchantItem
  34. {
  35. protected static final Logger _log = Logger.getLogger(EnchantItem.class.getName());
  36. private static final L2ItemType[] ENCHANT_TYPES = new L2ItemType[]
  37. {
  38. L2EtcItemType.ANCIENT_CRYSTAL_ENCHANT_AM,
  39. L2EtcItemType.ANCIENT_CRYSTAL_ENCHANT_WP,
  40. L2EtcItemType.BLESS_SCRL_ENCHANT_AM,
  41. L2EtcItemType.BLESS_SCRL_ENCHANT_WP,
  42. L2EtcItemType.SCRL_ENCHANT_AM,
  43. L2EtcItemType.SCRL_ENCHANT_WP,
  44. L2EtcItemType.SCRL_INC_ENCHANT_PROP_AM,
  45. L2EtcItemType.SCRL_INC_ENCHANT_PROP_WP,
  46. };
  47. private final int _id;
  48. protected boolean _isWeapon;
  49. private final int _grade;
  50. private final int _maxEnchantLevel;
  51. private final double _bonusRate;
  52. private List<Integer> _itemIds;
  53. public EnchantItem(StatsSet set)
  54. {
  55. _id = set.getInteger("id");
  56. if (getItem() == null)
  57. {
  58. throw new NullPointerException();
  59. }
  60. else if (!Util.contains(ENCHANT_TYPES, getItem().getItemType()))
  61. {
  62. throw new IllegalAccessError();
  63. }
  64. _isWeapon = getItem().getItemType() == L2EtcItemType.SCRL_INC_ENCHANT_PROP_WP;
  65. _grade = ItemTable._crystalTypes.get(set.getString("targetGrade", "none"));
  66. _maxEnchantLevel = set.getInteger("maxEnchant", 65535);
  67. _bonusRate = set.getDouble("bonusRate", 0);
  68. }
  69. /**
  70. * @return id of current item
  71. */
  72. public final int getId()
  73. {
  74. return _id;
  75. }
  76. /**
  77. * @return bonus chance that would be added
  78. */
  79. public final double getBonusRate()
  80. {
  81. return _bonusRate;
  82. }
  83. /**
  84. * @return {@link L2Item} current item/scroll
  85. */
  86. public final L2Item getItem()
  87. {
  88. return ItemTable.getInstance().getTemplate(_id);
  89. }
  90. /**
  91. * @return grade of the item/scroll.
  92. */
  93. public final int getGrade()
  94. {
  95. return _grade;
  96. }
  97. /**
  98. * @return {@code true} if scroll is for weapon, {@code false} for armor
  99. */
  100. public boolean isWeapon()
  101. {
  102. return _isWeapon;
  103. }
  104. /**
  105. * Enforces current scroll to use only those items as possible items to enchant
  106. * @param id
  107. */
  108. public final void addItem(int id)
  109. {
  110. if (_itemIds == null)
  111. {
  112. _itemIds = new ArrayList<>();
  113. }
  114. _itemIds.add(id);
  115. }
  116. /**
  117. * @param enchantItem
  118. * @return {@code true} if current item is valid to be enchanted, {@code false} otherwise
  119. */
  120. public final boolean isValid(L2ItemInstance enchantItem)
  121. {
  122. if (enchantItem == null)
  123. {
  124. return false;
  125. }
  126. else if (enchantItem.isEnchantable() == 0)
  127. {
  128. return false;
  129. }
  130. else if (!isValidItemType(enchantItem.getItem().getType2()))
  131. {
  132. return false;
  133. }
  134. else if ((_maxEnchantLevel != 0) && (enchantItem.getEnchantLevel() >= _maxEnchantLevel))
  135. {
  136. return false;
  137. }
  138. else if (_grade != enchantItem.getItem().getItemGradeSPlus())
  139. {
  140. return false;
  141. }
  142. else if ((_itemIds != null) && !_itemIds.contains(enchantItem.getId()))
  143. {
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * @param type2
  150. * @return {@code true} if current type2 is valid to be enchanted, {@code false} otherwise
  151. */
  152. private final boolean isValidItemType(int type2)
  153. {
  154. if (type2 == L2Item.TYPE2_WEAPON)
  155. {
  156. return _isWeapon;
  157. }
  158. else if ((type2 == L2Item.TYPE2_SHIELD_ARMOR) || (type2 == L2Item.TYPE2_ACCESSORY))
  159. {
  160. return !_isWeapon;
  161. }
  162. return false;
  163. }
  164. }