123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.datatables;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.Config;
- import com.l2jserver.gameserver.data.xml.impl.SkillTreesData;
- import com.l2jserver.gameserver.engines.DocumentEngine;
- import com.l2jserver.gameserver.model.skills.Skill;
- /**
- * Skill data.
- */
- public final class SkillData
- {
- private static Logger LOGGER = Logger.getLogger(SkillData.class.getName());
-
- private final Map<Integer, Skill> _skills = new HashMap<>();
- private final Map<Integer, Integer> _skillMaxLevel = new HashMap<>();
- private final Set<Integer> _enchantable = new HashSet<>();
-
- protected SkillData()
- {
- load();
- }
-
- public void reload()
- {
- load();
- // Reload Skill Tree as well.
- SkillTreesData.getInstance().load();
- }
-
- private void load()
- {
- final Map<Integer, Skill> _temp = new HashMap<>();
- DocumentEngine.getInstance().loadAllSkills(_temp);
-
- _skills.clear();
- _skills.putAll(_temp);
-
- _skillMaxLevel.clear();
- _enchantable.clear();
- for (Skill skill : _skills.values())
- {
- final int skillId = skill.getId();
- final int skillLvl = skill.getLevel();
- if (skillLvl > 99)
- {
- if (!_enchantable.contains(skillId))
- {
- _enchantable.add(skillId);
- }
- continue;
- }
-
- // only non-enchanted skills
- final int maxLvl = getMaxLevel(skillId);
- if (skillLvl > maxLvl)
- {
- _skillMaxLevel.put(skillId, skillLvl);
- }
- }
- }
-
- /**
- * Provides the skill hash
- * @param skill The L2Skill to be hashed
- * @return getSkillHashCode(skill.getId(), skill.getLevel())
- */
- public static int getSkillHashCode(Skill skill)
- {
- return getSkillHashCode(skill.getId(), skill.getLevel());
- }
-
- /**
- * Centralized method for easier change of the hashing sys
- * @param skillId The Skill Id
- * @param skillLevel The Skill Level
- * @return The Skill hash number
- */
- public static int getSkillHashCode(int skillId, int skillLevel)
- {
- return (skillId * 1021) + skillLevel;
- }
-
- public Skill getSkill(int skillId, int level)
- {
- final Skill result = _skills.get(getSkillHashCode(skillId, level));
- if (result != null)
- {
- return result;
- }
-
- // skill/level not found, fix for transformation scripts
- final int maxLvl = getMaxLevel(skillId);
- // requested level too high
- if ((maxLvl > 0) && (level > maxLvl))
- {
- if (Config.DEBUG)
- {
- LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
- }
- return _skills.get(getSkillHashCode(skillId, maxLvl));
- }
-
- LOGGER.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
- return null;
- }
-
- public int getMaxLevel(int skillId)
- {
- final Integer maxLevel = _skillMaxLevel.get(skillId);
- return maxLevel != null ? maxLevel : 0;
- }
-
- /**
- * Verifies if the given skill ID correspond to an enchantable skill.
- * @param skillId the skill ID
- * @return {@code true} if the skill is enchantable, {@code false} otherwise
- */
- public boolean isEnchantable(int skillId)
- {
- return _enchantable.contains(skillId);
- }
-
- /**
- * @param addNoble
- * @param hasCastle
- * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
- */
- public Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
- {
- Skill[] temp = new Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
- int i = 0;
- temp[i++] = _skills.get(SkillData.getSkillHashCode(246, 1));
- temp[i++] = _skills.get(SkillData.getSkillHashCode(247, 1));
-
- if (addNoble)
- {
- temp[i++] = _skills.get(SkillData.getSkillHashCode(326, 1));
- }
- if (hasCastle)
- {
- temp[i++] = _skills.get(SkillData.getSkillHashCode(844, 1));
- temp[i++] = _skills.get(SkillData.getSkillHashCode(845, 1));
- }
- return temp;
- }
-
- public static SkillData getInstance()
- {
- return SingletonHolder._instance;
- }
-
- private static class SingletonHolder
- {
- protected static final SkillData _instance = new SkillData();
- }
- }
|