ExperienceTable.java 3.5 KB

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