Search in sources :

Example 6 with FunctionReturnException

use of com.laytonsmith.core.exceptions.FunctionReturnException in project CommandHelper by EngineHub.

the class AbstractEvent method execute.

/**
 * This function is run when the actual event occurs.
 *
 * @param tree The compiled parse tree
 * @param b The bound event
 * @param env The operating environment
 * @param activeEvent The active event being executed
 */
@Override
public final void execute(ParseTree tree, BoundEvent b, Environment env, BoundEvent.ActiveEvent activeEvent) throws ConfigRuntimeException {
    preExecution(env, activeEvent);
    // Various events have a player to put into the env.
    // Do this after preExcecution() in case the particular event needs to inject the player first.
    Construct c = activeEvent.getParsedEvent().get("player");
    if (c != null) {
        try {
            MCPlayer p = Static.GetPlayer(c, Target.UNKNOWN);
            env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
        } catch (CREPlayerOfflineException e) {
            // Set env CommandSender to prevent incorrect inherited player from being used in a player event.
            if (env.getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
            }
        }
    }
    ProfilePoint event = null;
    if (env.getEnv(GlobalEnv.class).GetProfiler() != null) {
        event = env.getEnv(GlobalEnv.class).GetProfiler().start("Event " + b.getEventName() + " (defined at " + b.getTarget().toString() + ")", LogLevel.ERROR);
    }
    try {
        try {
            // Get the label from the bind time environment, and put it in the current environment.
            String label = b.getEnvironment().getEnv(GlobalEnv.class).GetLabel();
            if (label == null) {
                // Set the permission to global if it's null, since that means
                // it wasn't set, and so we aren't in a secured environment anyways.
                label = Static.GLOBAL_PERMISSION;
            }
            env.getEnv(GlobalEnv.class).SetLabel(label);
            MethodScriptCompiler.execute(tree, env, null, null);
        } catch (CancelCommandException ex) {
            if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
                StreamUtils.GetSystemOut().println(ex.getMessage());
            }
        } catch (FunctionReturnException ex) {
        // We simply allow this to end the event execution
        } catch (ProgramFlowManipulationException ex) {
            ConfigRuntimeException.HandleUncaughtException(new CREFormatException("Unexpected control flow operation used.", ex.getTarget()), env);
        }
    } finally {
        if (event != null) {
            event.stop();
        }
        // Finally, among other things, we need to clean-up injected players and entities
        postExecution(env, activeEvent);
    }
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) Construct(com.laytonsmith.core.constructs.Construct) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint)

Example 7 with FunctionReturnException

use of com.laytonsmith.core.exceptions.FunctionReturnException in project CommandHelper by EngineHub.

the class EventUtils method FireListeners.

public static void FireListeners(SortedSet<BoundEvent> toRun, Event driver, BindableEvent e) {
    // Sort our event handlers by priorities
    BoundEvent.ActiveEvent activeEvent = new BoundEvent.ActiveEvent(e);
    for (BoundEvent b : toRun) {
        if (activeEvent.canReceive() || b.getPriority().equals(Priority.MONITOR)) {
            try {
                // We must re-set the active event's bound event and parsed event
                activeEvent.setBoundEvent(b);
                activeEvent.setParsedEvent(driver.evaluate(e));
                b.trigger(activeEvent);
            } catch (FunctionReturnException ex) {
            // We also know how to deal with this
            } catch (EventException ex) {
                throw new CREEventException(ex.getMessage(), Target.UNKNOWN, ex);
            } catch (ConfigRuntimeException ex) {
                // An exception has bubbled all the way up
                ConfigRuntimeException.HandleUncaughtException(ex, b.getEnvironment());
            }
        }
    }
    for (BoundEvent b : toRun) {
        activeEvent.setBoundEvent(b);
        if (activeEvent.isCancelled()) {
            activeEvent.executeCancelled();
        } else {
            activeEvent.executeTriggered();
        }
    }
}
Also used : EventException(com.laytonsmith.core.exceptions.EventException) CREEventException(com.laytonsmith.core.exceptions.CRE.CREEventException) CREEventException(com.laytonsmith.core.exceptions.CRE.CREEventException) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException)

Example 8 with FunctionReturnException

use of com.laytonsmith.core.exceptions.FunctionReturnException in project CommandHelper by EngineHub.

the class CompositeFunction method exec.

@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
    ParseTree tree;
    if (!cachedScripts.containsKey(this.getClass())) {
        try {
            String script = script();
            tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)).getChildAt(0);
        } catch (ConfigCompileException | ConfigCompileGroupException ex) {
            // This is really bad.
            throw new Error(ex);
        }
        if (cacheCompile()) {
            cachedScripts.put(this.getClass(), tree);
        }
    } else {
        tree = cachedScripts.get(this.getClass());
    }
    GlobalEnv env = environment.getEnv(GlobalEnv.class);
    IVariableList oldVariables = env.GetVarList();
    IVariableList newVariables = new IVariableList();
    newVariables.set(new IVariable(CClassType.get("array"), "@arguments", new CArray(t, args.length, args), t));
    env.SetVarList(newVariables);
    Construct ret = CVoid.VOID;
    try {
        env.GetScript().eval(tree, environment);
    } catch (FunctionReturnException ex) {
        ret = ex.getReturn();
    }
    env.SetVarList(oldVariables);
    return ret;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 9 with FunctionReturnException

use of com.laytonsmith.core.exceptions.FunctionReturnException in project CommandHelper by EngineHub.

the class BukkitMCCommand method handleCustomCommand.

@Override
public boolean handleCustomCommand(MCCommandSender sender, String label, String[] args) {
    if (Commands.onCommand.containsKey(cmd.getName().toLowerCase())) {
        Target t = Target.UNKNOWN;
        CArray cargs = new CArray(t);
        for (String arg : args) {
            cargs.push(new CString(arg, t), t);
        }
        CClosure closure = Commands.onCommand.get(cmd.getName().toLowerCase());
        CommandHelperEnvironment cEnv = closure.getEnv().getEnv(CommandHelperEnvironment.class);
        cEnv.SetCommandSender(sender);
        cEnv.SetCommand("/" + label + StringUtils.Join(args, " "));
        try {
            closure.execute(new CString(label, t), new CString(sender.getName(), t), cargs, // reserved for an obgen style command array
            new CArray(t));
        } catch (FunctionReturnException e) {
            Construct fret = e.getReturn();
            if (fret instanceof CBoolean) {
                return ((CBoolean) fret).getBoolean();
            }
        } catch (ConfigRuntimeException cre) {
            cre.setEnv(closure.getEnv());
            ConfigRuntimeException.HandleUncaughtException(cre, closure.getEnv());
        }
        return true;
    } else {
        return false;
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString)

Example 10 with FunctionReturnException

use of com.laytonsmith.core.exceptions.FunctionReturnException in project CommandHelper by EngineHub.

the class MethodScriptExecutionQueue method getExceptionHandler.

private Thread.UncaughtExceptionHandler getExceptionHandler() {
    Thread.UncaughtExceptionHandler uceh = new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            Environment env = Environment.createEnvironment(MethodScriptExecutionQueue.this.env);
            if (e instanceof ConfigRuntimeException) {
                // This should be handled by the default UEH
                ConfigRuntimeException.HandleUncaughtException(((ConfigRuntimeException) e), env);
            } else if (e instanceof FunctionReturnException) {
                // ignored, so we want to warn them, but not trigger a flat out error.
                if (!(((FunctionReturnException) e).getReturn() instanceof CVoid)) {
                    ConfigRuntimeException.DoWarning("Closure is returning a value in an execution queue task," + " which is unexpected behavior. It may return void however, which will" + " simply stop that one task. " + ((FunctionReturnException) e).getTarget().toString());
                }
            } else if (e instanceof CancelCommandException) {
                // Ok. If there's a message, echo it to console.
                String msg = ((CancelCommandException) e).getMessage().trim();
                if (!"".equals(msg)) {
                    Target tt = ((CancelCommandException) e).getTarget();
                    new Echoes.console().exec(tt, env, new CString(msg, tt));
                }
            } else {
                // handle, so let it bubble up further.
                throw new RuntimeException(e);
            }
        }
    };
    return uceh;
}
Also used : ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) CVoid(com.laytonsmith.core.constructs.CVoid) CString(com.laytonsmith.core.constructs.CString) Echoes(com.laytonsmith.core.functions.Echoes) Target(com.laytonsmith.core.constructs.Target) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) Environment(com.laytonsmith.core.environments.Environment) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException)

Aggregations

FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)10 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)8 Construct (com.laytonsmith.core.constructs.Construct)6 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)5 CArray (com.laytonsmith.core.constructs.CArray)4 CString (com.laytonsmith.core.constructs.CString)4 Target (com.laytonsmith.core.constructs.Target)4 ParseTree (com.laytonsmith.core.ParseTree)3 IVariable (com.laytonsmith.core.constructs.IVariable)3 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)3 Environment (com.laytonsmith.core.environments.Environment)3 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)3 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)3 LoopManipulationException (com.laytonsmith.core.exceptions.LoopManipulationException)3 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)3 StackTraceManager (com.laytonsmith.core.exceptions.StackTraceManager)3 ArrayList (java.util.ArrayList)3 CClosure (com.laytonsmith.core.constructs.CClosure)2 IVariableList (com.laytonsmith.core.constructs.IVariableList)2