SkillTable.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.gameserver.datatables;
  14. import com.l2jserver.gameserver.model.L2Skill;
  15. import com.l2jserver.gameserver.skills.SkillsEngine;
  16. import gnu.trove.TIntIntHashMap;
  17. import gnu.trove.TIntObjectHashMap;
  18. /**
  19. *
  20. */
  21. public class SkillTable
  22. {
  23. private final TIntObjectHashMap<L2Skill> _skills;
  24. private final TIntIntHashMap _skillMaxLevel;
  25. public static SkillTable getInstance()
  26. {
  27. return SingletonHolder._instance;
  28. }
  29. private SkillTable()
  30. {
  31. _skills = new TIntObjectHashMap<L2Skill>();
  32. _skillMaxLevel = new TIntIntHashMap();
  33. reload();
  34. }
  35. public void reload()
  36. {
  37. _skills.clear();
  38. SkillsEngine.getInstance().loadAllSkills(_skills);
  39. _skillMaxLevel.clear();
  40. for (final L2Skill skill : _skills.getValues(new L2Skill[_skills.size()]))
  41. {
  42. final int skillId = skill.getId();
  43. final int skillLvl = skill.getLevel();
  44. final int maxLvl = _skillMaxLevel.get(skillId);
  45. if (skillLvl > maxLvl)
  46. _skillMaxLevel.put(skillId, skillLvl);
  47. }
  48. }
  49. /**
  50. * Provides the skill hash
  51. *
  52. * @param skill
  53. * The L2Skill to be hashed
  54. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  55. */
  56. public static int getSkillHashCode(L2Skill skill)
  57. {
  58. return getSkillHashCode(skill.getId(), skill.getLevel());
  59. }
  60. /**
  61. * Centralized method for easier change of the hashing sys
  62. *
  63. * @param skillId
  64. * The Skill Id
  65. * @param skillLevel
  66. * The Skill Level
  67. * @return The Skill hash number
  68. */
  69. public static int getSkillHashCode(int skillId, int skillLevel)
  70. {
  71. return skillId * 1021 + skillLevel;
  72. }
  73. public final L2Skill getInfo(final int skillId, final int level)
  74. {
  75. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  76. if (result != null)
  77. return result;
  78. // skill/level not found, fix for transformation scripts
  79. final int maxLvl = _skillMaxLevel.get(skillId);
  80. // requested level too high
  81. if (maxLvl > 0 && level > maxLvl)
  82. return _skills.get(getSkillHashCode(skillId, maxLvl));
  83. return null;
  84. }
  85. public final int getMaxLevel(final int skillId)
  86. {
  87. return _skillMaxLevel.get(skillId);
  88. }
  89. /**
  90. * Returns an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  91. */
  92. public L2Skill[] getSiegeSkills(boolean addNoble)
  93. {
  94. L2Skill[] temp = null;
  95. if (addNoble)
  96. {
  97. temp = new L2Skill[3];
  98. temp[2] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  99. }
  100. else
  101. temp = new L2Skill[2];
  102. temp[0] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  103. temp[1] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  104. return temp;
  105. }
  106. @SuppressWarnings("synthetic-access")
  107. private static class SingletonHolder
  108. {
  109. protected static final SkillTable _instance = new SkillTable();
  110. }
  111. }