EnchantSkillGroupsData.java 7.3 KB

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