Search in sources :

Example 1 with SpellBatch

use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.

the class BaseSpell method cast.

@Override
public boolean cast(ConfigurationSection extraParameters, Location defaultLocation) {
    if (mage.isPlayer() && mage.getPlayer().getGameMode() == GameMode.SPECTATOR) {
        if (mage.getDebugLevel() > 0 && extraParameters != null) {
            mage.sendDebugMessage("Cannot cast in spectator mode.");
        }
        return false;
    }
    if (toggle != ToggleType.NONE && isActive()) {
        mage.sendDebugMessage(ChatColor.DARK_BLUE + "Deactivating " + ChatColor.GOLD + getName());
        deactivate(true, true);
        processResult(SpellResult.DEACTIVATE, parameters);
        return true;
    }
    if (mage.getDebugLevel() > 5 && extraParameters != null) {
        Collection<String> keys = extraParameters.getKeys(false);
        if (keys.size() > 0) {
            mage.sendDebugMessage(ChatColor.BLUE + "Cast " + ChatColor.GOLD + getName() + " " + ChatColor.GREEN + ConfigurationUtils.getParameters(extraParameters));
        }
    }
    this.reset();
    Location location = mage.getLocation();
    if (location != null) {
        Set<String> overrides = controller.getSpellOverrides(mage, location);
        if (overrides != null && !overrides.isEmpty()) {
            if (extraParameters == null) {
                extraParameters = new MemoryConfiguration();
            }
            for (String entry : overrides) {
                String[] pieces = StringUtils.split(entry, ' ');
                if (pieces.length < 2)
                    continue;
                String fullKey = pieces[0];
                String[] key = StringUtils.split(fullKey, '.');
                if (key.length == 0)
                    continue;
                if (key.length == 2 && !key[0].equals("default") && !key[0].equals(spellKey.getBaseKey()) && !key[0].equals(spellKey.getKey())) {
                    continue;
                }
                fullKey = key.length == 2 ? key[1] : key[0];
                ConfigurationUtils.set(extraParameters, fullKey, pieces[1]);
            }
        }
    }
    if (this.currentCast == null) {
        this.currentCast = new CastContext();
        this.currentCast.setSpell(this);
    }
    this.location = defaultLocation;
    workingParameters = new SpellParameters(this);
    ConfigurationUtils.addConfigurations(workingParameters, this.parameters);
    ConfigurationUtils.addConfigurations(workingParameters, extraParameters);
    processParameters(workingParameters);
    // Check to see if this is allowed to be cast by a command block
    if (!commandBlockAllowed) {
        CommandSender sender = mage.getCommandSender();
        if (sender != null && sender instanceof BlockCommandSender) {
            Block block = mage.getLocation().getBlock();
            if (block.getType() == Material.COMMAND) {
                block.setType(Material.AIR);
            }
            return false;
        }
    }
    // Allow other plugins to cancel this cast
    PreCastEvent preCast = new PreCastEvent(mage, this);
    Bukkit.getPluginManager().callEvent(preCast);
    if (preCast.isCancelled()) {
        processResult(SpellResult.CANCELLED, workingParameters);
        sendCastMessage(SpellResult.CANCELLED, " (no cast)");
        return false;
    }
    // Don't allow casting if the player is confused or weakened
    bypassConfusion = workingParameters.getBoolean("bypass_confusion", bypassConfusion);
    bypassWeakness = workingParameters.getBoolean("bypass_weakness", bypassWeakness);
    LivingEntity livingEntity = mage.getLivingEntity();
    if (livingEntity != null && !mage.isSuperPowered()) {
        if (!bypassConfusion && livingEntity.hasPotionEffect(PotionEffectType.CONFUSION)) {
            processResult(SpellResult.CURSED, workingParameters);
            sendCastMessage(SpellResult.CURSED, " (no cast)");
            return false;
        }
        // Don't allow casting if the player is weakened
        if (!bypassWeakness && livingEntity.hasPotionEffect(PotionEffectType.WEAKNESS)) {
            processResult(SpellResult.CURSED, workingParameters);
            sendCastMessage(SpellResult.CURSED, " (no cast)");
            return false;
        }
    }
    // Don't perform permission check until after processing parameters, in case of overrides
    if (!canCast(getLocation())) {
        processResult(SpellResult.INSUFFICIENT_PERMISSION, workingParameters);
        sendCastMessage(SpellResult.INSUFFICIENT_PERMISSION, " (no cast)");
        if (mage.getDebugLevel() > 1) {
            CommandSender messageTarget = mage.getDebugger();
            if (messageTarget == null) {
                messageTarget = mage.getCommandSender();
            }
            if (messageTarget != null) {
                mage.debugPermissions(messageTarget, this);
            }
        }
        return false;
    }
    this.preCast();
    // PVP override settings
    bypassPvpRestriction = workingParameters.getBoolean("bypass_pvp", false);
    bypassPvpRestriction = workingParameters.getBoolean("bp", bypassPvpRestriction);
    bypassPermissions = workingParameters.getBoolean("bypass_permissions", bypassPermissions);
    bypassFriendlyFire = workingParameters.getBoolean("bypass_friendly_fire", false);
    onlyFriendlyFire = workingParameters.getBoolean("only_friendly", false);
    // Check cooldowns
    cooldown = workingParameters.getInt("cooldown", cooldown);
    cooldown = workingParameters.getInt("cool", cooldown);
    mageCooldown = workingParameters.getInt("cooldown_mage", mageCooldown);
    // Color override
    color = ConfigurationUtils.getColor(workingParameters, "color", color);
    particle = workingParameters.getString("particle", null);
    double cooldownRemaining = getRemainingCooldown() / 1000.0;
    String timeDescription = "";
    if (cooldownRemaining > 0) {
        if (cooldownRemaining > 60 * 60) {
            long hours = (long) Math.ceil(cooldownRemaining / (60 * 60));
            if (hours == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_hour");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_hours").replace("$hours", Long.toString(hours));
            }
        } else if (cooldownRemaining > 60) {
            long minutes = (long) Math.ceil(cooldownRemaining / 60);
            if (minutes == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_minute");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_minutes").replace("$minutes", Long.toString(minutes));
            }
        } else if (cooldownRemaining >= 1) {
            long seconds = (long) Math.ceil(cooldownRemaining);
            if (seconds == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_second");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_seconds").replace("$seconds", Long.toString(seconds));
            }
        } else {
            timeDescription = controller.getMessages().get("cooldown.wait_moment");
            if (timeDescription.contains("$seconds")) {
                timeDescription = timeDescription.replace("$seconds", SECONDS_FORMATTER.format(cooldownRemaining));
            }
        }
        castMessage(getMessage("cooldown").replace("$time", timeDescription));
        processResult(SpellResult.COOLDOWN, workingParameters);
        sendCastMessage(SpellResult.COOLDOWN, " (no cast)");
        return false;
    }
    CastingCost required = getRequiredCost();
    if (required != null) {
        String baseMessage = getMessage("insufficient_resources");
        String costDescription = required.getDescription(controller.getMessages(), mage);
        // Send loud messages when items are required.
        if (required.isItem()) {
            sendMessage(baseMessage.replace("$cost", costDescription));
        } else {
            castMessage(baseMessage.replace("$cost", costDescription));
        }
        processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
        sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
        return false;
    }
    if (requiredHealth > 0) {
        LivingEntity li = mage.getLivingEntity();
        double healthPercentage = li == null ? 0 : 100 * li.getHealth() / li.getMaxHealth();
        if (healthPercentage < requiredHealth) {
            processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
            sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
            return false;
        }
    }
    if (controller.isSpellProgressionEnabled()) {
        long progressLevel = getProgressLevel();
        for (Entry<String, EquationTransform> entry : progressLevelEquations.entrySet()) {
            workingParameters.set(entry.getKey(), entry.getValue().get(progressLevel));
        }
    }
    // Check for cancel-on-cast-other spells, after we have determined that this spell can really be cast.
    if (!passive) {
        for (Iterator<Batch> iterator = mage.getPendingBatches().iterator(); iterator.hasNext(); ) {
            Batch batch = iterator.next();
            if (!(batch instanceof SpellBatch))
                continue;
            SpellBatch spellBatch = (SpellBatch) batch;
            Spell spell = spellBatch.getSpell();
            if (spell.cancelOnCastOther()) {
                spell.cancel();
                batch.finish();
                iterator.remove();
            }
        }
    }
    return finalizeCast(workingParameters);
}
Also used : EquationTransform(de.slikey.effectlib.math.EquationTransform) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) SpellParameters(com.elmakers.mine.bukkit.magic.SpellParameters) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) PreCastEvent(com.elmakers.mine.bukkit.api.event.PreCastEvent) Spell(com.elmakers.mine.bukkit.api.spell.Spell) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) LivingEntity(org.bukkit.entity.LivingEntity) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) CastContext(com.elmakers.mine.bukkit.action.CastContext) Batch(com.elmakers.mine.bukkit.api.batch.Batch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Block(org.bukkit.block.Block) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 2 with SpellBatch

use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.

the class Mage method cancelPending.

@Nullable
@Override
public Batch cancelPending(String spellKey, boolean force) {
    Batch stoppedPending = null;
    if (pendingBatches.size() > 0) {
        List<Batch> batches = new ArrayList<>();
        batches.addAll(pendingBatches);
        for (Batch batch : batches) {
            if (spellKey != null || !force) {
                if (!(batch instanceof SpellBatch)) {
                    continue;
                }
                SpellBatch spellBatch = (SpellBatch) batch;
                Spell spell = spellBatch.getSpell();
                if (spell == null) {
                    continue;
                }
                if (!force && !spell.isCancellable()) {
                    continue;
                }
                if (spellKey != null && !spell.getSpellKey().getBaseKey().equalsIgnoreCase(spellKey)) {
                    continue;
                }
            }
            if (!(batch instanceof UndoBatch)) {
                if (batch instanceof SpellBatch) {
                    SpellBatch spellBatch = (SpellBatch) batch;
                    Spell spell = spellBatch.getSpell();
                    if (spell != null) {
                        spell.cancel();
                    }
                }
                batch.finish();
                pendingBatches.remove(batch);
                stoppedPending = batch;
            }
        }
    }
    return stoppedPending;
}
Also used : SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Batch(com.elmakers.mine.bukkit.api.batch.Batch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) ArrayList(java.util.ArrayList) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Nullable(javax.annotation.Nullable)

Example 3 with SpellBatch

use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.

the class Mage method onDamage.

public void onDamage(EntityDamageEvent event) {
    String damageType = currentDamageType;
    currentDamageType = null;
    LivingEntity entity = getLivingEntity();
    if (entity == null) {
        return;
    }
    // Send on to any registered spells
    List<Listener> active = new ArrayList<>(damageListeners);
    for (Listener listener : active) {
        callEvent(listener, event);
        if (event.isCancelled())
            break;
    }
    EntityDamageEvent.DamageCause cause = event.getCause();
    if (cause == EntityDamageEvent.DamageCause.FALL) {
        if (fallProtectionCount > 0 && fallProtection > 0 && fallProtection > System.currentTimeMillis()) {
            event.setCancelled(true);
            fallProtectionCount--;
            if (fallingSpell != null) {
                double scale = 1;
                LivingEntity li = getLivingEntity();
                if (li != null) {
                    scale = event.getDamage() / li.getMaxHealth();
                }
                fallingSpell.playEffects("land", (float) scale, getLocation().getBlock().getRelative(BlockFace.DOWN));
            }
            if (fallProtectionCount <= 0) {
                fallProtection = 0;
                fallingSpell = null;
            }
            return;
        } else {
            fallingSpell = null;
        }
    }
    if (isSuperProtected()) {
        event.setCancelled(true);
        if (entity.getFireTicks() > 0) {
            entity.setFireTicks(0);
        }
        return;
    }
    if (event.isCancelled()) {
        return;
    }
    // First check for damage reduction
    double reduction = 0;
    Double overallProtection = protection.get("overall");
    if (overallProtection != null) {
        reduction = overallProtection * controller.getMaxDamageReduction("overall");
    }
    // Apply weaknesses
    double multiplier = 1;
    Double overallWeakness = weakness.get("overall");
    if (overallWeakness != null && overallWeakness > 0) {
        double defendMultiplier = controller.getMaxDefendMultiplier("overall");
        if (defendMultiplier > 1) {
            defendMultiplier = 1 + (defendMultiplier - 1) * overallWeakness;
            multiplier *= defendMultiplier;
        }
    }
    if (cause == EntityDamageEvent.DamageCause.FIRE_TICK) {
        // Also put out fire if they have maxed out fire protection.
        double damageReductionFire = getProtection("fire");
        if (damageReductionFire >= 1 && entity.getFireTicks() > 0) {
            entity.setFireTicks(0);
        }
    }
    if (damageType == null) {
        switch(cause) {
            case CONTACT:
            case ENTITY_ATTACK:
                damageType = "physical";
                break;
            case FIRE:
            case FIRE_TICK:
            case LAVA:
                damageType = "fire";
                break;
            case BLOCK_EXPLOSION:
            case ENTITY_EXPLOSION:
                damageType = "explosion";
                break;
            default:
                damageType = cause.name().toLowerCase();
                break;
        }
    }
    lastDamageType = damageType;
    double protection = getProtection(damageType);
    double maxReduction = controller.getMaxDamageReduction(damageType);
    reduction += protection * maxReduction;
    if (reduction >= 1) {
        event.setCancelled(true);
        sendDebugMessage(ChatColor.RED + "Damage nullified by " + ChatColor.BLUE + damageType + " (" + cause + ")", 8);
        return;
    }
    double damage = event.getDamage();
    sendDebugMessage(ChatColor.RED + "Damaged by " + ChatColor.BLUE + (damageType == null ? "generic" : damageType) + " (" + cause + ")" + ChatColor.RED + " for " + ChatColor.DARK_RED + damage, 10);
    if (reduction > 0) {
        damage = (1.0 - reduction) * damage;
        sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " reduced by " + ChatColor.AQUA + reduction + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
        event.setDamage(damage);
    }
    double weakness = getWeakness(damageType);
    double maxMultiplier = controller.getMaxDefendMultiplier(damageType);
    if (maxMultiplier > 1 && weakness > 0) {
        weakness = 1 + (maxMultiplier - 1) * weakness;
        multiplier *= weakness;
    }
    if (multiplier > 1) {
        damage = multiplier * damage;
        sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " multiplied by " + ChatColor.AQUA + multiplier + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
        event.setDamage(damage);
    }
    if (damage > 0) {
        for (Iterator<Batch> iterator = pendingBatches.iterator(); iterator.hasNext(); ) {
            Batch batch = iterator.next();
            if (!(batch instanceof SpellBatch))
                continue;
            SpellBatch spellBatch = (SpellBatch) batch;
            Spell spell = spellBatch.getSpell();
            double cancelOnDamage = spell.cancelOnDamage();
            if (cancelOnDamage > 0 && cancelOnDamage < damage) {
                spell.cancel();
                batch.finish();
                iterator.remove();
            }
        }
    }
}
Also used : SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Listener(org.bukkit.event.Listener) ArrayList(java.util.ArrayList) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) LivingEntity(org.bukkit.entity.LivingEntity) Batch(com.elmakers.mine.bukkit.api.batch.Batch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 4 with SpellBatch

use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.

the class UndoSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Target target = getTarget();
    int timeout = parameters.getInt("target_timeout", 0);
    boolean targetSelf = parameters.getBoolean("target_up_self", false);
    boolean targetDown = parameters.getBoolean("target_down_block", false);
    Entity targetEntity = target.getEntity();
    SpellResult result = SpellResult.CAST;
    if (targetSelf && isLookingUp()) {
        targetEntity = mage.getEntity();
        getCurrentCast().setTargetName(mage.getName());
        result = SpellResult.ALTERNATE_UP;
    }
    if (targetEntity != null && controller.isMage(targetEntity)) {
        Mage targetMage = controller.getMage(targetEntity);
        Batch batch = targetMage.cancelPending();
        if (batch != null) {
            undoListName = (batch instanceof SpellBatch) ? ((SpellBatch) batch).getSpell().getName() : null;
            return SpellResult.ALTERNATE;
        }
        UndoQueue queue = targetMage.getUndoQueue();
        UndoList undoList = queue.undoRecent(timeout);
        if (undoList != null) {
            undoListName = undoList.getName();
        }
        return undoList != null ? result : SpellResult.NO_TARGET;
    }
    if (!parameters.getBoolean("target_blocks", true)) {
        return SpellResult.NO_TARGET;
    }
    Block targetBlock = target.getBlock();
    if (targetDown && isLookingDown()) {
        targetBlock = getLocation().getBlock();
    }
    if (targetBlock != null) {
        boolean targetAll = mage.isSuperPowered();
        if (targetAll) {
            UndoList undid = controller.undoRecent(targetBlock, timeout);
            if (undid != null) {
                Mage targetMage = undid.getOwner();
                undoListName = undid.getName();
                getCurrentCast().setTargetName(targetMage.getName());
                return result;
            }
        } else {
            getCurrentCast().setTargetName(mage.getName());
            UndoList undoList = mage.undo(targetBlock);
            if (undoList != null) {
                undoListName = undoList.getName();
                return result;
            }
        }
    }
    return SpellResult.NO_TARGET;
}
Also used : Entity(org.bukkit.entity.Entity) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Target(com.elmakers.mine.bukkit.utility.Target) UndoQueue(com.elmakers.mine.bukkit.api.block.UndoQueue) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Batch(com.elmakers.mine.bukkit.api.batch.Batch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Block(org.bukkit.block.Block) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Aggregations

Batch (com.elmakers.mine.bukkit.api.batch.Batch)4 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)4 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)3 Spell (com.elmakers.mine.bukkit.api.spell.Spell)3 UndoBatch (com.elmakers.mine.bukkit.batch.UndoBatch)2 ActionSpell (com.elmakers.mine.bukkit.spell.ActionSpell)2 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)2 ArrayList (java.util.ArrayList)2 Block (org.bukkit.block.Block)2 LivingEntity (org.bukkit.entity.LivingEntity)2 CastContext (com.elmakers.mine.bukkit.action.CastContext)1 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 UndoQueue (com.elmakers.mine.bukkit.api.block.UndoQueue)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)1 PrerequisiteSpell (com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell)1 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)1 SpellParameters (com.elmakers.mine.bukkit.magic.SpellParameters)1 Target (com.elmakers.mine.bukkit.utility.Target)1