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