diff --git a/build.gradle.kts b/build.gradle.kts index 30f2d4d..a3dd4af 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ val PLUGIN_NAME = "OriginBlacklist" val PLUGIN_IDEN = "originblacklist" val PLUGIN_DOMN = "xyz.webmc.$PLUGIN_IDEN" val PLUGIN_DESC = "An eaglercraft client blacklist plugin." -val PLUGIN_VERS = "2.0.7" +val PLUGIN_VERS = "2.0.8" val PLUGIN_SITE = "https://github.com/WebMCDevelopment/$PLUGIN_IDEN" val PLUGIN_DEPA = listOf("EaglercraftXServer") val PLUGIN_DEPB = listOf("EaglercraftXServer") diff --git a/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java b/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java index 6c7752d..82bd972 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java +++ b/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java @@ -114,7 +114,7 @@ public final class OriginBlacklist { } this.plugin.kickPlayer(this.getBlacklistedComponent("kick", blacklisted.getArrayString(), blacklisted.getAltString(), blacklisted.getString(), "not allowed", "not allowed on the server", - blacklisted_value, blacklisted.getActionString()), event); + blacklisted_value, blacklisted.getActionString(), false), event); this.sendWebhooks(event, blacklisted); final String name = player.getName(); if (isNonNull(name)) { @@ -136,9 +136,12 @@ public final class OriginBlacklist { } else { blacklisted_value = UNKNOWN_STR; } - this.plugin.setMOTD(this.getBlacklistedComponent("motd", blacklisted.getArrayString(), blacklisted.getAltString(), - blacklisted.getString(), "blacklisted", "blacklisted from the server", blacklisted_value, - blacklisted.getActionString()), event); + if (this.isMOTDEnabled()) { + this.plugin + .setMOTD(this.getBlacklistedComponent("motd", blacklisted.getArrayString(), blacklisted.getAltString(), + blacklisted.getString(), "blacklisted", "blacklisted from the server", blacklisted_value, + blacklisted.getActionString(), true), event); + } } } @@ -150,6 +153,14 @@ public final class OriginBlacklist { return this.config.getBoolean("bStats"); } + public final boolean isMOTDEnabled() { + return this.config.getBoolean("motd.enabled"); + } + + public final boolean isWebhooksEnabled() { + return this.config.getBoolean("discord.webhook.enabled"); + } + public final boolean isLogFileEnabled() { return this.config.getBoolean("logFile"); } @@ -165,13 +176,17 @@ public final class OriginBlacklist { public final void setEaglerMOTD(final Component comp, final OriginBlacklistMOTDEvent event) { final IMOTDConnection conn = event.getEaglerEvent().getMOTDConnection(); final List lst = new ArrayList<>(); - for (String ln : getComponentString(comp).split("\n")) { + for (final String ln : getComponentString(comp).split("\n")) { lst.add(ln); } + final List pLst = new ArrayList<>(); + for (final Json5Element ln : this.config.getArray("motd.players.hover").getAsJson5Array()) { + pLst.add(getLegacyFromMiniMessage(ln.getAsString().replaceAll("%discord_invite%", this.config.getString("discord.invite")))); + } conn.setServerMOTD(lst); - conn.setPlayerTotal(0); - conn.setPlayerUnlimited(); - conn.setPlayerList(List.of()); + conn.setPlayerTotal(this.config.getInteger("motd.players.online")); + conn.setPlayerMax(this.config.getInteger("motd.players.max")); + conn.setPlayerList(pLst); conn.setServerIcon(this.config.getIconBytes()); conn.sendToUser(); conn.disconnect(); @@ -345,8 +360,8 @@ public final class OriginBlacklist { private final Component getBlacklistedComponent(final String type, final String id, final String blockType, final String blockTypeAlt, final String notAllowed, final String notAllowedAlt, final String blockValue, - final String action) { - final Json5Array arr = this.config.getArray("messages." + type); + final String action, final boolean isMOTD) { + final Json5Array arr = this.config.getArray(isMOTD ? (type + ".text") : ("messages." + type)); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { if (i > 0) @@ -359,21 +374,26 @@ public final class OriginBlacklist { .replaceAll("%block_type%", blockType) .replaceAll("%not_allowed%", notAllowed) .replaceAll("%not_allowed_alt%", notAllowedAlt) - .replaceAll("%blocked_value%", blockValue); + .replaceAll("%blocked_value%", blockValue) + .replaceAll("%discord_invite%", this.config.getString("discord.invite")); return MiniMessage.miniMessage().deserialize(str); } private final void sendWebhooks(final OriginBlacklistLoginEvent event, final EnumBlacklistType type) { - if (this.config.getBoolean("discord.enabled")) { + if (this.isWebhooksEnabled()) { final OPlayer player = event.getPlayer(); final EnumConnectionType connType = event.getConnectionType(); - /* final String userAgent; - if (connType == EnumConnectionType.EAGLER) { - final IEaglerLoginConnection loginConn = event.getEaglerEvent().getLoginConnection(); - userAgent = loginConn.getWebSocketHeader(EnumWebSocketHeader.HEADER_USER_AGENT); - } else { - userAgent = UNKNOWN_STR; - } */ + /* + * final String userAgent; + * if (connType == EnumConnectionType.EAGLER) { + * final IEaglerLoginConnection loginConn = + * event.getEaglerEvent().getLoginConnection(); + * userAgent = + * loginConn.getWebSocketHeader(EnumWebSocketHeader.HEADER_USER_AGENT); + * } else { + * userAgent = UNKNOWN_STR; + * } + */ final byte[] payload = String.format( """ { @@ -466,17 +486,17 @@ public final class OriginBlacklist { private final void updateLogFile(final OriginBlacklistLoginEvent event, final EnumBlacklistType type) { if (this.isLogFileEnabled()) { final OPlayer player = event.getPlayer(); - final String txt = Instant.now() + " - [player=" + player.getName() + "," + "blacklist_reason=" + type.toString() + "]"; + final String txt = Instant.now() + " - [player=" + player.getName() + "," + "blacklist_reason=" + type.toString() + + "]"; final Path dir = Paths.get(this.getDataDir()); try { Files.createDirectories(dir); Files.writeString( - dir.resolve("blacklist.log"), - txt + "\n", - StandardOpenOption.CREATE, - StandardOpenOption.WRITE, - StandardOpenOption.APPEND - ); + dir.resolve("blacklist.log"), + txt + "\n", + StandardOpenOption.CREATE, + StandardOpenOption.WRITE, + StandardOpenOption.APPEND); } catch (final Throwable t) { t.printStackTrace(); } diff --git a/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java index 500468e..7b12155 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java +++ b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java @@ -22,7 +22,8 @@ import de.marhali.json5.Json5Object; import de.marhali.json5.Json5Primitive; public final class OriginBlacklistConfig { - private static final Json5Object DEFAULT_CONFIG = getDefaultConfig(); + public static final Json5Object DEFAULT_CONFIG = getDefaultConfig(); + public static final int LATEST_CONFIG_VERSION = 2; private final Json5 json5; private final File file; @@ -41,7 +42,7 @@ public final class OriginBlacklistConfig { .writeComments() .prettyPrinting() .build()); - + this.file = new File(plugin.getDataDir() + "/config.json5"); this.filePath = file.toPath(); this.iconFile = new File(plugin.getDataDir() + "/blacklisted.png"); @@ -73,6 +74,7 @@ public final class OriginBlacklistConfig { Json5Element parsed = this.json5.parse(text); if (parsed instanceof Json5Object) { this.config = (Json5Object) parsed; + this.config = OriginBlacklistConfigTransformer.transformConfig(this.config); merge(this.config, DEFAULT_CONFIG); } else { throw new IOException("Config must be an object!"); @@ -262,18 +264,29 @@ public final class OriginBlacklistConfig { kick.add("%action%"); kick.add(""); kick.add("Think this is a mistake? Join our discord:"); - kick.add("discord.gg/changethisintheconfig"); + kick.add("%discord_invite%"); addJSONObj(mObj, "kick", kick, null); - final Json5Array motd = new Json5Array(); - motd.add("This %block_type% is %not_allowed%!"); - motd.add("» %blocked_value%"); - addJSONObj(mObj, "motd", motd, null); final Json5Object actions = new Json5Object(); actions.add("generic", Json5Primitive.fromString("Please switch to a different %block_type%.")); actions.add("player_name", Json5Primitive.fromString("Please change your %block_type%.")); actions.add("ip_address", Json5Primitive.fromString("Please contact staff for assistance.")); addJSONObj(mObj, "actions", actions, null); addJSONObj(obj, "messages", mObj, null); + final Json5Object nObj = new Json5Object(); + addJSONObj(nObj, "enabled", Json5Primitive.fromBoolean(true), null); + final Json5Array mArr = new Json5Array(); + mArr.add("This %block_type% is %not_allowed%!"); + mArr.add("» %blocked_value%"); + addJSONObj(nObj, "text", mArr, null); + final Json5Object mPlayers = new Json5Object(); + addJSONObj(mPlayers, "online", Json5Primitive.fromNumber(0), null); + addJSONObj(mPlayers, "max", Json5Primitive.fromNumber(0), null); + final Json5Array hArr = new Json5Array(); + hArr.add("Join our discord"); + hArr.add("%discord_invite%"); + addJSONObj(mPlayers, "hover", hArr, null); + addJSONObj(nObj, "players", mPlayers, null); + addJSONObj(obj, "motd", nObj, null); final Json5Object bObj = new Json5Object(); final Json5Array origins = new Json5Array(); origins.add(".*eaglerhackedclients\\.vercel\\.app.*"); @@ -302,9 +315,12 @@ public final class OriginBlacklistConfig { addJSONObj(bObj, "ip_addresses", ips, null); addJSONObj(obj, "blacklist", bObj, null); final Json5Object dObj = new Json5Object(); - addJSONObj(dObj, "enabled", Json5Primitive.fromBoolean(false), null); - addJSONObj(dObj, "webhook_urls", new Json5Array(), null); - addJSONObj(dObj, "send_ips", Json5Primitive.fromBoolean(true), null); + addJSONObj(dObj, "invite", Json5Primitive.fromString("discord.gg/changeme"), null); + final Json5Object webhook = new Json5Object(); + addJSONObj(webhook, "enabled", Json5Primitive.fromBoolean(false), null); + addJSONObj(webhook, "webhook_urls", new Json5Array(), null); + addJSONObj(webhook, "send_ips", Json5Primitive.fromBoolean(true), null); + addJSONObj(dObj, "webhook", webhook, null); addJSONObj(obj, "discord", dObj, null); final Json5Object uObj = new Json5Object(); addJSONObj(uObj, "enabled", Json5Primitive.fromBoolean(true), null); @@ -317,7 +333,7 @@ public final class OriginBlacklistConfig { addJSONObj(obj, "block_undefined_origin", Json5Primitive.fromBoolean(false), null); addJSONObj(obj, "bStats", Json5Primitive.fromBoolean(true), null); addJSONObj(obj, "logFile", Json5Primitive.fromBoolean(true), null); - addJSONObj(obj, "config_version", Json5Primitive.fromNumber(1), "DO NOT CHANGE"); + addJSONObj(obj, "config_version", Json5Primitive.fromNumber(LATEST_CONFIG_VERSION), "DO NOT CHANGE"); return obj; } diff --git a/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfigTransformer.java b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfigTransformer.java new file mode 100644 index 0000000..de27559 --- /dev/null +++ b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfigTransformer.java @@ -0,0 +1,39 @@ +package xyz.webmc.originblacklist.base.config; + +import de.marhali.json5.Json5Array; +import de.marhali.json5.Json5Object; +import de.marhali.json5.Json5Primitive; + +public final class OriginBlacklistConfigTransformer { + public static final Json5Object transformConfig(final Json5Object config) { + final Json5Object obj = config.deepCopy(); + final int ver = obj.get("config_version").getAsInt(); + if (ver <= 1) { + final Json5Object mObj = obj.get("messages").getAsJson5Object().deepCopy(); + final Json5Array motd = mObj.get("motd").getAsJson5Array().deepCopy(); + mObj.remove("motd"); + final Json5Object nObj = new Json5Object(); + nObj.add("text", motd); + obj.remove("messages"); + obj.add("messages", mObj); + obj.add("motd", nObj); + final Json5Object dObj = obj.get("discord").getAsJson5Object().deepCopy(); + final Json5Primitive wEnabled = dObj.get("enabled").getAsJson5Primitive(); + final Json5Array wURLS = dObj.get("webhook_urls").getAsJson5Array().deepCopy(); + final Json5Primitive wAddrs = dObj.get("send_ips").getAsJson5Primitive(); + dObj.remove("enabled"); + dObj.remove("webhook_urls"); + dObj.remove("send_ips"); + final Json5Object oObj = new Json5Object(); + oObj.add("enabled", wEnabled); + oObj.add("webhook_urls", wURLS); + oObj.add("send_ips", wAddrs); + dObj.add("webhook", oObj); + obj.remove("discord"); + obj.add("discord", dObj); + } + obj.remove("config_version"); + obj.add("config_version", Json5Primitive.fromNumber(OriginBlacklistConfig.LATEST_CONFIG_VERSION)); + return obj; + } +}