-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Messaging.java
187 lines (157 loc) · 6.46 KB
/
Messaging.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
package net.citizensnpcs.api.util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Owner;
public class Messaging {
private static class DebugFormatter extends Formatter {
private final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
@Override
public String format(LogRecord rec) {
Throwable exception = rec.getThrown();
String out = this.date.format(rec.getMillis());
out += "[" + rec.getLevel().getName().toUpperCase() + "] ";
out += rec.getMessage() + '\n';
if (exception != null) {
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
return out + writer;
}
return out;
}
}
public static void configure(File debugFile, boolean debug, String messageColour, String highlightColour,
String errorColour) {
DEBUG = debug;
MESSAGE_COLOUR = Colorizer.parseColors(messageColour);
HIGHLIGHT_COLOUR = Colorizer.parseColors(highlightColour);
ERROR_COLOUR = Colorizer.parseColors(errorColour);
if (Bukkit.getLogger() != null) {
LOGGER = Bukkit.getLogger();
DEBUG_LOGGER = LOGGER;
}
if (debugFile != null) {
DEBUG_LOGGER = Logger.getLogger("CitizensDebug");
try {
FileHandler fh = new FileHandler(debugFile.getAbsolutePath(), true);
fh.setFormatter(new DebugFormatter());
DEBUG_LOGGER.setUseParentHandlers(false);
DEBUG_LOGGER.addHandler(fh);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void debug(Object... msg) {
if (isDebugging()) {
DEBUG_LOGGER.log(Level.INFO, SPACE.join(msg));
}
}
public static boolean isDebugging() {
return DEBUG;
}
private static void log(Level level, Object... msg) {
LOGGER.log(level, "[Citizens] " + SPACE.join(msg));
}
public static void log(Object... msg) {
log(Level.INFO, msg);
}
public static void logTr(String key, Object... msg) {
log(Level.INFO, Translator.translate(key, msg));
}
private static String prettify(String message) {
String trimmed = message.trim();
String messageColour = MESSAGE_COLOUR;
if (!trimmed.isEmpty()) {
if (trimmed.charAt(0) == ChatColor.COLOR_CHAR) {
ChatColor test = ChatColor.getByChar(trimmed.substring(1, 2));
if (test == null) {
message = messageColour + message;
} else
messageColour = test.toString();
} else {
message = messageColour + message;
}
}
message = message.replace("[[", HIGHLIGHT_COLOUR);
message = message.replace("{{", ERROR_COLOUR);
return CHAT_NEWLINE.matcher(message).replaceAll("<n>]]").replace("]]", messageColour);
}
public static void send(CommandSender sender, Object... msg) {
sendMessageTo(sender, SPACE.join(msg));
}
public static void sendError(CommandSender sender, Object... msg) {
send(sender, ERROR_COLOUR + SPACE.join(msg));
}
public static void sendErrorTr(CommandSender sender, String key, Object... msg) {
sendMessageTo(sender, ChatColor.RED + Translator.translate(key, msg));
}
private static void sendMessageTo(CommandSender sender, String rawMessage) {
if (sender instanceof Player) {
Player player = (Player) sender;
rawMessage = rawMessage.replace("<player>", player.getName());
rawMessage = rawMessage.replace("<world>", player.getWorld().getName());
}
rawMessage = Colorizer.parseColors(rawMessage);
for (String message : CHAT_NEWLINE_SPLITTER.split(rawMessage)) {
sender.sendMessage(prettify(message));
}
}
public static void sendTr(CommandSender sender, String key, Object... msg) {
sendMessageTo(sender, Translator.translate(key, msg));
}
public static void sendWithNPC(CommandSender sender, Object msg, NPC npc) {
String send = msg.toString();
send = send.replace("<owner>", npc.getTrait(Owner.class).getOwner());
send = send.replace("<npc>", npc.getName());
send = send.replace("<id>", Integer.toString(npc.getId()));
send(sender, send);
}
public static void severe(Object... messages) {
log(Level.SEVERE, messages);
}
public static void severeTr(String key, Object... messages) {
log(Level.SEVERE, Translator.translate(key, messages));
}
public static String tr(String key, Object... messages) {
return prettify(Translator.translate(key, messages));
}
public static String tryTranslate(Object possible) {
if (possible == null)
return "";
String message = possible.toString();
int count = 0;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c == '.')
count++;
}
return count >= 2 ? tr(message) : message;
}
private static final Pattern CHAT_NEWLINE = Pattern.compile("<br>|<n>|\\n", Pattern.MULTILINE);
private static final Splitter CHAT_NEWLINE_SPLITTER = Splitter.on(CHAT_NEWLINE);
private static boolean DEBUG = false;
private static Logger DEBUG_LOGGER;
private static String ERROR_COLOUR = ChatColor.RED.toString();
private static String HIGHLIGHT_COLOUR = ChatColor.YELLOW.toString();
private static Logger LOGGER = Logger.getLogger("Citizens");
private static String MESSAGE_COLOUR = ChatColor.GREEN.toString();
private static final Joiner SPACE = Joiner.on(" ").useForNull("null");
}