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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations