use of com.laytonsmith.core.constructs.CClosure in project CommandHelper by EngineHub.
the class AbstractFunction method profileMessage.
@Override
public String profileMessage(Construct... args) {
StringBuilder b = new StringBuilder();
boolean first = true;
for (Construct ccc : args) {
if (!first) {
b.append(", ");
}
first = false;
if (ccc instanceof CArray) {
// Arrays take too long to toString, so we don't want to actually toString them here if
// we don't need to.
b.append("<arrayNotShown size:" + ((CArray) ccc).size() + ">");
} else if (ccc instanceof CClosure) {
// The toString of a closure is too long, so let's not output them either.
b.append("<closureNotShown>");
} else if (ccc instanceof CString) {
String val = ccc.val().replace("\\", "\\\\").replace("'", "\\'");
int max = 1000;
if (val.length() > max) {
val = val.substring(0, max) + "... (" + (val.length() - max) + " more characters hidden)";
}
b.append("'").append(val).append("'");
} else if (ccc instanceof IVariable) {
b.append(((IVariable) ccc).getVariableName());
} else {
b.append(ccc.val());
}
}
return "Executing function: " + this.getName() + "(" + b.toString() + ")";
}
use of com.laytonsmith.core.constructs.CClosure in project CommandHelper by EngineHub.
the class BukkitMCCommand method handleTabComplete.
// I may be able to move these to c.l.c.f.Commands.java
@Override
public List<String> handleTabComplete(MCCommandSender sender, String alias, String[] args) {
if (Commands.onTabComplete.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.onTabComplete.get(cmd.getName().toLowerCase());
try {
closure.execute(new CString(alias, 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 CArray) {
List<String> ret = new ArrayList<>();
if (((CArray) fret).inAssociativeMode()) {
for (Construct key : ((CArray) fret).keySet()) {
ret.add(((CArray) fret).get(key, Target.UNKNOWN).val());
}
} else {
for (Construct value : ((CArray) fret).asList()) {
ret.add(value.val());
}
}
return ret;
}
} catch (ConfigRuntimeException cre) {
ConfigRuntimeException.HandleUncaughtException(cre, closure.getEnv());
return new ArrayList<>();
}
}
BukkitMCCommandTabCompleteEvent event = new BukkitMCCommandTabCompleteEvent(sender, cmd, alias, args);
EventUtils.TriggerListener(Driver.TAB_COMPLETE, "tab_complete_command", event);
return event.getCompletions();
}
use of com.laytonsmith.core.constructs.CClosure in project CommandHelper by EngineHub.
the class ConfigRuntimeException method GetReaction.
/**
* If a exception bubbles all the way up to the top, this should be called first, to see what reaction the plugin
* should take. Generally speaking, you'll want to use {@link #HandleUncaughtException} instead of this, though if
* you need to take custom action, you can determine the user's preferred reaction with this method.
*
* @param e
* @return
*/
public static Reaction GetReaction(ConfigRuntimeException e, Environment env) {
// If there is an exception handler, call it to see what it says.
Reaction reaction = Reaction.REPORT;
if (env.getEnv(GlobalEnv.class).GetExceptionHandler() != null) {
CClosure c = env.getEnv(GlobalEnv.class).GetExceptionHandler();
CArray ex = ObjectGenerator.GetGenerator().exception(e, env, Target.UNKNOWN);
if (e.getEnv() != null) {
MCCommandSender sender = e.getEnv().getEnv(CommandHelperEnvironment.class).GetCommandSender();
c.getEnv().getEnv(CommandHelperEnvironment.class).SetCommandSender(sender);
}
Construct ret = CNull.NULL;
try {
c.execute(new Construct[] { ex });
} catch (FunctionReturnException retException) {
ret = retException.getReturn();
}
if (ret instanceof CNull || Prefs.ScreamErrors()) {
reaction = Reaction.REPORT;
} else {
if (Static.getBoolean(ret, Target.UNKNOWN)) {
reaction = Reaction.IGNORE;
} else {
reaction = Reaction.FATAL;
}
}
}
return reaction;
}
use of com.laytonsmith.core.constructs.CClosure 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;
}
}
Aggregations