Search in sources :

Example 16 with SpellResult

use of com.elmakers.mine.bukkit.api.spell.SpellResult in project MagicPlugin by elBukkit.

the class MultiplyAction method perform.

@Override
public SpellResult perform(CastContext context) {
    SpellResult result = SpellResult.NO_ACTION;
    if (remaining.size() == 0)
        return result;
    int startingWork = context.getWorkAllowed();
    List<ActionHandler> subActions = new ArrayList<>(remaining);
    remaining.clear();
    context.setWorkAllowed(0);
    int splitWork = Math.max(1, startingWork / subActions.size());
    for (ActionHandler action : subActions) {
        context.setWorkAllowed(context.getWorkAllowed() + splitWork);
        SpellResult actionResult = action.perform(context);
        context.addResult(actionResult);
        if (actionResult.isStop()) {
            remaining.add(action);
        }
        result = result.min(actionResult);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) ActionHandler(com.elmakers.mine.bukkit.action.ActionHandler)

Example 17 with SpellResult

use of com.elmakers.mine.bukkit.api.spell.SpellResult in project MagicPlugin by elBukkit.

the class EntityProjectileAction method step.

@Override
public SpellResult step(CastContext context) {
    SpellResult result = super.step(context);
    if (entity == null) {
        return SpellResult.CAST;
    }
    // Note that in testing it somehow doesn't seem to matter if we adjust the location here
    // I really have no idea why, but it seems to work OK if we adjust it on spawn.
    Location target = adjustLocation(actionContext.getTargetLocation());
    if (doVelocity) {
        Vector velocity = this.velocity.clone().multiply(distanceTravelledThisTick);
        if (velocityOffset != null) {
            velocity = velocity.add(velocityOffset);
        }
        SafetyUtils.setVelocity(entity, velocity);
    }
    if (doTeleport) {
        if (orient) {
            target.setDirection(velocity);
        }
        entity.teleport(target);
    }
    return result;
}
Also used : SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 18 with SpellResult

use of com.elmakers.mine.bukkit.api.spell.SpellResult in project MagicPlugin by elBukkit.

the class UndoAction method perform.

@Override
public SpellResult perform(CastContext context) {
    // Start of new functionality
    if (undoOldest > 0 || undoToSize > 0) {
        return performNew(context);
    }
    // Old functionality- this should be converted into an action that processes
    // blocks instead of creating a separate batch.
    Entity targetEntity = context.getTargetEntity();
    SpellResult result = SpellResult.CAST;
    Mage mage = context.getMage();
    if (targetSelf) {
        targetEntity = context.getEntity();
        context.setTargetName(mage.getName());
        result = SpellResult.ALTERNATE_UP;
    }
    MageController controller = context.getController();
    if (targetEntity != null && controller.isMage(targetEntity)) {
        Mage targetMage = controller.getMage(targetEntity);
        mage.sendDebugMessage(ChatColor.AQUA + "Undo checking last spell of " + ChatColor.GOLD + targetMage + ChatColor.AQUA + " with timeout of " + ChatColor.YELLOW + timeout + ChatColor.AQUA + " for target spellKey" + ChatColor.BLUE + targetSpellKey, 2);
        Batch batch = targetMage.cancelPending(targetSpellKey);
        if (batch != null) {
            undoListName = batch.getName();
            if (cancel) {
                return SpellResult.DEACTIVATE;
            }
        }
        UndoQueue queue = targetMage.getUndoQueue();
        UndoList undoList = queue.undoRecent(timeout, targetSpellKey);
        if (undoList != null) {
            undoListName = undoList.getName();
        }
        return undoList != null ? result : SpellResult.NO_TARGET;
    }
    if (!targetBlocks) {
        return SpellResult.NO_TARGET;
    }
    Block targetBlock = context.getTargetBlock();
    if (targetDown) {
        targetBlock = context.getLocation().getBlock();
    }
    if (targetBlock != null) {
        boolean undoAny = targetOtherBlocks;
        undoAny = undoAny || (adminPermission != null && context.getController().hasPermission(context.getMage().getCommandSender(), adminPermission));
        undoAny = undoAny || mage.isSuperPowered();
        if (undoAny) {
            mage.sendDebugMessage(ChatColor.AQUA + "Looking for recent cast at " + ChatColor.GOLD + targetBlock + ChatColor.AQUA + " with timeout of " + ChatColor.YELLOW + blockTimeout, 2);
            UndoList undid = controller.undoRecent(targetBlock, blockTimeout);
            if (undid != null) {
                Mage targetMage = undid.getOwner();
                undoListName = undid.getName();
                if (targetMage != null) {
                    context.setTargetName(targetMage.getName());
                }
                return result;
            }
        } else {
            mage.sendDebugMessage(ChatColor.AQUA + "Looking for recent self-cast at " + ChatColor.GOLD + targetBlock, 2);
            context.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) MageController(com.elmakers.mine.bukkit.api.magic.MageController) UndoQueue(com.elmakers.mine.bukkit.api.block.UndoQueue) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Batch(com.elmakers.mine.bukkit.api.batch.Batch) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Block(org.bukkit.block.Block) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Example 19 with SpellResult

use of com.elmakers.mine.bukkit.api.spell.SpellResult in project MagicPlugin by elBukkit.

the class BaseSpell method finish.

@Override
public void finish(com.elmakers.mine.bukkit.api.action.CastContext context) {
    SpellResult result = context.getResult();
    // Notify other plugins of this spell cast
    CastEvent castEvent = new CastEvent(mage, this, result);
    Bukkit.getPluginManager().callEvent(castEvent);
    // Message targets
    if (result.isSuccess() && (loud || (!mage.isQuiet() && !quiet))) {
        messageTargets("cast_player_message");
    }
    // Clear cooldown on miss
    if (result.shouldRefundCooldown(castOnNoTarget)) {
        clearCooldown();
    }
    if (cancelEffects) {
        context.cancelEffects();
    }
    // Track cast counts
    if (result.isSuccess() && !passive) {
        spellData.addCast();
        if (template != null && template.spellData != null) {
            template.spellData.addCast();
            SpellCategory category = template.getCategory();
            if (category != null) {
                category.addCast();
            }
        }
        // Reward SP
        Wand wand = context.getWand();
        Wand activeWand = mage.getActiveWand();
        if (activeWand != null && wand != null && activeWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), activeWand.getItem()) && activeWand.getItem().equals(wand.getItem())) {
            wand = activeWand;
        }
        Wand offhandWand = mage.getOffhandWand();
        if (offhandWand != null && wand != null && offhandWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), offhandWand.getItem()) && offhandWand.getItem().equals(wand.getItem())) {
            wand = offhandWand;
        }
        WandUpgradePath path = wand == null ? null : wand.getPath();
        if (earns > 0 && wand != null && path != null && path.earnsSP() && controller.isSPEnabled() && controller.isSPEarnEnabled() && !mage.isAtMaxSkillPoints()) {
            long now = System.currentTimeMillis();
            int scaledEarn = earns;
            if (spellData.getLastEarn() > 0 && earnCooldown > 0 && now < spellData.getLastEarn() + earnCooldown) {
                scaledEarn = (int) Math.floor((double) earns * (now - spellData.getLastEarn()) / earnCooldown);
                if (scaledEarn > 0) {
                    context.playEffects("earn_scaled_sp");
                }
            } else {
                context.playEffects("earn_sp");
            }
            if (scaledEarn > 0) {
                mage.addSkillPoints((int) Math.floor(mage.getSPMultiplier() * scaledEarn));
                spellData.setLastEarn(now);
            }
        }
        // This currently only works on wands.
        if (wand != null && wand.upgradesAllowed() && wand.getSpellLevel(spellKey.getBaseKey()) == spellKey.getLevel()) {
            if (controller.isSpellUpgradingEnabled()) {
                SpellTemplate upgrade = getUpgrade();
                long requiredCasts = getRequiredUpgradeCasts();
                String upgradePath = getRequiredUpgradePath();
                WandUpgradePath currentPath = wand.getPath();
                Set<String> upgradeTags = getRequiredUpgradeTags();
                if ((upgrade != null && requiredCasts > 0 && getCastCount() >= requiredCasts) && (upgradePath == null || upgradePath.isEmpty() || (currentPath != null && currentPath.hasPath(upgradePath))) && (upgradeTags == null || upgradeTags.isEmpty() || (currentPath != null && currentPath.hasAllTags(upgradeTags)))) {
                    if (PrerequisiteSpell.hasPrerequisites(wand, upgrade)) {
                        MageSpell newSpell = mage.getSpell(upgrade.getKey());
                        if (isActive()) {
                            deactivate(true, true);
                            if (newSpell != null) {
                                newSpell.activate();
                            }
                        }
                        wand.forceAddSpell(upgrade.getKey());
                        playEffects("upgrade");
                        if (controller.isPathUpgradingEnabled()) {
                            wand.checkAndUpgrade(true);
                        }
                        // return so progress upgrade doesn't also happen
                        return;
                    }
                }
            }
            if (maxLevels > 0 && controller.isSpellProgressionEnabled()) {
                long previousLevel = getPreviousCastProgressLevel();
                long currentLevel = getProgressLevel();
                if (currentLevel != previousLevel) {
                    wand.addSpell(getKey());
                    if (currentLevel > previousLevel) {
                        Messages messages = controller.getMessages();
                        String progressDescription = getProgressDescription();
                        playEffects("progress");
                        if (progressDescription != null && !progressDescription.isEmpty()) {
                            mage.sendMessage(messages.get("wand.spell_progression").replace("$name", getName()).replace("$wand", getName()).replace("$level", Long.toString(getProgressLevel())).replace("$max_level", Long.toString(maxLevels)));
                        }
                    }
                    if (controller.isPathUpgradingEnabled()) {
                        wand.checkAndUpgrade(true);
                    }
                }
            }
        }
    }
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) SpellCategory(com.elmakers.mine.bukkit.api.spell.SpellCategory) Messages(com.elmakers.mine.bukkit.api.magic.Messages) CastEvent(com.elmakers.mine.bukkit.api.event.CastEvent) PreCastEvent(com.elmakers.mine.bukkit.api.event.PreCastEvent) Wand(com.elmakers.mine.bukkit.api.wand.Wand) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 20 with SpellResult

use of com.elmakers.mine.bukkit.api.spell.SpellResult in project MagicPlugin by elBukkit.

the class BaseSpell method finalizeCast.

protected boolean finalizeCast(ConfigurationSection parameters) {
    SpellResult result = null;
    // Global parameters
    controller.disablePhysics(parameters.getInt("disable_physics", 0));
    if (!mage.isSuperPowered()) {
        if (backfireChance > 0 && random.nextDouble() < backfireChance) {
            backfire();
        } else if (fizzleChance > 0 && random.nextDouble() < fizzleChance) {
            result = SpellResult.FIZZLE;
        }
    }
    if (result == null) {
        result = onCast(parameters);
    }
    if (backfired) {
        result = SpellResult.BACKFIRE;
    }
    if (result == SpellResult.CAST) {
        LivingEntity sourceEntity = mage.getLivingEntity();
        Entity targetEntity = getTargetEntity();
        if (sourceEntity == targetEntity) {
            result = SpellResult.CAST_SELF;
        }
    }
    processResult(result, parameters);
    boolean success = result.isSuccess();
    boolean free = result.isFree(castOnNoTarget);
    if (!free) {
        if (costs != null && !mage.isCostFree()) {
            UndoList undoList = currentCast.getUndoList();
            for (CastingCost cost : costs) {
                if (undoList != null && cost.isItem() && currentCast != null) {
                    undoList.setConsumed(true);
                }
                cost.use(this);
            }
        }
        updateCooldown();
    }
    if (success && toggle != ToggleType.NONE) {
        activate();
    }
    sendCastMessage(result, " (" + success + ")");
    return success;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Aggregations

SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)21 Entity (org.bukkit.entity.Entity)6 Mage (com.elmakers.mine.bukkit.api.magic.Mage)5 Block (org.bukkit.block.Block)5 Batch (com.elmakers.mine.bukkit.api.batch.Batch)3 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)3 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 Location (org.bukkit.Location)3 Vector (org.bukkit.util.Vector)3 ActionHandler (com.elmakers.mine.bukkit.action.ActionHandler)2 UndoQueue (com.elmakers.mine.bukkit.api.block.UndoQueue)2 Wand (com.elmakers.mine.bukkit.api.wand.Wand)2 Target (com.elmakers.mine.bukkit.utility.Target)2 ArrayList (java.util.ArrayList)2 ArmorStand (org.bukkit.entity.ArmorStand)2 ActionContext (com.elmakers.mine.bukkit.action.ActionContext)1 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)1 CastEvent (com.elmakers.mine.bukkit.api.event.CastEvent)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 Messages (com.elmakers.mine.bukkit.api.magic.Messages)1