Search in sources :

Example 11 with Target

use of com.laytonsmith.core.constructs.Target 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 12 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class ConfigRuntimeException method DoWarning.

/**
 * To standardize the warning messages displayed, this function should be used. It checks the preference setting for
 * warnings to see if the warning should be shown to begin with, if checkPref is true. The exception is simply used
 * to get an error message, and is otherwise unused. If the exception is a ConfigRuntimeException, it is displayed
 * specially (including line number and file)
 *
 * @param e
 * @param optionalMessage
 * @throws NullPointerException If both the exception and message are null (or empty)
 */
public static void DoWarning(Exception e, String optionalMessage, boolean checkPrefs) {
    if (e == null && (optionalMessage == null || optionalMessage.isEmpty())) {
        throw new NullPointerException("Both the exception and the message cannot be empty");
    }
    if (!checkPrefs || Prefs.ShowWarnings()) {
        String exceptionMessage = "";
        Target t = Target.UNKNOWN;
        if (e instanceof ConfigRuntimeException) {
            ConfigRuntimeException cre = (ConfigRuntimeException) e;
            exceptionMessage = MCChatColor.YELLOW + cre.getMessage() + MCChatColor.WHITE + " :: " + MCChatColor.GREEN + AbstractCREException.getExceptionName(cre) + MCChatColor.WHITE + ":" + MCChatColor.YELLOW + cre.target.file() + MCChatColor.WHITE + ":" + MCChatColor.AQUA + cre.target.line();
            t = cre.getTarget();
        } else if (e != null) {
            exceptionMessage = MCChatColor.YELLOW + e.getMessage();
        }
        String message = exceptionMessage + MCChatColor.WHITE + optionalMessage;
        CHLog.GetLogger().Log(CHLog.Tags.GENERAL, LogLevel.WARNING, Static.MCToANSIColors(message) + TermColors.reset(), t);
    // Warnings are not shown to players ever
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target)

Example 13 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class ConfigRuntimeException method DoReport.

/**
 * If the Reaction returned by GetReaction is to report the exception, this function should be used to standardize
 * the report format. If the error message wouldn't be very useful by itself, or if a hint is desired, an optional
 * message may be provided (null otherwise).
 *
 * @param e
 * @param optionalMessage
 */
@SuppressWarnings("ThrowableResultIgnored")
private static void DoReport(String message, String exceptionType, ConfigRuntimeException ex, List<StackTraceElement> stacktrace, MCPlayer currentPlayer) {
    String type = exceptionType;
    if (exceptionType == null) {
        type = "FATAL";
    }
    List<StackTraceElement> st = new ArrayList<>(stacktrace);
    if (message == null) {
        message = "";
    }
    if (!"".equals(message.trim())) {
        message = ": " + message;
    }
    Target top = Target.UNKNOWN;
    for (StackTraceElement e : st) {
        Target t = e.getDefinedAt();
        if (top == Target.UNKNOWN) {
            top = t;
        }
    }
    StringBuilder log = new StringBuilder();
    StringBuilder console = new StringBuilder();
    StringBuilder player = new StringBuilder();
    PrintMessage(log, console, player, type, message, ex, st);
    if (ex != null) {
        // Otherwise, a CCE
        if (ex.getCause() != null && ex.getCause() instanceof ConfigRuntimeException) {
            ex = (ConfigRuntimeException) ex.getCause();
        }
        while (ex instanceof CRECausedByWrapper) {
            Target t = ex.getTarget();
            log.append("Caused by:\n");
            console.append(TermColors.CYAN).append("Caused by:\n");
            player.append(MCChatColor.AQUA).append("Caused by:\n");
            CArray exception = ((CRECausedByWrapper) ex).getException();
            CArray stackTrace = Static.getArray(exception.get("stackTrace", t), t);
            List<StackTraceElement> newSt = new ArrayList<>();
            for (Construct consElement : stackTrace.asList()) {
                CArray element = Static.getArray(consElement, t);
                int line = Static.getInt32(element.get("line", t), t);
                File file = new File(element.get("file", t).val());
                int col = element.getColumn();
                Target stElementTarget = new Target(line, file, col);
                newSt.add(new StackTraceElement(element.get("id", t).val(), stElementTarget));
            }
            String nType = exception.get("classType", t).val();
            String nMessage = exception.get("message", t).val();
            if (!"".equals(nMessage.trim())) {
                nMessage = ": " + nMessage;
            }
            PrintMessage(log, console, player, nType, nMessage, ex, newSt);
            ex = (ConfigRuntimeException) ex.getCause();
        }
    }
    // Log
    // Don't log to screen though, since we're ALWAYS going to do that ourselves.
    CHLog.GetLogger().Log("COMPILE ERROR".equals(exceptionType) ? CHLog.Tags.COMPILER : CHLog.Tags.RUNTIME, LogLevel.ERROR, log.toString(), top, false);
    // Console
    StreamUtils.GetSystemOut().println(console.toString() + TermColors.reset());
    // Player
    if (currentPlayer != null) {
        currentPlayer.sendMessage(player.toString());
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CRECausedByWrapper(com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper) ArrayList(java.util.ArrayList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) File(java.io.File)

Example 14 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class ConfigRuntimeException method PrintMessage.

private static void PrintMessage(StringBuilder log, StringBuilder console, StringBuilder player, String type, String message, Throwable ex, List<StackTraceElement> st) {
    log.append(type).append(message).append("\n");
    console.append(TermColors.RED).append(type).append(TermColors.WHITE).append(message).append("\n");
    player.append(MCChatColor.RED).append(type).append(MCChatColor.WHITE).append(message).append("\n");
    for (StackTraceElement e : st) {
        Target t = e.getDefinedAt();
        String proc = e.getProcedureName();
        File file = t.file();
        int line = t.line();
        int column = t.col();
        String filepath;
        String simplepath;
        if (file == null) {
            filepath = simplepath = "Unknown Source";
        } else {
            filepath = file.getPath();
            simplepath = file.getName();
        }
        log.append("\t").append(proc).append(":").append(filepath).append(":").append(line).append(".").append(column).append("\n");
        console.append("\t").append(TermColors.GREEN).append(proc).append(TermColors.WHITE).append(":").append(TermColors.YELLOW).append(filepath).append(TermColors.WHITE).append(":").append(TermColors.CYAN).append(line).append(".").append(column).append("\n");
        player.append("\t").append(MCChatColor.GREEN).append(proc).append(MCChatColor.WHITE).append(":").append(MCChatColor.YELLOW).append(simplepath).append(MCChatColor.WHITE).append(":").append(MCChatColor.AQUA).append(line).append(".").append(column).append("\n");
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) File(java.io.File)

Example 15 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class AbstractCREException method getFromCArray.

@SuppressWarnings({ "ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown" })
public static AbstractCREException getFromCArray(CArray exception, Target t) throws ClassNotFoundException {
    String classType = exception.get("classType", t).val();
    Class<? extends Mixed> clzz = NativeTypeList.getNativeClass(classType);
    Throwable cause = null;
    if (exception.get("causedBy", t) instanceof CArray) {
        // It has a cause
        cause = new CRECausedByWrapper((CArray) exception.get("causedBy", t));
    }
    String message = exception.get("message", t).val();
    List<StackTraceElement> st = new ArrayList<>();
    for (Construct consStElement : Static.getArray(exception.get("stackTrace", t), t).asList()) {
        CArray stElement = Static.getArray(consStElement, t);
        int line = Static.getInt32(stElement.get("line", t), t);
        File f = new File(stElement.get("file", t).val());
        // 
        int col = 0;
        st.add(new StackTraceElement(stElement.get("id", t).val(), new Target(line, f, col)));
    }
    // Now we have parsed everything into POJOs
    Class[] types = new Class[] { String.class, Target.class, Throwable.class };
    Object[] args = new Object[] { message, t, cause };
    AbstractCREException ex = (AbstractCREException) ReflectionUtils.newInstance(clzz, types, args);
    ex.stackTrace = st;
    return ex;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) ArrayList(java.util.ArrayList) Target(com.laytonsmith.core.constructs.Target) Construct(com.laytonsmith.core.constructs.Construct) File(java.io.File)

Aggregations

Target (com.laytonsmith.core.constructs.Target)17 CString (com.laytonsmith.core.constructs.CString)6 Construct (com.laytonsmith.core.constructs.Construct)6 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)6 ParseTree (com.laytonsmith.core.ParseTree)5 CArray (com.laytonsmith.core.constructs.CArray)5 CFunction (com.laytonsmith.core.constructs.CFunction)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)4 ArrayList (java.util.ArrayList)4 CKeyword (com.laytonsmith.core.constructs.CKeyword)3 Token (com.laytonsmith.core.constructs.Token)3 CClosure (com.laytonsmith.core.constructs.CClosure)2 CInt (com.laytonsmith.core.constructs.CInt)2 IVariable (com.laytonsmith.core.constructs.IVariable)2 Variable (com.laytonsmith.core.constructs.Variable)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)2 File (java.io.File)2 List (java.util.List)2