ExperienceData.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright © 2004-2019 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.data.json;
  20. import static com.l2jserver.gameserver.config.Configuration.server;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.io.FileReader;
  24. import java.io.IOException;
  25. import java.lang.reflect.Type;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import com.google.gson.Gson;
  31. import com.google.gson.reflect.TypeToken;
  32. import com.google.gson.stream.JsonReader;
  33. /**
  34. * @author Zealar
  35. * @since 2.6.0.0
  36. */
  37. public final class ExperienceData {
  38. private static final Logger LOG = LoggerFactory.getLogger(ExperienceData.class);
  39. private static final Gson GSON = new Gson();
  40. private static final Type TYPE_MAP_INTEGER_LONG = new TypeToken<Map<Integer, Long>>() {
  41. }.getType();
  42. private final Map<Integer, Long> _expTable = new HashMap<>();
  43. ExperienceData() {
  44. load();
  45. }
  46. public void load() {
  47. _expTable.clear();
  48. try (JsonReader reader = new JsonReader(new FileReader(new File(server().getDatapackRoot(), "data/stats/expData.json")))) {
  49. _expTable.putAll(GSON.fromJson(reader, TYPE_MAP_INTEGER_LONG));
  50. } catch (FileNotFoundException fnfe) {
  51. LOG.warn("data/stats/expData.json not found!");
  52. } catch (IOException ioe) {
  53. LOG.warn("Failed to load expData.json for: ", ioe);
  54. }
  55. }
  56. /**
  57. * Gets the exp for level.
  58. * @param level the level required.
  59. * @return the experience points required to reach the given level.
  60. */
  61. public long getExpForLevel(int level) {
  62. return _expTable.get(level);
  63. }
  64. public float getPercentFromCurrentLevel(long exp, int level) {
  65. long expPerLevel = getExpForLevel(level);
  66. long expPerLevel2 = getExpForLevel(level + 1);
  67. return (float) (exp - expPerLevel) / (expPerLevel2 - expPerLevel);
  68. }
  69. /**
  70. * Gets the single instance of ExperienceTable.
  71. * @return single instance of ExperienceTable
  72. */
  73. public static ExperienceData getInstance() {
  74. return SingletonHolder._instance;
  75. }
  76. private static class SingletonHolder {
  77. static final ExperienceData _instance = new ExperienceData();
  78. }
  79. }