OptionData.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.xml.impl;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.gameserver.model.holders.SkillHolder;
  26. import com.l2jserver.gameserver.model.options.Options;
  27. import com.l2jserver.gameserver.model.options.OptionsSkillHolder;
  28. import com.l2jserver.gameserver.model.options.OptionsSkillType;
  29. import com.l2jserver.gameserver.model.stats.Stats;
  30. import com.l2jserver.gameserver.model.stats.functions.FuncTemplate;
  31. import com.l2jserver.util.data.xml.IXmlReader;
  32. /**
  33. * Item Option data.
  34. * @author UnAfraid
  35. */
  36. public class OptionData implements IXmlReader
  37. {
  38. private final Map<Integer, Options> _optionData = new HashMap<>();
  39. protected OptionData()
  40. {
  41. load();
  42. }
  43. @Override
  44. public synchronized void load()
  45. {
  46. _optionData.clear();
  47. parseDatapackDirectory("data/stats/options", false);
  48. LOGGER.info("{}: Loaded: {} Options.", getClass().getSimpleName(), _optionData.size());
  49. }
  50. @Override
  51. public void parseDocument(Document doc)
  52. {
  53. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  54. {
  55. if ("list".equalsIgnoreCase(n.getNodeName()))
  56. {
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. if ("option".equalsIgnoreCase(d.getNodeName()))
  60. {
  61. final int id = parseInteger(d.getAttributes(), "id");
  62. final Options op = new Options(id);
  63. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  64. {
  65. switch (cd.getNodeName())
  66. {
  67. case "for":
  68. {
  69. for (Node fd = cd.getFirstChild(); fd != null; fd = fd.getNextSibling())
  70. {
  71. switch (fd.getNodeName())
  72. {
  73. case "add":
  74. case "sub":
  75. case "mul":
  76. case "div":
  77. case "set":
  78. case "share":
  79. case "enchant":
  80. case "enchanthp":
  81. {
  82. parseFuncs(fd.getAttributes(), fd.getNodeName(), op);
  83. }
  84. }
  85. }
  86. break;
  87. }
  88. case "active_skill":
  89. {
  90. op.setActiveSkill(new SkillHolder(parseInteger(cd.getAttributes(), "id"), parseInteger(cd.getAttributes(), "level")));
  91. break;
  92. }
  93. case "passive_skill":
  94. {
  95. op.setPassiveSkill(new SkillHolder(parseInteger(cd.getAttributes(), "id"), parseInteger(cd.getAttributes(), "level")));
  96. break;
  97. }
  98. case "attack_skill":
  99. {
  100. op.addActivationSkill(new OptionsSkillHolder(parseInteger(cd.getAttributes(), "id"), parseInteger(cd.getAttributes(), "level"), parseDouble(cd.getAttributes(), "chance"), OptionsSkillType.ATTACK));
  101. break;
  102. }
  103. case "magic_skill":
  104. {
  105. op.addActivationSkill(new OptionsSkillHolder(parseInteger(cd.getAttributes(), "id"), parseInteger(cd.getAttributes(), "level"), parseDouble(cd.getAttributes(), "chance"), OptionsSkillType.MAGIC));
  106. break;
  107. }
  108. case "critical_skill":
  109. {
  110. op.addActivationSkill(new OptionsSkillHolder(parseInteger(cd.getAttributes(), "id"), parseInteger(cd.getAttributes(), "level"), parseDouble(cd.getAttributes(), "chance"), OptionsSkillType.CRITICAL));
  111. break;
  112. }
  113. }
  114. }
  115. _optionData.put(op.getId(), op);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. private void parseFuncs(NamedNodeMap attrs, String functionName, Options op)
  122. {
  123. Stats stat = Stats.valueOfXml(parseString(attrs, "stat"));
  124. double val = parseDouble(attrs, "val");
  125. int order = -1;
  126. final Node orderNode = attrs.getNamedItem("order");
  127. if (orderNode != null)
  128. {
  129. order = Integer.parseInt(orderNode.getNodeValue());
  130. }
  131. op.addFunc(new FuncTemplate(null, null, functionName, order, stat, val));
  132. }
  133. public Options getOptions(int id)
  134. {
  135. return _optionData.get(id);
  136. }
  137. /**
  138. * Gets the single instance of OptionsData.
  139. * @return single instance of OptionsData
  140. */
  141. public static final OptionData getInstance()
  142. {
  143. return SingletonHolder._instance;
  144. }
  145. private static class SingletonHolder
  146. {
  147. protected static final OptionData _instance = new OptionData();
  148. }
  149. }