Skip to content

Commit 00b1c99

Browse files
committed
add more motd customization
1 parent 1cd60a4 commit 00b1c99

File tree

4 files changed

+113
-38
lines changed

4 files changed

+113
-38
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ val PLUGIN_NAME = "OriginBlacklist"
1414
val PLUGIN_IDEN = "originblacklist"
1515
val PLUGIN_DOMN = "xyz.webmc.$PLUGIN_IDEN"
1616
val PLUGIN_DESC = "An eaglercraft client blacklist plugin."
17-
val PLUGIN_VERS = "2.0.7"
17+
val PLUGIN_VERS = "2.0.8"
1818
val PLUGIN_SITE = "https://github.com/WebMCDevelopment/$PLUGIN_IDEN"
1919
val PLUGIN_DEPA = listOf("EaglercraftXServer")
2020
val PLUGIN_DEPB = listOf("EaglercraftXServer")

src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public final void handleLogin(final OriginBlacklistLoginEvent event) {
114114
}
115115
this.plugin.kickPlayer(this.getBlacklistedComponent("kick", blacklisted.getArrayString(),
116116
blacklisted.getAltString(), blacklisted.getString(), "not allowed", "not allowed on the server",
117-
blacklisted_value, blacklisted.getActionString()), event);
117+
blacklisted_value, blacklisted.getActionString(), false), event);
118118
this.sendWebhooks(event, blacklisted);
119119
final String name = player.getName();
120120
if (isNonNull(name)) {
@@ -136,9 +136,12 @@ public final void handleMOTD(final OriginBlacklistMOTDEvent event) {
136136
} else {
137137
blacklisted_value = UNKNOWN_STR;
138138
}
139-
this.plugin.setMOTD(this.getBlacklistedComponent("motd", blacklisted.getArrayString(), blacklisted.getAltString(),
140-
blacklisted.getString(), "blacklisted", "blacklisted from the server", blacklisted_value,
141-
blacklisted.getActionString()), event);
139+
if (this.isMOTDEnabled()) {
140+
this.plugin
141+
.setMOTD(this.getBlacklistedComponent("motd", blacklisted.getArrayString(), blacklisted.getAltString(),
142+
blacklisted.getString(), "blacklisted", "blacklisted from the server", blacklisted_value,
143+
blacklisted.getActionString(), true), event);
144+
}
142145
}
143146
}
144147

@@ -150,6 +153,14 @@ public final boolean isMetricsEnabled() {
150153
return this.config.getBoolean("bStats");
151154
}
152155

156+
public final boolean isMOTDEnabled() {
157+
return this.config.getBoolean("motd.enabled");
158+
}
159+
160+
public final boolean isWebhooksEnabled() {
161+
return this.config.getBoolean("discord.webhook.enabled");
162+
}
163+
153164
public final boolean isLogFileEnabled() {
154165
return this.config.getBoolean("logFile");
155166
}
@@ -165,13 +176,17 @@ public final OriginBlacklistConfig getConfig() {
165176
public final void setEaglerMOTD(final Component comp, final OriginBlacklistMOTDEvent event) {
166177
final IMOTDConnection conn = event.getEaglerEvent().getMOTDConnection();
167178
final List<String> lst = new ArrayList<>();
168-
for (String ln : getComponentString(comp).split("\n")) {
179+
for (final String ln : getComponentString(comp).split("\n")) {
169180
lst.add(ln);
170181
}
182+
final List<String> pLst = new ArrayList<>();
183+
for (final Json5Element ln : this.config.getArray("motd.players.hover").getAsJson5Array()) {
184+
pLst.add(getLegacyFromMiniMessage(ln.getAsString().replaceAll("%discord_invite%", this.config.getString("discord.invite"))));
185+
}
171186
conn.setServerMOTD(lst);
172-
conn.setPlayerTotal(0);
173-
conn.setPlayerUnlimited();
174-
conn.setPlayerList(List.of());
187+
conn.setPlayerTotal(this.config.getInteger("motd.players.online"));
188+
conn.setPlayerMax(this.config.getInteger("motd.players.max"));
189+
conn.setPlayerList(pLst);
175190
conn.setServerIcon(this.config.getIconBytes());
176191
conn.sendToUser();
177192
conn.disconnect();
@@ -345,8 +360,8 @@ public final IEaglerXServerAPI getEaglerAPI() {
345360

346361
private final Component getBlacklistedComponent(final String type, final String id, final String blockType,
347362
final String blockTypeAlt, final String notAllowed, final String notAllowedAlt, final String blockValue,
348-
final String action) {
349-
final Json5Array arr = this.config.getArray("messages." + type);
363+
final String action, final boolean isMOTD) {
364+
final Json5Array arr = this.config.getArray(isMOTD ? (type + ".text") : ("messages." + type));
350365
final StringBuilder sb = new StringBuilder();
351366
for (int i = 0; i < arr.size(); i++) {
352367
if (i > 0)
@@ -359,21 +374,26 @@ private final Component getBlacklistedComponent(final String type, final String
359374
.replaceAll("%block_type%", blockType)
360375
.replaceAll("%not_allowed%", notAllowed)
361376
.replaceAll("%not_allowed_alt%", notAllowedAlt)
362-
.replaceAll("%blocked_value%", blockValue);
377+
.replaceAll("%blocked_value%", blockValue)
378+
.replaceAll("%discord_invite%", this.config.getString("discord.invite"));
363379
return MiniMessage.miniMessage().deserialize(str);
364380
}
365381

366382
private final void sendWebhooks(final OriginBlacklistLoginEvent event, final EnumBlacklistType type) {
367-
if (this.config.getBoolean("discord.enabled")) {
383+
if (this.isWebhooksEnabled()) {
368384
final OPlayer player = event.getPlayer();
369385
final EnumConnectionType connType = event.getConnectionType();
370-
/* final String userAgent;
371-
if (connType == EnumConnectionType.EAGLER) {
372-
final IEaglerLoginConnection loginConn = event.getEaglerEvent().getLoginConnection();
373-
userAgent = loginConn.getWebSocketHeader(EnumWebSocketHeader.HEADER_USER_AGENT);
374-
} else {
375-
userAgent = UNKNOWN_STR;
376-
} */
386+
/*
387+
* final String userAgent;
388+
* if (connType == EnumConnectionType.EAGLER) {
389+
* final IEaglerLoginConnection loginConn =
390+
* event.getEaglerEvent().getLoginConnection();
391+
* userAgent =
392+
* loginConn.getWebSocketHeader(EnumWebSocketHeader.HEADER_USER_AGENT);
393+
* } else {
394+
* userAgent = UNKNOWN_STR;
395+
* }
396+
*/
377397
final byte[] payload = String.format(
378398
"""
379399
{
@@ -466,17 +486,17 @@ private final void checkForUpdates() {
466486
private final void updateLogFile(final OriginBlacklistLoginEvent event, final EnumBlacklistType type) {
467487
if (this.isLogFileEnabled()) {
468488
final OPlayer player = event.getPlayer();
469-
final String txt = Instant.now() + " - [player=" + player.getName() + "," + "blacklist_reason=" + type.toString() + "]";
489+
final String txt = Instant.now() + " - [player=" + player.getName() + "," + "blacklist_reason=" + type.toString()
490+
+ "]";
470491
final Path dir = Paths.get(this.getDataDir());
471492
try {
472493
Files.createDirectories(dir);
473494
Files.writeString(
474-
dir.resolve("blacklist.log"),
475-
txt + "\n",
476-
StandardOpenOption.CREATE,
477-
StandardOpenOption.WRITE,
478-
StandardOpenOption.APPEND
479-
);
495+
dir.resolve("blacklist.log"),
496+
txt + "\n",
497+
StandardOpenOption.CREATE,
498+
StandardOpenOption.WRITE,
499+
StandardOpenOption.APPEND);
480500
} catch (final Throwable t) {
481501
t.printStackTrace();
482502
}

src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import de.marhali.json5.Json5Primitive;
2323

2424
public final class OriginBlacklistConfig {
25-
private static final Json5Object DEFAULT_CONFIG = getDefaultConfig();
25+
public static final Json5Object DEFAULT_CONFIG = getDefaultConfig();
26+
public static final int LATEST_CONFIG_VERSION = 2;
2627

2728
private final Json5 json5;
2829
private final File file;
@@ -41,7 +42,7 @@ public OriginBlacklistConfig(final OriginBlacklist plugin) {
4142
.writeComments()
4243
.prettyPrinting()
4344
.build());
44-
45+
4546
this.file = new File(plugin.getDataDir() + "/config.json5");
4647
this.filePath = file.toPath();
4748
this.iconFile = new File(plugin.getDataDir() + "/blacklisted.png");
@@ -73,6 +74,7 @@ private final void reloadConfigUnsafe() throws IOException {
7374
Json5Element parsed = this.json5.parse(text);
7475
if (parsed instanceof Json5Object) {
7576
this.config = (Json5Object) parsed;
77+
this.config = OriginBlacklistConfigTransformer.transformConfig(this.config);
7678
merge(this.config, DEFAULT_CONFIG);
7779
} else {
7880
throw new IOException("Config must be an object!");
@@ -262,18 +264,29 @@ private static final Json5Object getDefaultConfig() {
262264
kick.add("%action%");
263265
kick.add("");
264266
kick.add("<aqua>Think this is a mistake? Join our discord:</aqua>");
265-
kick.add("<blue>discord.gg/changethisintheconfig</blue>");
267+
kick.add("<blue>%discord_invite%</blue>");
266268
addJSONObj(mObj, "kick", kick, null);
267-
final Json5Array motd = new Json5Array();
268-
motd.add("<red>This %block_type% is %not_allowed%!</red>");
269-
motd.add("<dark_gray>»</dark_gray> <gray>%blocked_value%</gray>");
270-
addJSONObj(mObj, "motd", motd, null);
271269
final Json5Object actions = new Json5Object();
272270
actions.add("generic", Json5Primitive.fromString("<gold>Please switch to a different %block_type%.</gold>"));
273271
actions.add("player_name", Json5Primitive.fromString("<gold>Please change your %block_type%.</gold>"));
274272
actions.add("ip_address", Json5Primitive.fromString("<gold>Please contact staff for assistance.</gold>"));
275273
addJSONObj(mObj, "actions", actions, null);
276274
addJSONObj(obj, "messages", mObj, null);
275+
final Json5Object nObj = new Json5Object();
276+
addJSONObj(nObj, "enabled", Json5Primitive.fromBoolean(true), null);
277+
final Json5Array mArr = new Json5Array();
278+
mArr.add("<red>This %block_type% is %not_allowed%!</red>");
279+
mArr.add("<dark_gray>»</dark_gray> <gray>%blocked_value%</gray>");
280+
addJSONObj(nObj, "text", mArr, null);
281+
final Json5Object mPlayers = new Json5Object();
282+
addJSONObj(mPlayers, "online", Json5Primitive.fromNumber(0), null);
283+
addJSONObj(mPlayers, "max", Json5Primitive.fromNumber(0), null);
284+
final Json5Array hArr = new Json5Array();
285+
hArr.add("<blue>Join our discord</blue>");
286+
hArr.add("<blue>%discord_invite%</blue>");
287+
addJSONObj(mPlayers, "hover", hArr, null);
288+
addJSONObj(nObj, "players", mPlayers, null);
289+
addJSONObj(obj, "motd", nObj, null);
277290
final Json5Object bObj = new Json5Object();
278291
final Json5Array origins = new Json5Array();
279292
origins.add(".*eaglerhackedclients\\.vercel\\.app.*");
@@ -302,9 +315,12 @@ private static final Json5Object getDefaultConfig() {
302315
addJSONObj(bObj, "ip_addresses", ips, null);
303316
addJSONObj(obj, "blacklist", bObj, null);
304317
final Json5Object dObj = new Json5Object();
305-
addJSONObj(dObj, "enabled", Json5Primitive.fromBoolean(false), null);
306-
addJSONObj(dObj, "webhook_urls", new Json5Array(), null);
307-
addJSONObj(dObj, "send_ips", Json5Primitive.fromBoolean(true), null);
318+
addJSONObj(dObj, "invite", Json5Primitive.fromString("discord.gg/changeme"), null);
319+
final Json5Object webhook = new Json5Object();
320+
addJSONObj(webhook, "enabled", Json5Primitive.fromBoolean(false), null);
321+
addJSONObj(webhook, "webhook_urls", new Json5Array(), null);
322+
addJSONObj(webhook, "send_ips", Json5Primitive.fromBoolean(true), null);
323+
addJSONObj(dObj, "webhook", webhook, null);
308324
addJSONObj(obj, "discord", dObj, null);
309325
final Json5Object uObj = new Json5Object();
310326
addJSONObj(uObj, "enabled", Json5Primitive.fromBoolean(true), null);
@@ -317,7 +333,7 @@ private static final Json5Object getDefaultConfig() {
317333
addJSONObj(obj, "block_undefined_origin", Json5Primitive.fromBoolean(false), null);
318334
addJSONObj(obj, "bStats", Json5Primitive.fromBoolean(true), null);
319335
addJSONObj(obj, "logFile", Json5Primitive.fromBoolean(true), null);
320-
addJSONObj(obj, "config_version", Json5Primitive.fromNumber(1), "DO NOT CHANGE");
336+
addJSONObj(obj, "config_version", Json5Primitive.fromNumber(LATEST_CONFIG_VERSION), "DO NOT CHANGE");
321337
return obj;
322338
}
323339

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package xyz.webmc.originblacklist.base.config;
2+
3+
import de.marhali.json5.Json5Array;
4+
import de.marhali.json5.Json5Object;
5+
import de.marhali.json5.Json5Primitive;
6+
7+
public final class OriginBlacklistConfigTransformer {
8+
public static final Json5Object transformConfig(final Json5Object config) {
9+
final Json5Object obj = config.deepCopy();
10+
final int ver = obj.get("config_version").getAsInt();
11+
if (ver <= 1) {
12+
final Json5Object mObj = obj.get("messages").getAsJson5Object().deepCopy();
13+
final Json5Array motd = mObj.get("motd").getAsJson5Array().deepCopy();
14+
mObj.remove("motd");
15+
final Json5Object nObj = new Json5Object();
16+
nObj.add("text", motd);
17+
obj.remove("messages");
18+
obj.add("messages", mObj);
19+
obj.add("motd", nObj);
20+
final Json5Object dObj = obj.get("discord").getAsJson5Object().deepCopy();
21+
final Json5Primitive wEnabled = dObj.get("enabled").getAsJson5Primitive();
22+
final Json5Array wURLS = dObj.get("webhook_urls").getAsJson5Array().deepCopy();
23+
final Json5Primitive wAddrs = dObj.get("send_ips").getAsJson5Primitive();
24+
dObj.remove("enabled");
25+
dObj.remove("webhook_urls");
26+
dObj.remove("send_ips");
27+
final Json5Object oObj = new Json5Object();
28+
oObj.add("enabled", wEnabled);
29+
oObj.add("webhook_urls", wURLS);
30+
oObj.add("send_ips", wAddrs);
31+
dObj.add("webhook", oObj);
32+
obj.remove("discord");
33+
obj.add("discord", dObj);
34+
}
35+
obj.remove("config_version");
36+
obj.add("config_version", Json5Primitive.fromNumber(OriginBlacklistConfig.LATEST_CONFIG_VERSION));
37+
return obj;
38+
}
39+
}

0 commit comments

Comments
 (0)