ExperienceTable.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.datatables;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. /**
  28. * @author mrTJO
  29. */
  30. public class ExperienceTable
  31. {
  32. private static Logger _log = Logger.getLogger(ExperienceTable.class.getName());
  33. private byte MAX_LEVEL;
  34. private byte MAX_PET_LEVEL;
  35. private Map<Integer, Long> _expTable;
  36. public static ExperienceTable getInstance()
  37. {
  38. return SingletonHolder._instance;
  39. }
  40. private ExperienceTable()
  41. {
  42. loadTable();
  43. }
  44. private void loadTable()
  45. {
  46. File xml = new File(Config.DATAPACK_ROOT, "data/stats/experience.xml");
  47. Document doc = null;
  48. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  49. factory.setValidating(false);
  50. factory.setIgnoringComments(true);
  51. if (xml.exists())
  52. {
  53. try
  54. {
  55. doc = factory.newDocumentBuilder().parse(xml);
  56. }
  57. catch (IOException e)
  58. {
  59. _log.log(Level.WARNING, "Could not read experience.xml table: " + e.getMessage(), e);
  60. return;
  61. }
  62. catch (Exception e)
  63. {
  64. _log.log(Level.WARNING, "Could not parse experience.xml table: " + e.getMessage(), e);
  65. return;
  66. }
  67. Node table = doc.getFirstChild();
  68. NamedNodeMap tableAttr = table.getAttributes();
  69. MAX_LEVEL = (byte)(Byte.parseByte(tableAttr.getNamedItem("maxLevel").getNodeValue())+1);
  70. MAX_PET_LEVEL = (byte)(Byte.parseByte(tableAttr.getNamedItem("maxPetLevel").getNodeValue())+1);
  71. _expTable = new HashMap<Integer, Long>(MAX_LEVEL+1);
  72. for (Node experience = table.getFirstChild(); experience != null; experience = experience.getNextSibling())
  73. {
  74. if (experience.getNodeName().equals("experience"))
  75. {
  76. NamedNodeMap attrs = experience.getAttributes();
  77. int level = Integer.parseInt(attrs.getNamedItem("level").getNodeValue());
  78. long exp = Long.parseLong(attrs.getNamedItem("tolevel").getNodeValue());
  79. _expTable.put(level, exp);
  80. }
  81. }
  82. _log.info("ExperienceTable: Loaded "+_expTable.size()+" levels");
  83. _log.info("ExperienceTable: Max Player Level is: "+(MAX_LEVEL-1));
  84. _log.info("ExperienceTable: Max Pet Level is: "+(MAX_PET_LEVEL-1));
  85. }
  86. else
  87. _log.warning("ExperienceTable: experience.xml not found!");
  88. }
  89. public long getExpForLevel(int level)
  90. {
  91. return _expTable.get(level);
  92. }
  93. public byte getMaxLevel()
  94. {
  95. return MAX_LEVEL;
  96. }
  97. public byte getMaxPetLevel()
  98. {
  99. return MAX_PET_LEVEL;
  100. }
  101. @SuppressWarnings("synthetic-access")
  102. private static class SingletonHolder
  103. {
  104. protected static final ExperienceTable _instance = new ExperienceTable();
  105. }
  106. }