Search in sources :

Example 6 with SpellResult

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

the class ActionHandler method perform.

@Override
public SpellResult perform(CastContext context) {
    Location targetLocation = context.getTargetLocation();
    Entity targetEntity = context.getTargetEntity();
    SpellResult result = SpellResult.NO_ACTION;
    if (actions == null || actions.size() == 0) {
        return result;
    }
    Mage mage = context.getMage();
    boolean showDebug = mage.getDebugLevel() > 2;
    if (showDebug) {
        debugIndent += "  ";
    }
    boolean isPending = false;
    while (currentAction != null) {
        ActionContext action = actions.get(currentAction);
        if (!started) {
            started = true;
            action.getAction().reset(context);
        }
        if (action.getAction().requiresTargetEntity() && targetEntity == null) {
            if (showDebug) {
                mage.sendDebugMessage(ChatColor.GRAY + debugIndent + "Action " + ChatColor.GOLD + action.getAction().getClass().getSimpleName() + ChatColor.WHITE + ": " + ChatColor.GRAY + "Skipped, requires entity target", 17);
            }
            result = result.min(SpellResult.NO_TARGET);
            advance(context);
            continue;
        }
        if (action.getAction().requiresTarget() && targetLocation == null) {
            if (showDebug) {
                mage.sendDebugMessage(ChatColor.GRAY + debugIndent + "Action " + ChatColor.GOLD + action.getAction().getClass().getSimpleName() + ChatColor.WHITE + ": " + ChatColor.GRAY + "Skipped, requires target", 17);
            }
            result = result.min(SpellResult.NO_TARGET);
            advance(context);
            continue;
        }
        SpellResult actionResult = action.perform(context);
        context.addWork(1);
        if (actionResult == SpellResult.PENDING) {
            isPending = true;
        } else {
            result = result.min(actionResult);
        }
        if (actionResult == SpellResult.STOP) {
            if (showDebug) {
                mage.sendDebugMessage(ChatColor.RED + debugIndent + "Action " + ChatColor.GOLD + action.getAction().getClass().getSimpleName() + ChatColor.WHITE + ": " + ChatColor.AQUA + actionResult.name().toLowerCase(), 15);
            }
            cancel(context);
        }
        if (actionResult.isStop()) {
            break;
        }
        if (showDebug) {
            mage.sendDebugMessage(ChatColor.WHITE + debugIndent + "Action " + ChatColor.GOLD + action.getAction().getClass().getSimpleName() + ChatColor.WHITE + ": " + ChatColor.AQUA + actionResult.name().toLowerCase(), 15);
        }
        advance(context);
        if (context.getWorkAllowed() <= 0) {
            isPending = true;
            break;
        }
    }
    if (showDebug) {
        debugIndent = debugIndent.substring(0, debugIndent.length() - 2);
    }
    SpellResult currentResult = context.getResult();
    context.addResult(result);
    SpellResult contextResult = context.processHandlers();
    if (contextResult == SpellResult.PENDING) {
        isPending = true;
    } else {
        context.addResult(contextResult);
    }
    SpellResult newResult = context.getResult();
    if (showDebug && newResult != currentResult) {
        mage.sendDebugMessage(ChatColor.AQUA + debugIndent + "Result changed from " + ChatColor.DARK_AQUA + currentResult.name().toLowerCase() + ChatColor.WHITE + " to " + ChatColor.AQUA + newResult.name().toLowerCase(), 12);
    }
    return isPending ? SpellResult.PENDING : newResult;
}
Also used : Entity(org.bukkit.entity.Entity) Mage(com.elmakers.mine.bukkit.api.magic.Mage) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) Location(org.bukkit.Location)

Example 7 with SpellResult

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

the class ActionHandler method start.

public SpellResult start(CastContext context, ConfigurationSection parameters) {
    prepare(context, parameters);
    reset(context);
    SpellResult handlerResult = perform(context);
    if (handlerResult == SpellResult.PENDING) {
        ActionBatch batch = new ActionBatch(context, this);
        if (!context.getMage().addBatch(batch)) {
            handlerResult = SpellResult.FAIL;
            finish(context);
            context.finish();
        }
    } else {
        finish(context);
        context.finish();
    }
    return handlerResult;
}
Also used : ActionBatch(com.elmakers.mine.bukkit.batch.ActionBatch) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Example 8 with SpellResult

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

the class BaseShopAction method perform.

@Override
public SpellResult perform(CastContext context) {
    if (isActive) {
        return SpellResult.PENDING;
    }
    if (finalResult != null) {
        return finalResult;
    }
    SpellResult contextResult = checkContext(context);
    if (!contextResult.isSuccess()) {
        return contextResult;
    }
    List<ShopItem> items = getItems(context);
    if (items == null) {
        return SpellResult.NO_ACTION;
    }
    return showItems(context, items);
}
Also used : SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Example 9 with SpellResult

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

the class CastContext method processHandlers.

@Override
public SpellResult processHandlers() {
    SpellResult result = SpellResult.NO_ACTION;
    if (handlers == null)
        return result;
    if (finishedHandlers == null) {
        finishedHandlers = new ArrayList<>();
    }
    int startingWork = getWorkAllowed();
    int splitWork = Math.max(1, startingWork / handlers.size());
    for (Iterator<ActionHandlerContext> iterator = handlers.iterator(); iterator.hasNext(); ) {
        ActionHandlerContext handler = iterator.next();
        handler.setWorkAllowed(splitWork);
        SpellResult actionResult = handler.perform();
        if (actionResult != SpellResult.PENDING) {
            result = result.min(actionResult);
            finishedHandlers.add(handler);
            iterator.remove();
        }
    }
    if (handlers.isEmpty()) {
        handlers = null;
        return result;
    }
    return SpellResult.PENDING;
}
Also used : SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Example 10 with SpellResult

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

the class CompoundAction method perform.

@Override
public SpellResult perform(CastContext context) {
    SpellResult result = SpellResult.NO_ACTION;
    while (!result.isStop()) {
        if (state == State.NOT_STARTED) {
            result = result.min(start(context));
            // Don't continue if the action failed to start
            if (result.isStop() || result.isFailure())
                break;
            state = State.STARTED;
        }
        if (state == State.STARTED) {
            result = result.min(step(context));
            if (result.isStop())
                break;
            state = State.STEPPING;
        }
        ActionHandler handler = currentHandler == null ? null : handlers.get(currentHandler);
        if (handler != null) {
            result = result.min(handler.perform(actionContext));
            if (result.isStop())
                break;
            if (stopOnSuccess && result.isSuccess()) {
                result = SpellResult.STOP;
                break;
            }
        }
        if (!next(context)) {
            if (handler != null) {
                handler.finish(actionContext);
            }
            break;
        }
        result = result.min(step(context));
    }
    return result;
}
Also used : 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