/** * Binds a skill to a material for the player. The bind will not work if the skill was already * bound to the material. * * @param mat material to bind the skill to * @param skill skill to bind to the material * @return true if was able to bind the skill, false otherwise */ public boolean bind(Material mat, PlayerSkill skill) { // Special cases if (mat == null || (skill != null && skill.getPlayerData() != this)) { return false; } PlayerSkill bound = getBoundSkill(mat); if (bound != skill) { // Apply the binding if (skill == null) { binds.remove(mat); } else { binds.put(mat, skill); } // Update the old skill's bind if (bound != null) { bound.setBind(null); } // Update the new skill's bind if (skill != null) { skill.setBind(mat); } return true; } // The skill was already bound else { return false; } }
/** * Clears the skill binding for the given skill. This will remove the bindings on all materials * involving the skill. * * @param skill skill to unbind */ public void clearBinds(Skill skill) { ArrayList<Material> keys = new ArrayList<Material>(binds.keySet()); for (Material key : keys) { PlayerSkill bound = binds.get(key); if (bound.getData() == skill) { binds.remove(key); } } }
/** * Stops passive abilities for the player if they are online. This is already called by the API * and shouldn't be called by other plugins. * * @param player player to stop the passive skills for */ public void stopPassives(Player player) { if (player == null) { return; } for (PlayerSkill skill : skills.values()) { if (skill.isUnlocked() && (skill.getData() instanceof PassiveSkill)) { ((PassiveSkill) skill.getData()).stopEffects(player, skill.getLevel()); } } }
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++; } }
/** * 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; } }
/** * 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; } }
/** Attempts to auto-level any skills that are able to do so */ public void autoLevel() { for (PlayerSkill skill : skills.values()) { autoLevel(skill.getData()); } }
/** * Retrieves the level of a skill for the owner. This is not case-sensitive. * * @param name name of the skill * @return level of the skill or 0 if not found */ public int getSkillLevel(String name) { PlayerSkill skill = getSkill(name); return skill == null ? 0 : skill.getLevel(); }
/** * Casts a skill for the player. In order to cast the skill, the player must be online, have the * skill unlocked, have enough mana, have the skill off cooldown, and have a proper target if * applicable. * * @param skill skill to cast * @return true if successfully cast the skill, false otherwise */ public boolean cast(PlayerSkill skill) { // Invalid skill if (skill == null) { throw new IllegalArgumentException("Skill cannot be null"); } SkillStatus status = skill.getStatus(); int level = skill.getLevel(); double cost = skill.getData().getManaCost(level); // Not unlocked if (level <= 0) { return false; } // On Cooldown if (status == SkillStatus.ON_COOLDOWN) { SkillAPI.getLanguage() .sendMessage( ErrorNodes.COOLDOWN, getPlayer(), FilterType.COLOR, RPGFilter.COOLDOWN.setReplacement(skill.getCooldown() + ""), RPGFilter.SKILL.setReplacement(skill.getData().getName())); } // Not enough mana else if (status == SkillStatus.MISSING_MANA) { SkillAPI.getLanguage() .sendMessage( ErrorNodes.MANA, getPlayer(), FilterType.COLOR, RPGFilter.SKILL.setReplacement(skill.getData().getName()), RPGFilter.MANA.setReplacement(getMana() + ""), RPGFilter.COST.setReplacement((int) Math.ceil(cost) + ""), RPGFilter.MISSING.setReplacement((int) Math.ceil(cost - getMana()) + "")); } // Skill Shots else if (skill.getData() instanceof SkillShot) { Player p = getPlayer(); PlayerCastSkillEvent event = new PlayerCastSkillEvent(this, skill, p); Bukkit.getPluginManager().callEvent(event); // Make sure it isn't cancelled if (!event.isCancelled()) { try { if (((SkillShot) skill.getData()).cast(p, level)) { skill.startCooldown(); if (SkillAPI.getSettings().isShowSkillMessages()) { skill.getData().sendMessage(p, SkillAPI.getSettings().getMessageRadius()); } if (SkillAPI.getSettings().isManaEnabled()) { useMana(cost, ManaCost.SKILL_CAST); } return true; } } catch (Exception ex) { Bukkit.getLogger() .severe( "Failed to cast skill - " + skill.getData().getName() + ": Internal skill error"); ex.printStackTrace(); } } } // Target Skills else if (skill.getData() instanceof TargetSkill) { Player p = getPlayer(); LivingEntity target = TargetHelper.getLivingTarget(p, skill.getData().getRange(level)); // Must have a target if (target == null) { return false; } PlayerCastSkillEvent event = new PlayerCastSkillEvent(this, skill, p); Bukkit.getPluginManager().callEvent(event); // Make sure it isn't cancelled if (!event.isCancelled()) { try { if (((TargetSkill) skill.getData()) .cast(p, target, level, !SkillAPI.getSettings().canAttack(p, target))) { skill.startCooldown(); if (SkillAPI.getSettings().isShowSkillMessages()) { skill.getData().sendMessage(p, SkillAPI.getSettings().getMessageRadius()); } if (SkillAPI.getSettings().isManaEnabled()) { useMana(cost, ManaCost.SKILL_CAST); } return true; } } catch (Exception ex) { Bukkit.getLogger() .severe( "Failed to cast skill - " + skill.getData().getName() + ": Internal skill error"); ex.printStackTrace(); } } } return false; }