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