use of com.laytonsmith.core.constructs.Construct 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.constructs.Construct in project CommandHelper by EngineHub.
the class Interpreter method doBuiltin.
public boolean doBuiltin(String script) {
List<String> args = StringUtils.ArgParser(script);
if (args.size() > 0) {
String command = args.get(0);
args.remove(0);
command = command.toLowerCase(Locale.ENGLISH);
switch(command) {
case "help":
pl(getHelpMsg());
pl("Shell builtins:");
pl("cd <dir> - Runs cd() with the provided argument.");
pl("s - equivalent to cd('..').");
pl("echo - Prints the arguments. If -e is set as the first argument, arguments are sent to colorize() first.");
pl("exit - Exits shellMode, and returns back to normal mscript mode.");
pl("logout - Exits the shell entirely with a return code of 0.");
pl("pwd - Runs pwd()");
pl("help - Prints this message.");
return true;
case "cd":
case "s":
if ("s".equals(command)) {
args.add("..");
}
if (args.size() > 1) {
pl(RED + "Too many arguments passed to cd");
return true;
}
Construct[] a = new Construct[0];
if (args.size() == 1) {
a = new Construct[] { new CString(args.get(0), Target.UNKNOWN) };
}
try {
new Cmdline.cd().exec(Target.UNKNOWN, env, a);
} catch (CREIOException ex) {
pl(RED + ex.getMessage());
}
return true;
case "pwd":
pl(new Cmdline.pwd().exec(Target.UNKNOWN, env).val());
return true;
case "exit":
// We need previous code to intercept, we cannot do this here.
throw new Error("I should not run");
case "logout":
new Cmdline.exit().exec(Target.UNKNOWN, env, new CInt(0, Target.UNKNOWN));
// won't actually run
return true;
case "echo":
// TODO Probably need some variable interpolation maybe? Otherwise, I don't think this command
// is actually useful as is, because this is not supposed to be a scripting environment.. that's
// what the normal shell is for.
boolean colorize = false;
if (args.size() > 0 && "-e".equals(args.get(0))) {
colorize = true;
args.remove(0);
}
String output = StringUtils.Join(args, " ");
if (colorize) {
output = new Echoes.colorize().exec(Target.UNKNOWN, env, new CString(output, Target.UNKNOWN)).val();
}
pl(output);
return true;
}
}
return false;
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class Manager method doAddEdit.
public static boolean doAddEdit(String key, String valueScript) {
try {
Environment env = Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
Construct c = MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(valueScript, null, true)), env, null, null);
String value = Construct.json_encode(c, Target.UNKNOWN);
pl(CYAN + "Adding: " + WHITE + value);
String[] k = key.split("\\.");
DaemonManager dm = new DaemonManager();
persistenceNetwork.set(dm, k, value);
try {
dm.waitForThreads();
} catch (InterruptedException e) {
//
}
return true;
} catch (Exception ex) {
pl(RED + ex.getMessage());
return false;
}
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class PNViewer method showData.
private void showData(final String key, String value) {
String[] split = key.split("\\.");
String[] namespace = new String[split.length - 1];
String keyPart = null;
for (int i = 0; i < split.length; i++) {
if (i == split.length - 1) {
keyPart = split[i];
} else {
namespace[i] = split[i];
}
}
namespaceLabel.setText(join(namespace));
keyLabel.setText(keyPart);
if (value == null) {
sourceLabel.setText("");
valueTypeLabel.setText("(empty key)");
valueTextArea.setText("");
} else {
sourceLabel.setText("(resolving)");
new Thread(new Runnable() {
@Override
public void run() {
final String source = network.getKeySource(key.split("\\.")).toString();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
sourceLabel.setText(source);
}
});
}
}).start();
Construct c = CNull.NULL;
try {
c = Construct.json_decode(value, Target.UNKNOWN);
} catch (MarshalException ex) {
Logger.getLogger(PNViewer.class.getName()).log(Level.SEVERE, null, ex);
}
valueTypeLabel.setText(new DataHandling.typeof().exec(Target.UNKNOWN, null, c).val());
valueTextArea.setText(c.val());
}
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class ObjectGenerator method fireworkEffect.
public MCFireworkEffect fireworkEffect(CArray fe, Target t) {
MCFireworkBuilder builder = StaticLayer.GetConvertor().GetFireworkBuilder();
if (fe.containsKey("flicker")) {
builder.setFlicker(Static.getBoolean(fe.get("flicker", t), t));
}
if (fe.containsKey("trail")) {
builder.setTrail(Static.getBoolean(fe.get("trail", t), t));
}
if (fe.containsKey("colors")) {
Construct colors = fe.get("colors", t);
if (colors instanceof CArray) {
CArray ccolors = (CArray) colors;
if (ccolors.size() == 0) {
builder.addColor(MCColor.WHITE);
} else {
for (Construct color : ccolors.asList()) {
MCColor mccolor;
if (color instanceof CString) {
mccolor = StaticLayer.GetConvertor().GetColor(color.val(), t);
} else if (color instanceof CArray) {
mccolor = color((CArray) color, t);
} else if (color instanceof CInt && ccolors.size() == 3) {
// Appears to be a single color
builder.addColor(color(ccolors, t));
break;
} else {
throw new CREFormatException("Expecting individual color to be an array or string, but found " + color.typeof(), t);
}
builder.addColor(mccolor);
}
}
} else if (colors instanceof CString) {
String[] split = colors.val().split("\\|");
if (split.length == 0) {
builder.addColor(MCColor.WHITE);
} else {
for (String s : split) {
builder.addColor(StaticLayer.GetConvertor().GetColor(s, t));
}
}
} else {
throw new CREFormatException("Expecting an array or string for colors parameter, but found " + colors.typeof(), t);
}
} else {
builder.addColor(MCColor.WHITE);
}
if (fe.containsKey("fade")) {
Construct colors = fe.get("fade", t);
if (colors instanceof CArray) {
CArray ccolors = (CArray) colors;
for (Construct color : ccolors.asList()) {
MCColor mccolor;
if (color instanceof CArray) {
mccolor = color((CArray) color, t);
} else if (color instanceof CString) {
mccolor = StaticLayer.GetConvertor().GetColor(color.val(), t);
} else if (color instanceof CInt && ccolors.size() == 3) {
// Appears to be a single color
builder.addFadeColor(color(ccolors, t));
break;
} else {
throw new CREFormatException("Expecting individual color to be an array or string, but found " + color.typeof(), t);
}
builder.addFadeColor(mccolor);
}
} else if (colors instanceof CString) {
String[] split = colors.val().split("\\|");
for (String s : split) {
builder.addFadeColor(StaticLayer.GetConvertor().GetColor(s, t));
}
} else {
throw new CREFormatException("Expecting an array or string for fade parameter, but found " + colors.typeof(), t);
}
}
if (fe.containsKey("type")) {
try {
builder.setType(MCFireworkType.valueOf(fe.get("type", t).val().toUpperCase()));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(ex.getMessage(), t, ex);
}
}
return builder.build();
}
Aggregations