BaseStats.java 6.1 KB

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