Search in sources :

Example 16 with Target

use of com.laytonsmith.core.constructs.Target 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 17 with Target

use of com.laytonsmith.core.constructs.Target 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

Target (com.laytonsmith.core.constructs.Target)17 CString (com.laytonsmith.core.constructs.CString)6 Construct (com.laytonsmith.core.constructs.Construct)6 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)6 ParseTree (com.laytonsmith.core.ParseTree)5 CArray (com.laytonsmith.core.constructs.CArray)5 CFunction (com.laytonsmith.core.constructs.CFunction)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)4 ArrayList (java.util.ArrayList)4 CKeyword (com.laytonsmith.core.constructs.CKeyword)3 Token (com.laytonsmith.core.constructs.Token)3 CClosure (com.laytonsmith.core.constructs.CClosure)2 CInt (com.laytonsmith.core.constructs.CInt)2 IVariable (com.laytonsmith.core.constructs.IVariable)2 Variable (com.laytonsmith.core.constructs.Variable)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)2 File (java.io.File)2 List (java.util.List)2