Search in sources :

Example 1 with CClosure

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() + ")";
}
Also used : CClosure(com.laytonsmith.core.constructs.CClosure) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString)

Example 2 with CClosure

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();
}
Also used : BukkitMCCommandTabCompleteEvent(com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents.BukkitMCCommandTabCompleteEvent) Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) ArrayList(java.util.ArrayList) List(java.util.List) 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 3 with CClosure

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;
}
Also used : CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CNull(com.laytonsmith.core.constructs.CNull)

Example 4 with CClosure

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

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)4 CClosure (com.laytonsmith.core.constructs.CClosure)4 Construct (com.laytonsmith.core.constructs.Construct)4 CString (com.laytonsmith.core.constructs.CString)3 Target (com.laytonsmith.core.constructs.Target)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)2 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)2 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)1 BukkitMCCommandTabCompleteEvent (com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents.BukkitMCCommandTabCompleteEvent)1 CBoolean (com.laytonsmith.core.constructs.CBoolean)1 CNull (com.laytonsmith.core.constructs.CNull)1 IVariable (com.laytonsmith.core.constructs.IVariable)1 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1