2
0

EnchantGroupsData.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 com.l2jserver.gameserver.datatables;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.logging.Level;
  19. import org.w3c.dom.NamedNodeMap;
  20. import org.w3c.dom.Node;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.engines.DocumentParser;
  23. import com.l2jserver.gameserver.model.L2EnchantSkillGroup;
  24. import com.l2jserver.gameserver.model.L2EnchantSkillGroup.EnchantSkillHolder;
  25. import com.l2jserver.gameserver.model.L2EnchantSkillLearn;
  26. import com.l2jserver.gameserver.model.StatsSet;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.skills.L2Skill;
  29. /**
  30. * This class holds the Enchant Groups information.
  31. * @author Micr0
  32. */
  33. public class EnchantGroupsData extends DocumentParser
  34. {
  35. public static final int NORMAL_ENCHANT_COST_MULTIPLIER = Config.NORMAL_ENCHANT_COST_MULTIPLIER;
  36. public static final int SAFE_ENCHANT_COST_MULTIPLIER = Config.SAFE_ENCHANT_COST_MULTIPLIER;
  37. public static final int NORMAL_ENCHANT_BOOK = 6622;
  38. public static final int SAFE_ENCHANT_BOOK = 9627;
  39. public static final int CHANGE_ENCHANT_BOOK = 9626;
  40. public static final int UNTRAIN_ENCHANT_BOOK = 9625;
  41. private final Map<Integer, L2EnchantSkillGroup> _enchantSkillGroups = new HashMap<>();
  42. private final Map<Integer, L2EnchantSkillLearn> _enchantSkillTrees = new HashMap<>();
  43. /**
  44. * Instantiates a new enchant groups table.
  45. */
  46. protected EnchantGroupsData()
  47. {
  48. load();
  49. }
  50. @Override
  51. public void load()
  52. {
  53. _enchantSkillGroups.clear();
  54. _enchantSkillTrees.clear();
  55. parseDatapackFile("data/enchantSkillGroups.xml");
  56. int routes = 0;
  57. for (L2EnchantSkillGroup group : _enchantSkillGroups.values())
  58. {
  59. routes += group.getEnchantGroupDetails().size();
  60. }
  61. _log.info(getClass().getSimpleName() + ": Loaded " + _enchantSkillGroups.size() + " groups and " + routes + " routes.");
  62. }
  63. @Override
  64. protected void parseDocument()
  65. {
  66. NamedNodeMap attrs;
  67. StatsSet set;
  68. Node att;
  69. int id = 0;
  70. L2EnchantSkillGroup group;
  71. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  72. {
  73. if ("list".equalsIgnoreCase(n.getNodeName()))
  74. {
  75. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  76. {
  77. if ("group".equalsIgnoreCase(d.getNodeName()))
  78. {
  79. attrs = d.getAttributes();
  80. id = parseInt(attrs, "id");
  81. group = _enchantSkillGroups.get(id);
  82. if (group == null)
  83. {
  84. group = new L2EnchantSkillGroup(id);
  85. _enchantSkillGroups.put(id, group);
  86. }
  87. for (Node b = d.getFirstChild(); b != null; b = b.getNextSibling())
  88. {
  89. if ("enchant".equalsIgnoreCase(b.getNodeName()))
  90. {
  91. attrs = b.getAttributes();
  92. set = new StatsSet();
  93. for (int i = 0; i < attrs.getLength(); i++)
  94. {
  95. att = attrs.item(i);
  96. set.set(att.getNodeName(), att.getNodeValue());
  97. }
  98. group.addEnchantDetail(new EnchantSkillHolder(set));
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Adds the new route for skill.
  108. * @param skillId the skill id
  109. * @param maxLvL the max lvl
  110. * @param route the route
  111. * @param group the group
  112. * @return the int
  113. */
  114. public int addNewRouteForSkill(int skillId, int maxLvL, int route, int group)
  115. {
  116. L2EnchantSkillLearn enchantableSkill = _enchantSkillTrees.get(skillId);
  117. if (enchantableSkill == null)
  118. {
  119. enchantableSkill = new L2EnchantSkillLearn(skillId, maxLvL);
  120. _enchantSkillTrees.put(skillId, enchantableSkill);
  121. }
  122. if (_enchantSkillGroups.containsKey(group))
  123. {
  124. enchantableSkill.addNewEnchantRoute(route, group);
  125. return _enchantSkillGroups.get(group).getEnchantGroupDetails().size();
  126. }
  127. _log.log(Level.SEVERE, "Error while loading generating enchant skill id: " + skillId + "; route: " + route + "; missing group: " + group);
  128. return 0;
  129. }
  130. /**
  131. * Gets the skill enchantment for skill.
  132. * @param skill the skill
  133. * @return the skill enchantment for skill
  134. */
  135. public L2EnchantSkillLearn getSkillEnchantmentForSkill(L2Skill skill)
  136. {
  137. // there is enchantment for this skill and we have the required level of it
  138. final L2EnchantSkillLearn esl = getSkillEnchantmentBySkillId(skill.getId());
  139. if ((esl != null) && (skill.getLevel() >= esl.getBaseLevel()))
  140. {
  141. return esl;
  142. }
  143. return null;
  144. }
  145. /**
  146. * Gets the skill enchantment by skill id.
  147. * @param skillId the skill id
  148. * @return the skill enchantment by skill id
  149. */
  150. public L2EnchantSkillLearn getSkillEnchantmentBySkillId(int skillId)
  151. {
  152. return _enchantSkillTrees.get(skillId);
  153. }
  154. /**
  155. * Gets the enchant skill group by id.
  156. * @param id the id
  157. * @return the enchant skill group by id
  158. */
  159. public L2EnchantSkillGroup getEnchantSkillGroupById(int id)
  160. {
  161. return _enchantSkillGroups.get(id);
  162. }
  163. /**
  164. * Gets the enchant skill sp cost.
  165. * @param skill the skill
  166. * @return the enchant skill sp cost
  167. */
  168. public int getEnchantSkillSpCost(L2Skill skill)
  169. {
  170. final L2EnchantSkillLearn enchantSkillLearn = _enchantSkillTrees.get(skill.getId());
  171. if (enchantSkillLearn != null)
  172. {
  173. final EnchantSkillHolder esh = enchantSkillLearn.getEnchantSkillHolder(skill.getLevel());
  174. if (esh != null)
  175. {
  176. return esh.getSpCost();
  177. }
  178. }
  179. return Integer.MAX_VALUE;
  180. }
  181. /**
  182. * Gets the enchant skill Adena cost.
  183. * @param skill the skill
  184. * @return the enchant skill Adena cost
  185. */
  186. public int getEnchantSkillAdenaCost(L2Skill skill)
  187. {
  188. final L2EnchantSkillLearn enchantSkillLearn = _enchantSkillTrees.get(skill.getId());
  189. if (enchantSkillLearn != null)
  190. {
  191. final EnchantSkillHolder esh = enchantSkillLearn.getEnchantSkillHolder(skill.getLevel());
  192. if (esh != null)
  193. {
  194. return esh.getAdenaCost();
  195. }
  196. }
  197. return Integer.MAX_VALUE;
  198. }
  199. /**
  200. * Gets the enchant skill rate.
  201. * @param player the player
  202. * @param skill the skill
  203. * @return the enchant skill rate
  204. */
  205. public byte getEnchantSkillRate(L2PcInstance player, L2Skill skill)
  206. {
  207. final L2EnchantSkillLearn enchantSkillLearn = _enchantSkillTrees.get(skill.getId());
  208. if (enchantSkillLearn != null)
  209. {
  210. final EnchantSkillHolder esh = enchantSkillLearn.getEnchantSkillHolder(skill.getLevel());
  211. if (esh != null)
  212. {
  213. return esh.getRate(player);
  214. }
  215. }
  216. return 0;
  217. }
  218. /**
  219. * Gets the single instance of EnchantGroupsData.
  220. * @return single instance of EnchantGroupsData
  221. */
  222. public static EnchantGroupsData getInstance()
  223. {
  224. return SingletonHolder._instance;
  225. }
  226. private static class SingletonHolder
  227. {
  228. protected static final EnchantGroupsData _instance = new EnchantGroupsData();
  229. }
  230. }