SkillsEngine.java 4.6 KB

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