SkillsEngine.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.map.hash.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. import com.l2jserver.util.file.filter.XMLFilter;
  26. /**
  27. * @author mkizub
  28. */
  29. public class SkillsEngine
  30. {
  31. private static final Logger _log = Logger.getLogger(SkillsEngine.class.getName());
  32. private final List<File> _itemFiles = new FastList<File>();
  33. private final List<File> _skillFiles = new FastList<File>();
  34. public static SkillsEngine getInstance()
  35. {
  36. return SingletonHolder._instance;
  37. }
  38. private SkillsEngine()
  39. {
  40. hashFiles("data/stats/items", _itemFiles);
  41. if (Config.CUSTOM_ITEMS_LOAD)
  42. hashFiles("data/stats/items/custom", _itemFiles);
  43. hashFiles("data/stats/skills", _skillFiles);
  44. if (Config.CUSTOM_SKILLS_LOAD)
  45. hashFiles("data/stats/skills/custom", _skillFiles);
  46. }
  47. private void hashFiles(String dirname, List<File> hash)
  48. {
  49. File dir = new File(Config.DATAPACK_ROOT, dirname);
  50. if (!dir.exists())
  51. {
  52. _log.warning("Dir " + dir.getAbsolutePath() + " not exists");
  53. return;
  54. }
  55. File[] files = dir.listFiles(new XMLFilter());
  56. for (File f : files)
  57. {
  58. hash.add(f);
  59. }
  60. }
  61. public List<L2Skill> loadSkills(File file)
  62. {
  63. if (file == null)
  64. {
  65. _log.warning("Skill file not found.");
  66. return null;
  67. }
  68. DocumentSkill doc = new DocumentSkill(file);
  69. doc.parse();
  70. return doc.getSkills();
  71. }
  72. public void loadAllSkills(final TIntObjectHashMap<L2Skill> allSkills)
  73. {
  74. int count = 0;
  75. for (File file : _skillFiles)
  76. {
  77. List<L2Skill> s = loadSkills(file);
  78. if (s == null)
  79. {
  80. continue;
  81. }
  82. for (L2Skill skill : s)
  83. {
  84. allSkills.put(SkillTable.getSkillHashCode(skill), skill);
  85. count++;
  86. }
  87. }
  88. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
  89. }
  90. /**
  91. * Return created items
  92. * @return List of {@link L2Item}
  93. */
  94. public List<L2Item> loadItems()
  95. {
  96. List<L2Item> list = new FastList<L2Item>();
  97. for (File f : _itemFiles)
  98. {
  99. DocumentItem document = new DocumentItem(f);
  100. document.parse();
  101. list.addAll(document.getItemList());
  102. }
  103. return list;
  104. }
  105. @SuppressWarnings("synthetic-access")
  106. private static class SingletonHolder
  107. {
  108. protected static final SkillsEngine _instance = new SkillsEngine();
  109. }
  110. }