Search in sources :

Example 1 with ActionContext

use of com.elmakers.mine.bukkit.action.ActionContext in project MagicPlugin by elBukkit.

the class ParallelAction method reset.

@Override
public void reset(CastContext context) {
    super.reset(context);
    ActionHandler actions = getHandler("actions");
    if (actions != null) {
        remaining = new ArrayList<>(actions.getActions());
        for (ActionContext action : remaining) {
            action.getAction().reset(context);
        }
    }
}
Also used : ActionContext(com.elmakers.mine.bukkit.action.ActionContext) ActionHandler(com.elmakers.mine.bukkit.action.ActionHandler)

Example 2 with ActionContext

use of com.elmakers.mine.bukkit.action.ActionContext in project MagicPlugin by elBukkit.

the class ParallelAction method perform.

@Override
public SpellResult perform(CastContext context) {
    SpellResult result = SpellResult.NO_ACTION;
    int startingWork = context.getWorkAllowed();
    List<ActionContext> subActions = new ArrayList<>(remaining);
    remaining.clear();
    context.setWorkAllowed(0);
    int splitWork = Math.max(1, startingWork / subActions.size());
    for (ActionContext 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) ActionContext(com.elmakers.mine.bukkit.action.ActionContext)

Example 3 with ActionContext

use of com.elmakers.mine.bukkit.action.ActionContext in project MagicPlugin by elBukkit.

the class RecurseSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Block targetBlock = getTargetBlock();
    if (targetBlock == null) {
        return SpellResult.NO_TARGET;
    }
    if (!hasBuildPermission(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    if (!isDestructible(targetBlock)) {
        return SpellResult.NO_TARGET;
    }
    BlockRecurse blockRecurse = new BlockRecurse();
    int size = parameters.getInt("size", 8);
    size = (int) (mage.getRadiusMultiplier() * size);
    blockRecurse.setMaxRecursion(size);
    ModifyBlockAction action = new ModifyBlockAction();
    action.initialize(this, parameters);
    blockRecurse.addReplaceable(new MaterialAndData(targetBlock));
    Material targetMaterial = targetBlock.getType();
    // A bit hacky, but is very handy!
    if (targetMaterial == Material.STATIONARY_WATER || targetMaterial == Material.WATER) {
        for (byte i = 0; i < 9; i++) {
            blockRecurse.addReplaceable(Material.STATIONARY_WATER, i);
            blockRecurse.addReplaceable(Material.WATER, i);
        }
    } else if (targetMaterial == Material.STATIONARY_LAVA || targetMaterial == Material.LAVA) {
        for (byte i = 0; i < 9; i++) {
            blockRecurse.addReplaceable(Material.STATIONARY_LAVA, i);
            blockRecurse.addReplaceable(Material.LAVA, i);
        }
    } else if (targetMaterial == Material.SNOW) {
        for (byte i = 0; i < 8; i++) {
            blockRecurse.addReplaceable(Material.SNOW, i);
        }
    }
    CastContext context = getCurrentCast();
    context.setTargetLocation(targetBlock.getLocation());
    blockRecurse.recurse(new ActionContext(action, parameters), context);
    registerForUndo();
    controller.updateBlock(targetBlock);
    return SpellResult.CAST;
}
Also used : CastContext(com.elmakers.mine.bukkit.api.action.CastContext) BlockRecurse(com.elmakers.mine.bukkit.batch.BlockRecurse) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ModifyBlockAction(com.elmakers.mine.bukkit.action.builtin.ModifyBlockAction) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ActionContext(com.elmakers.mine.bukkit.action.ActionContext)

Example 4 with ActionContext

use of com.elmakers.mine.bukkit.action.ActionContext in project MagicPlugin by elBukkit.

the class RandomAction method mapActions.

protected void mapActions() {
    actionProbability = new ArrayDeque<>();
    ActionHandler actions = getHandler("actions");
    if (actions != null) {
        List<ActionContext> options = actions.getActions();
        float totalWeight = 0;
        for (ActionContext option : options) {
            float weight = 1;
            ConfigurationSection actionParameters = option.getActionParameters();
            if (actionParameters != null) {
                weight = (float) actionParameters.getDouble("weight", weight);
            }
            totalWeight += weight;
            actionProbability.add(new WeightedPair<>(totalWeight, weight, option));
        }
    }
}
Also used : ActionContext(com.elmakers.mine.bukkit.action.ActionContext) ActionHandler(com.elmakers.mine.bukkit.action.ActionHandler) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

ActionContext (com.elmakers.mine.bukkit.action.ActionContext)4 ActionHandler (com.elmakers.mine.bukkit.action.ActionHandler)2 ModifyBlockAction (com.elmakers.mine.bukkit.action.builtin.ModifyBlockAction)1 CastContext (com.elmakers.mine.bukkit.api.action.CastContext)1 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)1 BlockRecurse (com.elmakers.mine.bukkit.batch.BlockRecurse)1 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)1 ArrayList (java.util.ArrayList)1 Material (org.bukkit.Material)1 Block (org.bukkit.block.Block)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1