BaseStats.java 6.1 KB

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