Search in sources :

Example 31 with Construct

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;
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString)

Example 32 with Construct

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;
}
Also used : CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) Echoes(com.laytonsmith.core.functions.Echoes) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Cmdline(com.laytonsmith.core.functions.Cmdline) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException)

Example 33 with Construct

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;
    }
}
Also used : DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) URISyntaxException(java.net.URISyntaxException) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) DataSourceException(com.laytonsmith.persistence.DataSourceException) IOException(java.io.IOException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException)

Example 34 with Construct

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());
    }
}
Also used : MarshalException(com.laytonsmith.core.exceptions.MarshalException) Construct(com.laytonsmith.core.constructs.Construct) DataHandling(com.laytonsmith.core.functions.DataHandling)

Example 35 with Construct

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();
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) MCColor(com.laytonsmith.abstraction.MCColor) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) MCFireworkBuilder(com.laytonsmith.abstraction.MCFireworkBuilder) CString(com.laytonsmith.core.constructs.CString) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) CString(com.laytonsmith.core.constructs.CString)

Aggregations

Construct (com.laytonsmith.core.constructs.Construct)42 CString (com.laytonsmith.core.constructs.CString)25 CArray (com.laytonsmith.core.constructs.CArray)23 IVariable (com.laytonsmith.core.constructs.IVariable)14 CInt (com.laytonsmith.core.constructs.CInt)11 CNull (com.laytonsmith.core.constructs.CNull)11 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)10 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Variable (com.laytonsmith.core.constructs.Variable)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)7 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)7 CBoolean (com.laytonsmith.core.constructs.CBoolean)6 Target (com.laytonsmith.core.constructs.Target)6 Environment (com.laytonsmith.core.environments.Environment)6 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)6 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)6