/
dB.java
307 lines (269 loc) · 12.2 KB
/
dB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package net.aufdemrand.denizen.utilities.debugging;
import net.aufdemrand.denizen.Settings;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* Preferred method of outputting debugger information with Denizen and
* denizen-related plugins.
*
* Attempts to unify the style of reporting information to the Console and
* player with the use of color, headers, footers, and formatting.
*
*
* Example, this code:
*
* dB.echoDebug(DebugElement.Header, "Sample debug information");
* dB.echoDebug("This is an example of a piece of debug information. Parts and pieces " +
* "of an entire debug sequence may be in completely different classes, so making " +
* "a unified way to output to the console can make a world of difference with " +
* "debugging and usability.");
* dB.echoDebug(DebugElement.Spacer);
* dB.echoDebug("Here are some examples of a few different ways to log with the logger.");
* dB.echoApproval("Notable events can nicely show success or approval.");
* dB.echoError("Your users will be able to easily distinguish problems.");
* dB.info("...and important pieces of information can be easily spotted.");
* dB.echoDebug(DebugElement.Footer);
*
*
* will produce this output (with color):
*
* 16:05:05 [INFO] +- Sample debug information ------+
* 16:05:05 [INFO] This is an example of a piece of debug information. Parts
* and pieces of an entire debug sequence may be in completely
* different classes, so making a unified way to output to the
* console can make a world of difference with debugging and
* usability.
* 16:05:05 [INFO]
* 16:05:05 [INFO] Here are some examples of a few different ways to log with the
* logger.
* 16:05:05 [INFO] OKAY! Notable events can nicely show success or approval.
* 16:05:05 [INFO] ERROR! Your users will be able to easily distinguish problems.
* 16:05:05 [INFO] +> ...and important pieces of information can easily be spotted.
* 16:05:05 [INFO] +---------------------+
*
*
* @author Jeremy Schroeder
*
*/
public class dB {
static ConsoleSender cs = new ConsoleSender();
public static boolean debugMode = Settings.ShowDebug();
public static boolean showStackTraces = true;
public static boolean showScriptBuilder = false;
public static boolean showColor = true;
/**
* Can be used with echoDebug(...) to output a header, footer,
* or a spacer.
*
* DebugElement.Header = +- string description ------+
* DebugElement.Spacer =
* DebugElement.Footer = +--------------+
*
* Also includes color.
*
*/
public static enum DebugElement {
Header, Footer, Spacer
}
public static void report(String name, String report) {
dB.echoDebug("<Y>+> <G>Executing '<Y>" + name + "<G>': " + report);
}
/**
* ConsoleSender sends dScript debugging information to the logger
* will attempt to intelligently wrap any debug information that is more
* than one line. This is used by the dB static methods which do some
* additional formatting.
*
* Example:
*
* 16:05:05 [INFO] +- Executing command: ENGRAVE/aufdemrand ------+
* 16:05:05 [INFO] ...set TYPE: 'TARGET:fullwall'
* 16:05:05 [INFO] Engraving 'DIAMOND_CHESTPLATE' with an inscription of
'Mastaba'.
* 16:05:05 [INFO] +---------------------+
* 16:05:05 [INFO] +- Executing command: SUPERZAP ------+
* 16:05:05 [INFO] ERROR! SUPERZAP is an invalid dScript command! Are you
sure the command loaded?
* 16:05:05 [INFO] +---------------------+
*
*/
private static class ConsoleSender {
// Bukkit CommandSender sends color nicely to the logger.
static CommandSender commandSender = null;
static boolean skipFooter = false;
public static void sendMessage(String string) {
if (commandSender == null) commandSender = Bukkit.getServer().getConsoleSender();
// These colors are used a lot in the debugging of commands/etc, so having a few shortcuts is nicer
// than having a bunch of ChatColor.XXXX
string = string.replace("<Y>", ChatColor.YELLOW + "").replace("<G>", ChatColor.DARK_GRAY + "")
.replace("<A>", ChatColor.AQUA + "");
// 'Hack-fix' for disallowing multiple 'footers' to print in a row
if (string.equals(ChatColor.LIGHT_PURPLE + "+---------------------+")) {
if (!skipFooter) skipFooter = true;
else {
return;
}
} else skipFooter = false;
// Create buffer for wrapping debug text nicely.
String[] words = string.split(" ");
String buffer = "";
int length = 0;
for (String word : words) { // # of total chars * # of lines - timestamp
if (length + ChatColor.stripColor(word).length() + 1 < 60) {
buffer = buffer + word + " ";
length = length + ChatColor.stripColor(word).length() + 1;
} else {
// Increase # of lines to account for
length = ChatColor.stripColor(word).length() + 1;
// Leave spaces to account for timestamp and indent
buffer = buffer + "\n" + " " + word + " ";
} // 16:05:06 [INFO]
}
// Send buffer to the player
if (showColor)
commandSender.sendMessage(buffer);
else commandSender.sendMessage(ChatColor.stripColor(buffer));
}
}
// TODO: Clean up messages.
// Plan is to have all core debugging dialog nicely organized here. Outside plugins which use the
// debugger can use this as well, but the main goal would be to ensure that all 'output' is uniform.
public enum Messages {
ERROR_NO_NPCID ("No 'NPCID:#' argument."),
ERROR_NO_PLAYER ("No 'PLAYER:player_name' argument."),
ERROR_NO_TEXT("No 'TEXT' argument."),
ERROR_NO_PLAYER_IN_SCRIPTENTRY("No Player object in the ScriptEntry. Use 'PLAYER:player_name' argument."),
ERROR_INVALID_ANCHOR("Missing 'TEXT' argument."),
ERROR_INVALID_NOTABLE("Missing 'TEXT' argument."),
ERROR_MISSING_LOCATION("Missing 'LOCATION'."),
ERROR_NO_SCRIPT("Missing 'SCRIPT' argument."),
ERROR_MISSING_OTHER("Missing '%s' argument."),
ERROR_LOTS_OF_ARGUMENTS("Too many arguments. Are there misplaced quotes?"),
ERROR_UNKNOWN_ARGUMENT("Unknown argument: '%s' Check spelling and syntax."),
DEBUG_SET_TEXT("...set TEXT: '%s'"),
DEBUG_TOGGLE("...TOGGLE: '%s'"),
DEBUG_SET_DURATION("...set DURATION: '%s'"),
DEBUG_SET_GLOBAL("...set GLOBAL."),
DEBUG_SET_SCRIPT("...set SCRIPT: '%s'"),
DEBUG_SET_QUANTITY("...set QUANTITY: '%s'"),
DEBUG_SET_LOCATION("...set LOCATION: '%s'"),
DEBUG_SET_TYPE("...set TYPE: '%s'"),
DEBUG_SET_RANGE("...set RANGE: '%s'"),
DEBUG_SET_ITEM("...set ITEM: '%s'"),
DEBUG_SET_NAME("...set NAME: '%s'"),
DEBUG_SET_RADIUS("...set RADIUS: '%s'"),
DEBUG_SET_COOLDOWN("...set COOLDOWN: '%s'"),
DEBUG_SET_COMMAND("...set COMMAND: '%s'"),
DEBUG_SET_FLAG_ACTION("...set FLAG ACTION: '%s'"),
DEBUG_SET_FLAG_TYPE("...set FLAG TYPE: '%s'"),
ERROR_INVALID_ENTITY("Invalid entity!"),
ERROR_INVALID_ITEM("Invalid item!"),
ERROR_PLAYER_NPCS_ONLY("NPC must be Human (Player)!"),
DEBUG_SET_STEP("...set STEP: '%s'"),
DEBUG_RANDOMLY_SELECTED_STEP("...randomly selected step '%s'"),
ERROR_CANCELLING_DELAYED_TASK("Unable to cancel previously delayed task!"),
DEBUG_RUNNING_DELAYED_TASK(ChatColor.YELLOW + "// DELAYED // Running delayed task '%s'"),
DEBUG_SETTING_DELAYED_TASK(ChatColor.YELLOW + "// DELAYED // Setting delayed task '%s'"),
ERROR_NO_WORLD("No valid world specified!"),
ERROR_INVALID_WORLD("Invalid world!");
@Override
public String toString() {
return error;
}
private String error;
private Messages(String error) {
this.error = error;
}
}
/*
* Communicate with the Logger
*/
public static void echoDebug(DebugElement element) {
echoDebug(element, null);
}
public static void echoDebug(Messages message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.WHITE + String.format(message.toString(), arg));
}
public static void echoDebug(DebugElement element, String string) {
if (!debugMode) return;
if (element == DebugElement.Footer)
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + "+---------------------+");
else if (element == DebugElement.Header)
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + "+- " + string + " ------+");
else if (element == DebugElement.Spacer)
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + "");
}
public static void echoDebug(String message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.WHITE + String.format(message, arg));
}
public static void echoDebug(String message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.WHITE + message);
}
public static void echoApproval(String message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.GREEN + "OKAY! " + ChatColor.WHITE + String.format(message, arg));
}
public static void echoApproval(String message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.GREEN + "OKAY! " + ChatColor.WHITE + message);
}
public static void echoError(String message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR! " + ChatColor.WHITE + message);
}
public static void echoError(Messages message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR! " + ChatColor.WHITE + message.toString());
}
public static void echoError(Messages message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.GREEN + "OKAY! " + ChatColor.WHITE + String.format(message.toString(), arg));
}
public static void echoError(String message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR! " + ChatColor.WHITE + String.format(message, arg));
}
@SuppressWarnings("restriction")
public static void log(String message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
: sun.reflect.Reflection.getCallerClass(2).getSimpleName()) + "] "
+ ChatColor.WHITE + String.format(message, arg));
}
@SuppressWarnings("restriction")
public static void log(Messages message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
: sun.reflect.Reflection.getCallerClass(2).getSimpleName()) + "] "
+ ChatColor.WHITE + String.format(message.toString(), arg));
}
@SuppressWarnings("restriction")
public static void log(String message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
: sun.reflect.Reflection.getCallerClass(2).getSimpleName()) + "] " + ChatColor.WHITE + message);
}
/*
* These methods do NOT require DebugMode to be enabled
*/
public static void notify(Player player, String message, String arg) {
player.sendMessage(ChatColor.YELLOW + "+> " + ChatColor.WHITE + String.format(message, arg));
}
public static void notify(Player player, String message) {
player.sendMessage(ChatColor.YELLOW + "+> " + ChatColor.WHITE + ChatColor.WHITE + message);
}
public static void toggle() {
debugMode = !debugMode;
}
}