public boolean apply(SpellTargetEvent event) {
   Player player = event.getCaster();
   boolean check = condition.check(player, event.getTarget());
   if (check == false && type == ModifierType.REQUIRED) {
     event.setCancelled(true);
     return false;
   } else if (check == true && type == ModifierType.DENIED) {
     event.setCancelled(true);
     return false;
   } else if (check == true && type == ModifierType.STOP) {
     return false;
   } else if (check == false && type == ModifierType.CONTINUE) {
     return false;
   } else if (check == true && type == ModifierType.CAST) {
     Spell spell = MagicSpells.getSpellByInternalName(modifierVar);
     if (spell != null) {
       spell.cast(event.getCaster(), 1, null);
     }
   } else if (check == true && type == ModifierType.CAST_INSTEAD) {
     Spell spell = MagicSpells.getSpellByInternalName(modifierVar);
     if (spell != null) {
       if (spell instanceof TargetedEntitySpell) {
         ((TargetedEntitySpell) spell).castAtEntity(event.getCaster(), event.getTarget(), 1F);
       } else {
         spell.castSpell(event.getCaster(), SpellCastState.NORMAL, 1F, null);
       }
     }
   }
   return true;
 }
 public boolean apply(ManaChangeEvent event) {
   Player player = event.getPlayer();
   boolean check = condition.check(player);
   if (check == false && type == ModifierType.REQUIRED) {
     event.setNewAmount(event.getOldAmount());
     return false;
   } else if (check == true && type == ModifierType.DENIED) {
     event.setNewAmount(event.getOldAmount());
     return false;
   } else if (check == true && type == ModifierType.STOP) {
     return false;
   } else if (check == false && type == ModifierType.CONTINUE) {
     return false;
   } else if (check == true && type == ModifierType.POWER) {
     int gain = event.getNewAmount() - event.getOldAmount();
     gain = Math.round(gain * modifierVarFloat);
     int newAmt = event.getOldAmount() + gain;
     if (newAmt > event.getMaxMana()) newAmt = event.getMaxMana();
     event.setNewAmount(newAmt);
   } else if (check == true && type == ModifierType.ADD_POWER) {
     int newAmt = event.getNewAmount() + (int) modifierVarFloat;
     if (newAmt > event.getMaxMana()) newAmt = event.getMaxMana();
     event.setNewAmount(newAmt);
   } else if (check == true && (type == ModifierType.CAST || type == ModifierType.CAST_INSTEAD)) {
     Spell spell = MagicSpells.getSpellByInternalName(modifierVar);
     if (spell != null) {
       spell.cast(event.getPlayer(), 1, null);
     }
   }
   return true;
 }
 public boolean apply(SpellCastEvent event) {
   Player player = event.getCaster();
   boolean check = condition.check(player);
   if (check == false && type == ModifierType.REQUIRED) {
     event.setCancelled(true);
     return false;
   } else if (check == true && type == ModifierType.DENIED) {
     event.setCancelled(true);
     return false;
   } else if (check == true) {
     if (type == ModifierType.STOP) {
       return false;
     } else if (type == ModifierType.POWER) {
       event.increasePower(modifierVarFloat);
     } else if (type == ModifierType.ADD_POWER) {
       event.setPower(event.getPower() + modifierVarFloat);
     } else if (type == ModifierType.COOLDOWN) {
       event.setCooldown(modifierVarFloat);
     } else if (type == ModifierType.REAGENTS) {
       event.setReagents(event.getReagents().multiply(modifierVarFloat));
     } else if (type == ModifierType.CAST_TIME) {
       event.setCastTime(modifierVarInt);
     }
   } else if (check == false && type == ModifierType.CONTINUE) {
     return false;
   } else if (check == true && type == ModifierType.CAST) {
     Spell spell = MagicSpells.getSpellByInternalName(modifierVar);
     if (spell != null) {
       spell.cast(event.getCaster(), event.getPower(), event.getSpellArgs());
     }
   } else if (check == true && type == ModifierType.CAST_INSTEAD) {
     Spell spell = MagicSpells.getSpellByInternalName(modifierVar);
     if (spell != null) {
       spell.cast(event.getCaster(), event.getPower(), event.getSpellArgs());
     }
     event.setCancelled(true);
   }
   return true;
 }
  @Override
  public void initialize() {
    super.initialize();

    // create spell list
    spells = new ArrayList<Spell>();
    if (spellNames != null) {
      for (String spellName : spellNames) {
        Spell spell = MagicSpells.getSpellByInternalName(spellName);
        if (spell != null) {
          spells.add(spell);
        }
      }
    }
    if (spells.size() == 0) {
      MagicSpells.error("Passive spell '" + name + "' has no spells defined!");
      return;
    }

    // get trigger
    int trigCount = 0;
    if (triggers != null) {
      for (String strigger : triggers) {
        String type = strigger;
        String var = null;
        if (strigger.contains(" ")) {
          String[] data = Util.splitParams(strigger, 2);
          type = data[0];
          var = data[1];
        }
        type = type.toLowerCase();

        PassiveTrigger trigger = PassiveTrigger.getByName(type);
        if (trigger != null) {
          manager.registerSpell(this, trigger, var);
          trigCount++;
        } else {
          MagicSpells.error(
              "Invalid trigger '" + strigger + "' on passive spell '" + internalName + "'");
        }
      }
    }
    if (trigCount == 0) {
      MagicSpells.error("Passive spell '" + name + "' has no triggers defined!");
      return;
    }
  }
 private boolean activateSpells(
     Player caster, LivingEntity target, Location location, float basePower) {
   SpellCastState state = getCastState(caster);
   MagicSpells.debug(
       3,
       "Activating passive spell '"
           + name
           + "' for player "
           + caster.getName()
           + " (state: "
           + state
           + ")");
   if (!disabled
       && (chance >= .999 || random.nextFloat() <= chance)
       && state == SpellCastState.NORMAL) {
     disabled = true;
     SpellCastEvent event =
         new SpellCastEvent(
             this,
             caster,
             SpellCastState.NORMAL,
             basePower,
             null,
             this.cooldown,
             this.reagents.clone(),
             0);
     Bukkit.getPluginManager().callEvent(event);
     if (!event.isCancelled() && event.getSpellCastState() == SpellCastState.NORMAL) {
       if (event.haveReagentsChanged() && !hasReagents(caster, event.getReagents())) {
         disabled = false;
         return false;
       }
       setCooldown(caster, event.getCooldown());
       basePower = event.getPower();
       for (Spell spell : spells) {
         MagicSpells.debug(3, "    Casting spell effect '" + spell.getName() + "'");
         if (castWithoutTarget) {
           MagicSpells.debug(3, "    Casting without target");
           spell.castSpell(caster, SpellCastState.NORMAL, basePower, null);
           playSpellEffects(EffectPosition.CASTER, caster);
         } else if (spell instanceof TargetedEntitySpell
             && target != null
             && !isNonTargetedExternal(spell)) {
           MagicSpells.debug(3, "    Casting at entity");
           SpellTargetEvent targetEvent = new SpellTargetEvent(this, caster, target, basePower);
           Bukkit.getPluginManager().callEvent(targetEvent);
           if (!targetEvent.isCancelled()) {
             target = targetEvent.getTarget();
             ((TargetedEntitySpell) spell).castAtEntity(caster, target, targetEvent.getPower());
             playSpellEffects(caster, target);
           } else {
             MagicSpells.debug(3, "      Target cancelled (TE)");
           }
         } else if (spell instanceof TargetedLocationSpell
             && (location != null || target != null)) {
           MagicSpells.debug(3, "    Casting at location");
           Location loc = null;
           if (location != null) {
             loc = location;
           } else if (target != null) {
             loc = target.getLocation();
           }
           if (loc != null) {
             SpellTargetLocationEvent targetEvent =
                 new SpellTargetLocationEvent(this, caster, loc, basePower);
             Bukkit.getPluginManager().callEvent(targetEvent);
             if (!targetEvent.isCancelled()) {
               loc = targetEvent.getTargetLocation();
               ((TargetedLocationSpell) spell).castAtLocation(caster, loc, targetEvent.getPower());
               playSpellEffects(caster, loc);
             } else {
               MagicSpells.debug(3, "      Target cancelled (TL)");
             }
           }
         } else {
           MagicSpells.debug(3, "    Casting normally");
           float power = basePower;
           if (target != null) {
             SpellTargetEvent targetEvent = new SpellTargetEvent(this, caster, target, power);
             Bukkit.getPluginManager().callEvent(targetEvent);
             if (!targetEvent.isCancelled()) {
               power = targetEvent.getPower();
             } else {
               MagicSpells.debug(3, "      Target cancelled (UE)");
               continue;
             }
           } else if (location != null) {
             SpellTargetLocationEvent targetEvent =
                 new SpellTargetLocationEvent(this, caster, location, basePower);
             Bukkit.getPluginManager().callEvent(targetEvent);
             if (!targetEvent.isCancelled()) {
               power = targetEvent.getPower();
             } else {
               MagicSpells.debug(3, "      Target cancelled (UL)");
               continue;
             }
           }
           spell.castSpell(caster, SpellCastState.NORMAL, power, null);
           playSpellEffects(EffectPosition.CASTER, caster);
         }
       }
       removeReagents(caster, event.getReagents());
       sendMessage(caster, strCastSelf);
       SpellCastedEvent event2 =
           new SpellCastedEvent(
               this,
               caster,
               SpellCastState.NORMAL,
               basePower,
               null,
               event.getCooldown(),
               event.getReagents(),
               PostCastAction.HANDLE_NORMALLY);
       Bukkit.getPluginManager().callEvent(event2);
       disabled = false;
       return true;
     } else {
       MagicSpells.debug(3, "   Passive spell canceled");
       disabled = false;
       return false;
     }
   } else if (state != SpellCastState.NORMAL && sendFailureMessages) {
     if (state == SpellCastState.ON_COOLDOWN) {
       MagicSpells.sendMessage(
           caster, formatMessage(strOnCooldown, "%c", Math.round(getCooldown(caster)) + ""));
     } else if (state == SpellCastState.MISSING_REAGENTS) {
       MagicSpells.sendMessage(caster, strMissingReagents);
       if (MagicSpells.showStrCostOnMissingReagents() && strCost != null && !strCost.isEmpty()) {
         MagicSpells.sendMessage(caster, "    (" + strCost + ")");
       }
     }
   }
   return false;
 }