BaseStats.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. public final double calcBonus(L2Character actor)
  80. {
  81. return STRbonus[actor.getSTR()];
  82. }
  83. }
  84. private static final class INT implements BaseStat
  85. {
  86. public final double calcBonus(L2Character actor)
  87. {
  88. return INTbonus[actor.getINT()];
  89. }
  90. }
  91. private static final class DEX implements BaseStat
  92. {
  93. public final double calcBonus(L2Character actor)
  94. {
  95. return DEXbonus[actor.getDEX()];
  96. }
  97. }
  98. private static final class WIT implements BaseStat
  99. {
  100. public final double calcBonus(L2Character actor)
  101. {
  102. return WITbonus[actor.getWIT()];
  103. }
  104. }
  105. private static final class CON implements BaseStat
  106. {
  107. public final double calcBonus(L2Character actor)
  108. {
  109. return CONbonus[actor.getCON()];
  110. }
  111. }
  112. private static final class MEN implements BaseStat
  113. {
  114. public final double calcBonus(L2Character actor)
  115. {
  116. return MENbonus[actor.getMEN()];
  117. }
  118. }
  119. private static final class NULL implements BaseStat
  120. {
  121. public final double calcBonus(L2Character actor)
  122. {
  123. return 1f;
  124. }
  125. }
  126. static
  127. {
  128. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  129. factory.setValidating(false);
  130. factory.setIgnoringComments(true);
  131. final File file = new File(Config.DATAPACK_ROOT, "data/stats/statBonus.xml");
  132. Document doc = null;
  133. if (file.exists())
  134. {
  135. try
  136. {
  137. doc = factory.newDocumentBuilder().parse(file);
  138. }
  139. catch (Exception e)
  140. {
  141. _log.log(Level.WARNING, "[BaseStats] Could not parse file: " + e.getMessage(), e);
  142. }
  143. String statName;
  144. int val;
  145. double bonus;
  146. NamedNodeMap attrs;
  147. for (Node list = doc.getFirstChild(); list != null; list = list.getNextSibling())
  148. {
  149. if ("list".equalsIgnoreCase(list.getNodeName()))
  150. {
  151. for (Node stat = list.getFirstChild(); stat != null; stat = stat.getNextSibling())
  152. {
  153. statName = stat.getNodeName();
  154. for (Node value = stat.getFirstChild(); value != null; value = value.getNextSibling())
  155. {
  156. if ("stat".equalsIgnoreCase(value.getNodeName()))
  157. {
  158. attrs = value.getAttributes();
  159. try
  160. {
  161. val = Integer.parseInt(attrs.getNamedItem("value").getNodeValue());
  162. bonus = Double.parseDouble(attrs.getNamedItem("bonus").getNodeValue());
  163. }
  164. catch (Exception e)
  165. {
  166. _log.severe("[BaseStats] Invalid stats value: "+value.getNodeValue()+", skipping");
  167. continue;
  168. }
  169. if ("STR".equalsIgnoreCase(statName))
  170. STRbonus[val] = bonus;
  171. else if ("INT".equalsIgnoreCase(statName))
  172. INTbonus[val] = bonus;
  173. else if ("DEX".equalsIgnoreCase(statName))
  174. DEXbonus[val] = bonus;
  175. else if ("WIT".equalsIgnoreCase(statName))
  176. WITbonus[val] = bonus;
  177. else if ("CON".equalsIgnoreCase(statName))
  178. CONbonus[val] = bonus;
  179. else if ("MEN".equalsIgnoreCase(statName))
  180. MENbonus[val] = bonus;
  181. else
  182. _log.severe("[BaseStats] Invalid stats name: "+statName+", skipping");
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. else
  190. {
  191. throw new Error("[BaseStats] File not found: "+file.getName());
  192. }
  193. }
  194. }