Search in sources :

Example 1 with LoopContinueException

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

the class Script method run.

public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) {
    // Some things, such as the label are determined at compile time
    this.CurrentEnv = myEnv;
    this.CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
    MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender();
    if (!hasBeenCompiled || compilerError) {
        Target target = Target.UNKNOWN;
        if (left.size() >= 1) {
            try {
                target = new Target(left.get(0).line_num, left.get(0).file, left.get(0).column);
            } catch (NullPointerException e) {
            // Oh well, we tried to get more information
            }
        }
        throw ConfigRuntimeException.CreateUncatchableException("Unable to run command, script not yet compiled," + " or a compiler error occurred for that command. To see the compile error, run /reloadaliases", target);
    }
    enforceLabelPermissions();
    try {
        for (ParseTree rootNode : cright) {
            if (rootNode == null) {
                continue;
            }
            for (Construct tempNode : rootNode.getAllData()) {
                if (tempNode instanceof Variable) {
                    if (left_vars == null) {
                        throw ConfigRuntimeException.CreateUncatchableException("$variables may not be used in this context." + " Only @variables may be.", tempNode.getTarget());
                    }
                    Construct c = Static.resolveDollarVar(left_vars.get(((Variable) tempNode).getVariableName()), vars);
                    ((Variable) tempNode).setVal(new CString(c.toString(), tempNode.getTarget()));
                }
            }
            MethodScriptCompiler.registerAutoIncludes(CurrentEnv, this);
            MethodScriptCompiler.execute(rootNode, CurrentEnv, done, this);
        }
    } catch (ConfigRuntimeException ex) {
        // We don't know how to handle this really, so let's pass it up the chain.
        throw ex;
    } catch (CancelCommandException e) {
    // p.sendMessage(e.getMessage());
    // The message in the exception is actually empty
    } catch (LoopBreakException e) {
        if (p != null) {
            p.sendMessage("The break() function must be used inside a for() or foreach() loop");
        }
        StreamUtils.GetSystemOut().println("The break() function must be used inside a for() or foreach() loop");
    } catch (LoopContinueException e) {
        if (p != null) {
            p.sendMessage("The continue() function must be used inside a for() or foreach() loop");
        }
        StreamUtils.GetSystemOut().println("The continue() function must be used inside a for() or foreach() loop");
    } catch (FunctionReturnException e) {
        if (myEnv.getEnv(GlobalEnv.class).GetEvent() != null) {
            // Oh, we're running in an event handler. Those know how to catch it too.
            throw e;
        }
        if (p != null) {
            p.sendMessage("The return() function must be used inside a procedure.");
        }
        StreamUtils.GetSystemOut().println("The return() function must be used inside a procedure.");
    } catch (Throwable t) {
        StreamUtils.GetSystemOut().println("An unexpected exception occurred during the execution of a script.");
        t.printStackTrace();
        if (p != null) {
            p.sendMessage("An unexpected exception occurred during the execution of your script." + " Please check the console for more information.");
        }
    }
    if (done != null) {
        done.done(null);
    }
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CString(com.laytonsmith.core.constructs.CString) Target(com.laytonsmith.core.constructs.Target) LoopContinueException(com.laytonsmith.core.exceptions.LoopContinueException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) LoopBreakException(com.laytonsmith.core.exceptions.LoopBreakException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException)

Aggregations

MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)1 CString (com.laytonsmith.core.constructs.CString)1 Construct (com.laytonsmith.core.constructs.Construct)1 IVariable (com.laytonsmith.core.constructs.IVariable)1 Target (com.laytonsmith.core.constructs.Target)1 Variable (com.laytonsmith.core.constructs.Variable)1 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)1 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)1 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)1 LoopBreakException (com.laytonsmith.core.exceptions.LoopBreakException)1 LoopContinueException (com.laytonsmith.core.exceptions.LoopContinueException)1