Search in sources :

Example 6 with AbstractCREException

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

the class ExampleScript method getOutput.

public String getOutput() throws IOException, DataSourceException, URISyntaxException {
    if (output != null) {
        return output;
    }
    Script s = Script.GenerateScript(script, Static.GLOBAL_PERMISSION);
    Environment env;
    try {
        env = Static.GenerateStandaloneEnvironment();
    } catch (Profiles.InvalidProfileException ex) {
        throw new RuntimeException(ex);
    }
    Class[] interfaces = new Class[] { MCPlayer.class };
    MCPlayer p = (MCPlayer) Proxy.newProxyInstance(ExampleScript.class.getClassLoader(), interfaces, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("getName") || method.getName().equals("getDisplayName")) {
                return "Player";
            }
            if (method.getName().equals("sendMessage")) {
                playerOutput.append(args[0].toString()).append("\n");
            }
            if (method.getName().equals("isOnline")) {
                return true;
            }
            return genericReturn(method.getReturnType());
        }
    });
    // TODO: Remove this dependency. Make MCPlayer implement a generic "User" and make that
    // part of the GlobalEnv.
    env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
    final StringBuilder finalOutput = new StringBuilder();
    String thrown = null;
    try {
        List<Variable> vars = new ArrayList<>();
        try {
            MethodScriptCompiler.execute(originalScript, new File("/" + functionName + ".ms"), true, env, new MethodScriptComplete() {

                @Override
                public void done(String output) {
                    if (output != null) {
                        finalOutput.append(output);
                    }
                }
            }, null, vars);
        } catch (ConfigCompileException | ConfigCompileGroupException ex) {
        // We already checked for compile errors, so this won't happen
        }
    } catch (ConfigRuntimeException e) {
        String name = e.getClass().getName();
        if (e instanceof AbstractCREException) {
            name = ((AbstractCREException) e).getName();
        }
        thrown = "\n(Throws " + name + ": " + e.getMessage() + ")";
    }
    String playerOut = playerOutput.toString().trim();
    String finalOut = finalOutput.toString().trim();
    String out = (playerOut.isEmpty() ? "" : playerOut) + (finalOut.isEmpty() || !playerOut.trim().isEmpty() ? "" : ":" + finalOut);
    if (thrown != null) {
        out += thrown;
    }
    return out;
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) Profiles(com.laytonsmith.core.Profiles) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Script(com.laytonsmith.core.Script) AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File)

Aggregations

AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 ArrayList (java.util.ArrayList)5 Environment (com.laytonsmith.core.environments.Environment)4 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)4 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)4 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)4 StackTraceManager (com.laytonsmith.core.exceptions.StackTraceManager)4 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)3 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)3 LoopManipulationException (com.laytonsmith.core.exceptions.LoopManipulationException)3 ProgramFlowManipulationException (com.laytonsmith.core.exceptions.ProgramFlowManipulationException)3 MCPlayer (com.laytonsmith.abstraction.MCPlayer)2 ParseTree (com.laytonsmith.core.ParseTree)2 CArray (com.laytonsmith.core.constructs.CArray)2 CNull (com.laytonsmith.core.constructs.CNull)2 Construct (com.laytonsmith.core.constructs.Construct)2 IVariable (com.laytonsmith.core.constructs.IVariable)2 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)2 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)2