BaseStats.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 java.io.File;
  17. import java.util.NoSuchElementException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.xml.parsers.DocumentBuilderFactory;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.NamedNodeMap;
  23. import org.w3c.dom.Node;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. /**
  27. *
  28. * @author DS
  29. *
  30. */
  31. public enum BaseStats
  32. {
  33. STR(new STR()),
  34. INT(new INT()),
  35. DEX(new DEX()),
  36. WIT(new WIT()),
  37. CON(new CON()),
  38. MEN(new MEN()),
  39. NULL(new NULL());
  40. protected static final Logger _log = Logger.getLogger(BaseStats.class.getName());
  41. public static final int MAX_STAT_VALUE = 100;
  42. private static final double[] STRbonus = new double[MAX_STAT_VALUE];
  43. private static final double[] INTbonus = new double[MAX_STAT_VALUE];
  44. private static final double[] DEXbonus = new double[MAX_STAT_VALUE];
  45. private static final double[] WITbonus = new double[MAX_STAT_VALUE];
  46. private static final double[] CONbonus = new double[MAX_STAT_VALUE];
  47. private static final double[] MENbonus = new double[MAX_STAT_VALUE];
  48. private final BaseStat _stat;
  49. public final String getValue()
  50. {
  51. return _stat.getClass().getSimpleName();
  52. }
  53. private BaseStats(BaseStat s)
  54. {
  55. _stat = s;
  56. }
  57. public final double calcBonus(L2Character actor)
  58. {
  59. if (actor != null)
  60. return _stat.calcBonus(actor);
  61. return 1;
  62. }
  63. public static final BaseStats valueOfXml(String name)
  64. {
  65. name = name.intern();
  66. for (BaseStats s : values())
  67. {
  68. if (s.getValue().equalsIgnoreCase(name))
  69. return s;
  70. }
  71. throw new NoSuchElementException("Unknown name '" + name + "' for enum BaseStats");
  72. }
  73. private interface BaseStat
  74. {
  75. public double calcBonus(L2Character actor);
  76. }
  77. private static final class STR implements BaseStat
  78. {
  79. @Override
  80. public final double calcBonus(L2Character actor)
  81. {
  82. return STRbonus[actor.getSTR()];
  83. }
  84. }
  85. private static final class INT implements BaseStat
  86. {
  87. @Override
  88. public final double calcBonus(L2Character actor)
  89. {
  90. return INTbonus[actor.getINT()];
  91. }
  92. }
  93. private static final class DEX implements BaseStat
  94. {
  95. @Override
  96. public final double calcBonus(L2Character actor)
  97. {
  98. return DEXbonus[actor.getDEX()];
  99. }
  100. }
  101. private static final class WIT implements BaseStat
  102. {
  103. @Override
  104. public final double calcBonus(L2Character actor)
  105. {
  106. return WITbonus[actor.getWIT()];
  107. }
  108. }
  109. private static final class CON implements BaseStat
  110. {
  111. @Override
  112. public final double calcBonus(L2Character actor)
  113. {
  114. return CONbonus[actor.getCON()];
  115. }
  116. }
  117. private static final class MEN implements BaseStat
  118. {
  119. @Override
  120. public final double calcBonus(L2Character actor)
  121. {
  122. return MENbonus[actor.getMEN()];
  123. }
  124. }
  125. private static final class NULL implements BaseStat
  126. {
  127. @Override
  128. public final double calcBonus(L2Character actor)
  129. {
  130. return 1f;
  131. }
  132. }
  133. static
  134. {
  135. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  136. factory.setValidating(false);
  137. factory.setIgnoringComments(true);
  138. final File file = new File(Config.DATAPACK_ROOT, "data/stats/statBonus.xml");
  139. Document doc = null;
  140. if (file.exists())
  141. {
  142. try
  143. {
  144. doc = factory.newDocumentBuilder().parse(file);
  145. }
  146. catch (Exception e)
  147. {
  148. _log.log(Level.WARNING, "[BaseStats] Could not parse file: " + e.getMessage(), e);
  149. }
  150. String statName;
  151. int val;
  152. double bonus;
  153. NamedNodeMap attrs;
  154. for (Node list = doc.getFirstChild(); list != null; list = list.getNextSibling())
  155. {
  156. if ("list".equalsIgnoreCase(list.getNodeName()))
  157. {
  158. for (Node stat = list.getFirstChild(); stat != null; stat = stat.getNextSibling())
  159. {
  160. statName = stat.getNodeName();
  161. for (Node value = stat.getFirstChild(); value != null; value = value.getNextSibling())
  162. {
  163. if ("stat".equalsIgnoreCase(value.getNodeName()))
  164. {
  165. attrs = value.getAttributes();
  166. try
  167. {
  168. val = Integer.parseInt(attrs.getNamedItem("value").getNodeValue());
  169. bonus = Double.parseDouble(attrs.getNamedItem("bonus").getNodeValue());
  170. }
  171. catch (Exception e)
  172. {
  173. _log.severe("[BaseStats] Invalid stats value: "+value.getNodeValue()+", skipping");
  174. continue;
  175. }
  176. if ("STR".equalsIgnoreCase(statName))
  177. STRbonus[val] = bonus;
  178. else if ("INT".equalsIgnoreCase(statName))
  179. INTbonus[val] = bonus;
  180. else if ("DEX".equalsIgnoreCase(statName))
  181. DEXbonus[val] = bonus;
  182. else if ("WIT".equalsIgnoreCase(statName))
  183. WITbonus[val] = bonus;
  184. else if ("CON".equalsIgnoreCase(statName))
  185. CONbonus[val] = bonus;
  186. else if ("MEN".equalsIgnoreCase(statName))
  187. MENbonus[val] = bonus;
  188. else
  189. _log.severe("[BaseStats] Invalid stats name: "+statName+", skipping");
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. else
  197. {
  198. throw new Error("[BaseStats] File not found: "+file.getName());
  199. }
  200. }
  201. }