EnchantGroupsData.java 7.4 KB

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