/** * Upgrades a skill owned by the player. The player must own the skill, have enough skill points, * meet the level and skill requirements, and not have maxed out the skill already in order to * upgrade the skill. This will consume the skill point cost while upgrading the skill. * * @param skill skill to upgrade * @return true if successfully was upgraded, false otherwise */ public boolean upgradeSkill(Skill skill) { // Cannot be null if (skill == null) { return false; } // Must be a valid available skill PlayerSkill data = skills.get(skill.getName().toLowerCase()); if (data == null) { return false; } // Must meet any skill requirements if (skill.getSkillReq() != null) { PlayerSkill req = skills.get(skill.getSkillReq().toLowerCase()); if (req != null && req.getLevel() < skill.getSkillReqLevel()) { return false; } } int level = data.getPlayerClass().getLevel(); int points = data.getPlayerClass().getPoints(); int cost = data.getCost(); if (!data.isMaxed() && level >= data.getLevelReq() && points >= cost) { // Upgrade event PlayerSkillUpgradeEvent event = new PlayerSkillUpgradeEvent(this, data, cost); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return false; } // Apply upgrade data.getPlayerClass().usePoints(cost); data.addLevels(1); // Passive calls Player player = getPlayer(); if (player != null && skill instanceof PassiveSkill) { if (data.getLevel() == 1) { ((PassiveSkill) skill).initialize(player, data.getLevel()); } else { ((PassiveSkill) skill).update(player, data.getLevel() - 1, data.getLevel()); } } // Unlock event if (data.getLevel() == 1) { Bukkit.getPluginManager().callEvent(new PlayerSkillUnlockEvent(this, data)); this.autoLevel(); } return true; } else { return false; } }
/** * Downgrades a skill owned by the player. The player must own the skill and it must not currently * be level 0 for the player to downgrade the skill. This will refund the skill point cost when * downgrading the skill. * * @param skill skill to downgrade * @return true if successfully downgraded, false otherwise */ public boolean downgradeSkill(Skill skill) { // Cannot be null if (skill == null) { return false; } // Must be a valid available skill PlayerSkill data = skills.get(skill.getName().toLowerCase()); if (data == null) { return false; } // Must not be a free skill if (data.getCost() == 0) { return false; } // Must not be required by another skill for (PlayerSkill s : skills.values()) { if (s.getData().getSkillReq() != null && s.getData().getSkillReq().equalsIgnoreCase(skill.getName()) && data.getLevel() <= s.getData().getSkillReqLevel()) { return false; } } int cost = skill.getCost(data.getLevel() - 1); if (data.getLevel() > 0) { // Upgrade event PlayerSkillDowngradeEvent event = new PlayerSkillDowngradeEvent(this, data, cost); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return false; } // Apply upgrade data.getPlayerClass().givePoints(cost, PointSource.REFUND); data.addLevels(-1); // Passive calls Player player = getPlayer(); if (player != null && skill instanceof PassiveSkill) { if (data.getLevel() == 0) { ((PassiveSkill) skill).stopEffects(player, 1); } else { ((PassiveSkill) skill).update(player, data.getLevel() + 1, data.getLevel()); } } // Clear bindings if (data.getLevel() == 0) { clearBinds(skill); } return true; } else { return false; } }
private void autoLevel(Skill skill) { PlayerSkill data = skills.get(skill.getKey()); if (data == null) return; if (!SkillAPI.isLoaded()) return; int lastLevel = data.getLevel(); while (data.getData().canAutoLevel() && !data.isMaxed() && data.getLevelReq() <= data.getPlayerClass().getLevel()) { upgradeSkill(skill); if (lastLevel == data.getLevel()) { break; } lastLevel++; } }