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();
}
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
}
}
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());
}
}
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");
}
}
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;
}
Aggregations