Skip to content

Commit a724d0f

Browse files
committed
Update to 1.13.2
1 parent 82c06b4 commit a724d0f

13 files changed

Lines changed: 156 additions & 127 deletions

File tree

src/main/java/net/acomputerdog/webchat/PluginWebChat.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
import java.util.Calendar;
2121
import java.util.Date;
2222
import java.util.TimeZone;
23+
import java.util.logging.Level;
2324

25+
/**
26+
* Plugin main class
27+
*/
2428
public class PluginWebChat extends JavaPlugin implements Listener {
2529
/*
2630
Delay in milliseconds between chat messages
2731
*/
28-
public int chatDelay = 750;
29-
public int maxLines = 50;
30-
public int webPort = 8080;
32+
private int chatDelay = 750;
33+
private int maxLines = 50;
34+
private int webPort = 8080;
3135

3236
private WebServer webServer;
3337
private ChatList chatList;
@@ -46,20 +50,13 @@ public void onEnable() {
4650
calendar.setTimeZone(TimeZone.getTimeZone("EST"));
4751

4852
getLogger().info("Starting web server...");
49-
try {
50-
webServer = new WebServer(this);
51-
webServer.start();
52-
} catch (IOException e) {
53-
getLogger().severe("Exception starting web server!");
54-
e.printStackTrace();
55-
stopServer();
56-
}
53+
webServer = new WebServer(this);
54+
webServer.start();
5755
getLogger().info("Server started.");
5856

5957
getServer().getPluginManager().registerEvents(this, this);
6058
} catch (Exception e) {
61-
getLogger().severe("Exception during startup! Plugin will be disabled!");
62-
e.printStackTrace();
59+
getLogger().log(Level.SEVERE, "Exception during startup. Plugin will be disabled!", e);
6360
getServer().getPluginManager().disablePlugin(this);
6461
}
6562
}
@@ -85,7 +82,7 @@ private void loadConfig() throws IOException, InvalidConfigurationException {
8582

8683
YamlConfiguration conf = new YamlConfiguration();
8784
conf.load(new File(getDataFolder(), "filter.yml"));
88-
chatFilter = new ChatFilter(this, conf);
85+
chatFilter = new ChatFilter(conf);
8986
}
9087

9188
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -122,7 +119,7 @@ public void onServerCommand(ServerCommandEvent e) {
122119
*/
123120
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
124121
public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
125-
String command = e.getMessage();//getCommand();
122+
String command = e.getMessage();
126123
String cmdName = getCommandName(command).toLowerCase();
127124
String playerName = e.getPlayer().getName();
128125
if (cmdName.equals("/say")) {
@@ -156,7 +153,8 @@ private void stopServer() {
156153
if (webServer != null) {
157154
try {
158155
webServer.stop();
159-
} catch (Throwable ignored) {
156+
} catch (Exception e) {
157+
getLogger().log(Level.SEVERE, "Exception stopping web server", e);
160158
}
161159
}
162160
}
@@ -197,12 +195,6 @@ private String prefix(int val) {
197195
}
198196

199197
private String getCommandName(String command) {
200-
if (command == null) {
201-
return null;
202-
}
203-
if (command.isEmpty()) {
204-
return "";
205-
}
206198
int idx = command.indexOf(' ');
207199
if (idx > 0) {
208200
return command.substring(0, idx);
@@ -212,17 +204,27 @@ private String getCommandName(String command) {
212204
}
213205

214206
private String getCommandArgs(String command) {
215-
if (command == null) {
216-
return null;
217-
}
218-
if (command.isEmpty()) {
219-
return "";
220-
}
221207
int idx = command.indexOf(' ');
222208
if (idx > 0 && idx < command.length() - 1) {
223-
return command.substring(idx + 1, command.length());
209+
return command.substring(idx + 1);
224210
} else {
225211
return null;
226212
}
227213
}
214+
215+
public int getWebPort() {
216+
return webPort;
217+
}
218+
219+
public void stop() {
220+
getServer().getPluginManager().disablePlugin(this);
221+
}
222+
223+
public int getChatDelay() {
224+
return chatDelay;
225+
}
226+
227+
public int getMaxLines() {
228+
return maxLines;
229+
}
228230
}

src/main/java/net/acomputerdog/webchat/chat/ChatFilter.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package net.acomputerdog.webchat.chat;
22

3-
import net.acomputerdog.webchat.PluginWebChat;
43
import org.bukkit.configuration.Configuration;
54

65
import java.util.List;
76
import java.util.regex.Matcher;
87
import java.util.regex.Pattern;
98

9+
/**
10+
* Filters chat messages
11+
*/
1012
public class ChatFilter {
11-
private final PluginWebChat plugin;
12-
1313
private final boolean filterIn;
1414
private final boolean filterOut;
1515
private final boolean strictFilter;
@@ -18,8 +18,7 @@ public class ChatFilter {
1818
private final int maxLineLength;
1919
private final Pattern[] filterPatterns;
2020

21-
public ChatFilter(PluginWebChat plugin, Configuration conf) {
22-
this.plugin = plugin;
21+
public ChatFilter(Configuration conf) {
2322

2423
filterIn = conf.getBoolean("filter_in", true);
2524
filterOut = conf.getBoolean("filter_out", true);
@@ -71,25 +70,25 @@ private String filterPatterns(String line) {
7170
int end = matcher.end();
7271
int length = end - start;
7372

74-
String newLine;
73+
StringBuilder newLine = new StringBuilder();
7574
if (!strictFilter) {
76-
newLine = line.substring(0, start + 1);
75+
newLine.append(line, 0, start + 1);
7776
for (int i = 0; i < length - 2; i++) {
78-
newLine = newLine + "*";
77+
newLine.append('*');
7978
}
8079
if (end > 0 && end <= line.length()) {
81-
newLine += line.substring(end - 1, line.length());
80+
newLine.append(line.substring(end - 1));
8281
}
8382
} else {
84-
newLine = line.substring(0, start);
83+
newLine.append(line, 0, start);
8584
for (int i = 0; i < length; i++) {
86-
newLine = newLine + "*";
85+
newLine.append('*');
8786
}
8887
if (end < line.length()) {
89-
newLine = line.substring(end);
88+
newLine.append(end);
9089
}
9190
}
92-
line = newLine;
91+
line = newLine.toString();
9392
}
9493
}
9594
return line;

src/main/java/net/acomputerdog/webchat/chat/ChatList.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.util.concurrent.Semaphore;
77
import java.util.function.Consumer;
88

9+
/**
10+
* Stores list of chat messages
11+
*/
912
public class ChatList {
1013
private final BoundedSet<String> lines;
1114
private final Semaphore lock;
@@ -15,7 +18,7 @@ public class ChatList {
1518
private int version;
1619

1720
public ChatList(PluginWebChat plugin) {
18-
lines = new BoundedSet<>(plugin.maxLines);
21+
lines = new BoundedSet<>(plugin.getMaxLines());
1922
lock = new Semaphore(1, true);
2023
filter = plugin.getChatFilter();
2124
version = 0;

src/main/java/net/acomputerdog/webchat/net/WebServer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,37 @@
66

77
import java.io.IOException;
88
import java.net.InetSocketAddress;
9+
import java.net.URISyntaxException;
10+
import java.util.logging.Level;
911
import java.util.logging.Logger;
1012

13+
/**
14+
* HTTP web server
15+
*/
1116
public class WebServer {
1217
private final HttpServer server;
1318
private final Thread serverThread;
1419
private final Logger logger;
1520
private final PluginWebChat plugin;
1621

17-
public WebServer(PluginWebChat plugin) throws IOException {
22+
public WebServer(PluginWebChat plugin) throws IOException, URISyntaxException {
1823
this.plugin = plugin;
1924
this.logger = plugin.getLogger();
2025
//set timeouts. Is either 10 or 100 seconds (unsure do to closed source/undocumented code)
2126
//should still set in JVM arguments
2227
System.setProperty("sun.net.httpserver.maxReqTime", "10");
2328
System.setProperty("sun.net.httpserver.maxRspTime", "10");
24-
this.server = HttpServer.create(new InetSocketAddress(plugin.webPort), 0);
29+
this.server = HttpServer.create(new InetSocketAddress(plugin.getWebPort()), 0);
2530
this.serverThread = new Thread(() -> {
2631
try {
2732
server.start();
28-
} catch (Throwable t) {
29-
logger.severe("Uncaught exception in server thread! Server stopping!");
30-
t.printStackTrace();
31-
plugin.getServer().getPluginManager().disablePlugin(plugin);
33+
} catch (Exception e) {
34+
logger.log(Level.SEVERE, "Uncaught exception in server thread, server stopping.", e);
35+
plugin.stop();
3236
}
3337
});
3438
serverThread.setName("web_server");
35-
SimpleHandler main = new SimpleHandler(this, getClass().getResourceAsStream("/main.html"));
39+
SimpleHandler main = new SimpleHandler(this, getClass().getResource("/main.html").toURI());
3640
server.createContext("/", main);
3741
server.createContext("/main.html", main);
3842
server.createContext("/chat", new ChatHandler(this, plugin));

src/main/java/net/acomputerdog/webchat/net/handler/ChatHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
import java.io.IOException;
99

10+
/**
11+
* Outdated endpoint to retrieve entire chat log
12+
*
13+
* TODO why is this still here?
14+
*
15+
* @deprecated Inefficient, use ChatUpdateHandler instead
16+
*/
1017
@Deprecated
1118
public class ChatHandler extends WebHandler {
1219
private final PluginWebChat plugin;

src/main/java/net/acomputerdog/webchat/net/handler/ChatUpdateHandler.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import java.io.IOException;
99

10+
/**
11+
* API endpoint that retrieves all chat messages after a specified point in time
12+
*
13+
* TODO use actual time instead of version
14+
*/
1015
public class ChatUpdateHandler extends WebHandler {
1116
private final PluginWebChat plugin;
1217

@@ -17,34 +22,42 @@ public ChatUpdateHandler(WebServer server, PluginWebChat plugin) {
1722

1823
@Override
1924
public void handleExchange(HttpExchange exchange) throws IOException {
20-
if (!exchange.getRequestMethod().equals("GET")) {
21-
sendResponse(exchange, "405 Method not allowed: only GET is accepted.", 405);
22-
return;
23-
}
24-
String request = exchange.getRequestURI().getQuery();
25-
int version = getOldVersion(request);
26-
ChatList chat = plugin.getChatList();
27-
if (version != chat.getVersion()) {
28-
sendResponse(exchange, chat.toString());
29-
} else {
30-
sendEmptyResponse(exchange);
25+
try {
26+
if (!exchange.getRequestMethod().equals("GET")) {
27+
sendResponse(exchange, "405 Method not allowed: only GET is accepted.", 405);
28+
return;
29+
}
30+
String request = exchange.getRequestURI().getQuery();
31+
int version = getOldVersion(request);
32+
ChatList chat = plugin.getChatList();
33+
if (version != chat.getVersion()) {
34+
sendResponse(exchange, chat.toString());
35+
} else {
36+
sendEmptyResponse(exchange);
37+
}
38+
} catch (IllegalArgumentException e) {
39+
// TODO proper malformed request response
40+
sendErrorResponse(exchange, "Invalid query parameter");
3141
}
3242
}
3343

3444
private int getOldVersion(String request) {
35-
int version = 0;
3645
if (request != null) {
3746
String[] params = request.split("&");
3847
for (String param : params) {
3948
String[] var = param.split("=");
4049
if (var.length == 2 && "version".equals(var[0])) {
4150
try {
42-
version = Integer.parseInt(var[1]);
51+
return Integer.parseInt(var[1]);
4352
} catch (NumberFormatException ignored) {
53+
throw new IllegalArgumentException("version is not an integer");
4454
}
4555
}
4656
}
57+
58+
throw new IllegalArgumentException("chat version argument is missing");
59+
} else {
60+
throw new IllegalArgumentException("request is null");
4761
}
48-
return version;
4962
}
5063
}

src/main/java/net/acomputerdog/webchat/net/handler/ChatVersionHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.io.IOException;
99

10+
/**
11+
* API endpoint that gets the "version" of the current chat list
12+
*/
1013
public class ChatVersionHandler extends WebHandler {
1114
private final PluginWebChat plugin;
1215

0 commit comments

Comments
 (0)