Exemplo n.º 1
0
  /**
   * 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;
  }
Exemplo n.º 2
0
 /**
  * Takes mana away from the player for an unknown reason. This will not cause the player to fall
  * below 0 mana.
  *
  * @param amount amount of mana to take away
  */
 public void useMana(double amount) {
   useMana(amount, ManaCost.SPECIAL);
 }