2
0

SubPledgeSkillTree.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 gnu.trove.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.Iterator;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javolution.util.FastList;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.model.L2Clan;
  28. import com.l2jserver.gameserver.model.L2Skill;
  29. /**
  30. * @author JIV
  31. *
  32. */
  33. public class SubPledgeSkillTree
  34. {
  35. private static final Logger _log = Logger.getLogger(SubPledgeSkillTree.class.getName());
  36. private TIntObjectHashMap<SubUnitSkill> skilltree = new TIntObjectHashMap<SubPledgeSkillTree.SubUnitSkill>();
  37. public SubPledgeSkillTree()
  38. {
  39. load();
  40. }
  41. public static SubPledgeSkillTree getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. public static class SubUnitSkill
  46. {
  47. private L2Skill skill;
  48. private int clanLvl;
  49. private int reputation;
  50. private int itemId;
  51. private int count;
  52. public SubUnitSkill(L2Skill skill, int clanLvl, int reputation, int itemId, int count)
  53. {
  54. super();
  55. this.skill = skill;
  56. this.clanLvl = clanLvl;
  57. this.reputation = reputation;
  58. this.itemId = itemId;
  59. this.count = count;
  60. }
  61. public L2Skill getSkill()
  62. {
  63. return skill;
  64. }
  65. public int getClanLvl()
  66. {
  67. return clanLvl;
  68. }
  69. public int getReputation()
  70. {
  71. return reputation;
  72. }
  73. public int getItemId()
  74. {
  75. return itemId;
  76. }
  77. public int getCount()
  78. {
  79. return count;
  80. }
  81. }
  82. public void reload()
  83. {
  84. load();
  85. }
  86. private void load()
  87. {
  88. skilltree.clear();
  89. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  90. factory.setValidating(false);
  91. factory.setIgnoringComments(true);
  92. File file = new File(Config.DATAPACK_ROOT, "data/skillTrees/subPledgeSkillTree.xml");
  93. Document doc = null;
  94. if (file.exists())
  95. {
  96. try
  97. {
  98. doc = factory.newDocumentBuilder().parse(file);
  99. }
  100. catch(Exception e)
  101. {
  102. _log.log(Level.WARNING, "Could not parse subPledgeSkillTree.xml file: " + e.getMessage(), e);
  103. }
  104. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  105. {
  106. if ("skill_tree".equalsIgnoreCase(n.getNodeName()))
  107. {
  108. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  109. {
  110. if ("skill".equalsIgnoreCase(d.getNodeName()))
  111. {
  112. NamedNodeMap attrs = d.getAttributes();
  113. Node att;
  114. int skillId;
  115. int skillLvl;
  116. int clanLvl;
  117. int reputation;
  118. int itemId;
  119. int count;
  120. att = attrs.getNamedItem("id");
  121. if (att == null)
  122. {
  123. _log.severe("[SubPledgeSkillTree] Missing id, skipping");
  124. continue;
  125. }
  126. skillId = Integer.parseInt(att.getNodeValue());
  127. att = attrs.getNamedItem("level");
  128. if (att == null)
  129. {
  130. _log.severe("[SubPledgeSkillTree] Missing level, skipping");
  131. continue;
  132. }
  133. skillLvl = Integer.parseInt(att.getNodeValue());
  134. att = attrs.getNamedItem("reputation");
  135. if (att == null)
  136. {
  137. _log.severe("[SubPledgeSkillTree] Missing reputation, skipping");
  138. continue;
  139. }
  140. reputation = Integer.parseInt(att.getNodeValue());
  141. att = attrs.getNamedItem("clan_level");
  142. if (att == null)
  143. {
  144. _log.severe("[SubPledgeSkillTree] Missing clan_level, skipping");
  145. continue;
  146. }
  147. clanLvl = Integer.parseInt(att.getNodeValue());
  148. att = attrs.getNamedItem("itemId");
  149. if (att == null)
  150. {
  151. _log.severe("[SubPledgeSkillTree] Missing itemId, skipping");
  152. continue;
  153. }
  154. itemId = Integer.parseInt(att.getNodeValue());
  155. att = attrs.getNamedItem("count");
  156. if (att == null)
  157. {
  158. _log.severe("[SubPledgeSkillTree] Missing count, skipping");
  159. continue;
  160. }
  161. count = Integer.parseInt(att.getNodeValue());
  162. L2Skill skill = SkillTable.getInstance().getInfo(skillId, skillLvl);
  163. if (skill == null)
  164. {
  165. _log.severe("[SubPledgeSkillTree] Skill "+skillId+" not exist, skipping");
  166. continue;
  167. }
  168. skilltree.put(SkillTable.getSkillHashCode(skill), new SubUnitSkill(skill, clanLvl, reputation, itemId, count));
  169. }
  170. }
  171. }
  172. }
  173. }
  174. _log.info(getClass().getSimpleName()+": Loaded "+skilltree.size()+" SubUnit Skills");
  175. }
  176. public SubUnitSkill getSkill(int skillhash)
  177. {
  178. return skilltree.get(skillhash);
  179. }
  180. public SubUnitSkill[] getAvailableSkills(L2Clan clan)
  181. {
  182. FastList<SubUnitSkill> list = FastList.newInstance();
  183. for (Object obj : skilltree.getValues())
  184. {
  185. SubUnitSkill skill = (SubUnitSkill) obj;
  186. if (skill.getClanLvl() <= clan.getLevel())
  187. list.add(skill);
  188. }
  189. Iterator<SubUnitSkill> it = list.iterator();
  190. while (it.hasNext())
  191. {
  192. SubUnitSkill sus = it.next();
  193. if (!clan.isLearnableSubSkill(sus.getSkill()))
  194. it.remove();
  195. }
  196. SubUnitSkill[] result = list.toArray(new SubUnitSkill[list.size()]);
  197. FastList.recycle(list);
  198. return result;
  199. }
  200. @SuppressWarnings("synthetic-access")
  201. private static class SingletonHolder
  202. {
  203. protected static final SubPledgeSkillTree _instance = new SubPledgeSkillTree();
  204. }
  205. }