From 7a4cdcec3c98fa4d7a24455043b14e9c07cf5d38 Mon Sep 17 00:00:00 2001 From: itsTyrion Date: Sun, 24 Jan 2021 00:11:24 +0100 Subject: [PATCH] Now using NanoJSON instead of TOML4J for configuration. Changed default disconnect message to English --- .idea/AntiVPNVelocity.iml | 12 +++++++++- .idea/modules.xml | 8 +++++++ .../java/de/itsTyrion/antiVPN/AntiVPN.java | 24 +++++++++++-------- src/main/resources/config.json | 6 +++++ src/main/resources/config.toml | 3 --- 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 .idea/modules.xml create mode 100644 src/main/resources/config.json delete mode 100644 src/main/resources/config.toml diff --git a/.idea/AntiVPNVelocity.iml b/.idea/AntiVPNVelocity.iml index 0a52457..a55b617 100644 --- a/.idea/AntiVPNVelocity.iml +++ b/.idea/AntiVPNVelocity.iml @@ -1,2 +1,12 @@ - \ No newline at end of file + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ea70bf9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/de/itsTyrion/antiVPN/AntiVPN.java b/src/main/java/de/itsTyrion/antiVPN/AntiVPN.java index 9794984..fddb14f 100644 --- a/src/main/java/de/itsTyrion/antiVPN/AntiVPN.java +++ b/src/main/java/de/itsTyrion/antiVPN/AntiVPN.java @@ -1,6 +1,8 @@ package de.itsTyrion.antiVPN; -import com.moandjiezana.toml.Toml; +import com.grack.nanojson.JsonObject; +import com.grack.nanojson.JsonParser; +import com.grack.nanojson.JsonParserException; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.PreLoginEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; @@ -13,6 +15,7 @@ import javax.inject.Inject; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -24,20 +27,21 @@ public class AntiVPN { @Getter private static AntiVPN instance; @Getter - private static Toml config; + private static JsonObject config; @Getter private final Logger logger; @Inject - public AntiVPN(ProxyServer server, Logger logger, @DataDirectory Path folder) { + public AntiVPN(ProxyServer server, Logger logger, @DataDirectory Path folder) throws JsonParserException { this.server = server; this.logger = logger; instance = this; + try { config = loadConfig(folder); // try to load the configuration file } catch (IOException e) { e.printStackTrace(); - config = new Toml(); + config = new JsonObject(); } } @@ -53,23 +57,23 @@ public void onInit(ProxyInitializeEvent event) { * @throws IOException If the file could not be read or if the default config could not be written */ @SuppressWarnings("ResultOfMethodCallIgnored") // We don't need to know if a file was created - private Toml loadConfig(Path path) throws IOException { + private JsonObject loadConfig(Path path) throws IOException, JsonParserException { val folder = path.toFile(); - val file = new File(folder, "config.toml"); + val file = new File(folder, "config.json"); if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); if (!file.exists()) { try (val input = getClass().getResourceAsStream("/" + file.getName())) { - if (input != null) { - Files.copy(input, file.toPath()); + if (input == null) { + throw new IOException("Could not read config from jar. Please re-download."); } else { - file.createNewFile(); + Files.copy(input, file.toPath()); } } } - return new Toml().read(file); + return JsonParser.object().from(new FileInputStream(file)); } } \ No newline at end of file diff --git a/src/main/resources/config.json b/src/main/resources/config.json new file mode 100644 index 0000000..85bbc12 --- /dev/null +++ b/src/main/resources/config.json @@ -0,0 +1,6 @@ +{ + "kickMessage": "&cVPNs/Proxies are not allowed", + "__comment": "Cache duration in hours", + "ipCacheDuration": 6, + "logFailedAttempts": true +} \ No newline at end of file diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml deleted file mode 100644 index a165710..0000000 --- a/src/main/resources/config.toml +++ /dev/null @@ -1,3 +0,0 @@ -kickMessage = "&cVPNs/Proxies sind nicht erlaubt" # What message will players be disconnected with? -ipCacheDuration = 6 # How many hours should IP's be cached for? -logFailedAttempts = true # Log "*username* tried to join using a VPN/Proxy" or not \ No newline at end of file