SkillsEngine.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 net.sf.l2j.gameserver.skills;
  16. import java.io.File;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.Config;
  22. import net.sf.l2j.gameserver.Item;
  23. import net.sf.l2j.gameserver.datatables.SkillTable;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.templates.item.L2Armor;
  26. import net.sf.l2j.gameserver.templates.item.L2EtcItem;
  27. import net.sf.l2j.gameserver.templates.item.L2EtcItemType;
  28. import net.sf.l2j.gameserver.templates.item.L2Item;
  29. import net.sf.l2j.gameserver.templates.item.L2Weapon;
  30. /**
  31. * @author mkizub
  32. *
  33. * TODO To change the template for this generated type comment go to
  34. * Window - Preferences - Java - Code Style - Code Templates
  35. */
  36. public class SkillsEngine {
  37. protected static final Logger _log = Logger.getLogger(SkillsEngine.class.getName());
  38. private static final SkillsEngine _instance = new SkillsEngine();
  39. private List<File> _armorFiles = new FastList<File>();
  40. private List<File> _weaponFiles = new FastList<File>();
  41. private List<File> _etcitemFiles = new FastList<File>();
  42. private List<File> _skillFiles = new FastList<File>();
  43. public static SkillsEngine getInstance()
  44. {
  45. return _instance;
  46. }
  47. private SkillsEngine()
  48. {
  49. hashFiles("data/stats/etcitem", _etcitemFiles);
  50. hashFiles("data/stats/armor", _armorFiles);
  51. hashFiles("data/stats/weapon", _weaponFiles);
  52. hashFiles("data/stats/skills", _skillFiles);
  53. }
  54. private void hashFiles(String dirname, List<File> hash)
  55. {
  56. File dir = new File(Config.DATAPACK_ROOT, dirname);
  57. if (!dir.exists())
  58. {
  59. _log.config("Dir "+dir.getAbsolutePath()+" not exists");
  60. return;
  61. }
  62. File[] files = dir.listFiles();
  63. for (File f : files)
  64. {
  65. if (f.getName().endsWith(".xml"))
  66. if (!f.getName().startsWith("custom"))
  67. hash.add(f);
  68. }
  69. File customfile = new File(Config.DATAPACK_ROOT, dirname+"/custom.xml");
  70. if (customfile.exists())
  71. hash.add(customfile);
  72. }
  73. public List<L2Skill> loadSkills(File file)
  74. {
  75. if (file == null)
  76. {
  77. _log.config("Skill file not found.");
  78. return null;
  79. }
  80. DocumentSkill doc = new DocumentSkill(file);
  81. doc.parse();
  82. return doc.getSkills();
  83. }
  84. public void loadAllSkills(Map<Integer, L2Skill> allSkills)
  85. {
  86. int count = 0;
  87. for (File file : _skillFiles)
  88. {
  89. List<L2Skill> s = loadSkills(file);
  90. if (s == null)
  91. continue;
  92. for (L2Skill skill : s)
  93. {
  94. allSkills.put(SkillTable.getSkillHashCode(skill), skill);
  95. count++;
  96. }
  97. }
  98. _log.config("SkillsEngine: Loaded "+count+" Skill templates from XML files.");
  99. }
  100. public List<L2Armor> loadArmors(Map<Integer, Item> armorData)
  101. {
  102. List<L2Armor> list = new FastList<L2Armor>();
  103. for (L2Item item : loadData(armorData, _armorFiles))
  104. {
  105. list.add((L2Armor)item);
  106. }
  107. return list;
  108. }
  109. public List<L2Weapon> loadWeapons(Map<Integer, Item> weaponData)
  110. {
  111. List<L2Weapon> list = new FastList<L2Weapon>();
  112. for (L2Item item : loadData(weaponData, _weaponFiles))
  113. {
  114. list.add((L2Weapon)item);
  115. }
  116. return list;
  117. }
  118. public List<L2EtcItem> loadItems(Map<Integer, Item> itemData)
  119. {
  120. List<L2EtcItem> list = new FastList<L2EtcItem>();
  121. List<Integer> xmlItem = new FastList<Integer>();
  122. for (L2Item item : loadData(itemData, _etcitemFiles))
  123. {
  124. list.add((L2EtcItem)item);
  125. xmlItem.add(item.getItemId());
  126. }
  127. for (Item item : itemData.values())
  128. {
  129. if (!xmlItem.contains(item.id))
  130. list.add(new L2EtcItem((L2EtcItemType)item.type, item.set));
  131. }
  132. return list;
  133. }
  134. public List<L2Item> loadData(Map<Integer, Item> itemData, List<File> files)
  135. {
  136. List<L2Item> list = new FastList<L2Item>();
  137. for (File f : files)
  138. {
  139. DocumentItem document = new DocumentItem(itemData, f);
  140. document.parse();
  141. list.addAll(document.getItemList());
  142. }
  143. return list;
  144. }
  145. }