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