From bf46564e46159ac47d4806b79852a4e0adfb749f Mon Sep 17 00:00:00 2001 From: aro Date: Sun, 29 Jan 2023 22:02:26 +0100 Subject: [PATCH] feat: switch to jackson for YAML config parsing --- nicko-core/dependency-reduced-pom.xml | 10 ++++ nicko-core/pom.xml | 22 ++++++++ .../nicko/config/Configuration.java | 6 ++- .../nicko/config/ConfigurationManager.java | 14 +++-- nicko-core/src/main/resources/config.yml | 53 +++++++++---------- 5 files changed, 71 insertions(+), 34 deletions(-) diff --git a/nicko-core/dependency-reduced-pom.xml b/nicko-core/dependency-reduced-pom.xml index 80a6a57..8f4cd54 100644 --- a/nicko-core/dependency-reduced-pom.xml +++ b/nicko-core/dependency-reduced-pom.xml @@ -35,6 +35,8 @@ net.wesjd:anvilgui de.studiocode.invui:* com.github.jsixface:* + com.fasterxml.jackson.dataformat + com.fasterxml.jackson.core @@ -50,6 +52,14 @@ com.github.jsixface net.artelnatif.libs.yaml + + com.fasterxml.jackson.dataformat + net.artelnatif.libs.jackson.yaml + + + com.fasterxml.jackson.core + net.artelnatif.libs.jackson.core + false diff --git a/nicko-core/pom.xml b/nicko-core/pom.xml index a3fb061..74941cb 100644 --- a/nicko-core/pom.xml +++ b/nicko-core/pom.xml @@ -111,6 +111,18 @@ yamlconfig 1.1.1 + + + com.fasterxml.jackson.core + jackson-core + 2.14.0-rc1 + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.14.0-rc1 + @@ -136,6 +148,8 @@ net.wesjd:anvilgui de.studiocode.invui:* com.github.jsixface:* + com.fasterxml.jackson.dataformat + com.fasterxml.jackson.core @@ -151,6 +165,14 @@ com.github.jsixface net.artelnatif.libs.yaml + + com.fasterxml.jackson.dataformat + net.artelnatif.libs.jackson.yaml + + + com.fasterxml.jackson.core + net.artelnatif.libs.jackson.core + false diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/config/Configuration.java b/nicko-core/src/main/java/net/artelnatif/nicko/config/Configuration.java index f0e606b..cba7486 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/config/Configuration.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/config/Configuration.java @@ -7,4 +7,8 @@ public record Configuration( String prefix, Boolean local, Boolean bungeecord, - Boolean customLocale) { } + Boolean customLocale) { + public Configuration() { + this("", "", "", "", false, false, false); + } +} diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/config/ConfigurationManager.java b/nicko-core/src/main/java/net/artelnatif/nicko/config/ConfigurationManager.java index b14b46f..9a2483a 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/config/ConfigurationManager.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/config/ConfigurationManager.java @@ -1,7 +1,8 @@ package net.artelnatif.nicko.config; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import net.artelnatif.nicko.Nicko; -import org.yaml.snakeyaml.Yaml; import java.io.*; import java.nio.file.Files; @@ -9,7 +10,7 @@ import java.nio.file.StandardCopyOption; public class ConfigurationManager { private final Nicko nicko; - private final Yaml yaml = new Yaml(); + private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); private final File directory; private final File file; @@ -21,17 +22,20 @@ public class ConfigurationManager { public void save(Configuration configuration) throws IOException { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { - yaml.dump(configuration, writer); + mapper.writeValue(writer, configuration); writer.flush(); } } public void saveDefaultConfig() { if (!file.exists()) { + System.out.println("FILE DOES NOT EXISTS"); try { - final InputStream input = ConfigurationManager.class.getResourceAsStream("config.yml"); + final InputStream input = getClass().getResourceAsStream("/config.yml"); if (input != null) { nicko.getLogger().info("Saved default configuration as config.yml"); + Files.createDirectories(file.getParentFile().toPath()); + Files.createFile(file.toPath()); Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } } catch (IOException e) { @@ -42,7 +46,7 @@ public class ConfigurationManager { public Configuration load() throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - return yaml.loadAs(reader, Configuration.class); + return mapper.readValue(reader, Configuration.class); } } } diff --git a/nicko-core/src/main/resources/config.yml b/nicko-core/src/main/resources/config.yml index 36c6271..2c8ae41 100644 --- a/nicko-core/src/main/resources/config.yml +++ b/nicko-core/src/main/resources/config.yml @@ -13,39 +13,36 @@ # GENERAL # ########### -common: - storage: - # SQL database's address - # Accepted values: valid IP address (e.g. localhost, 127.0.0.1) - address: "localhost" - # SQL database's username. - # Accepted values: any string - username: "" - # SQL database's password. - # Accepted values: any string - password: "" +# SQL database's address +# Accepted values: valid IP address (e.g. localhost, 127.0.0.1) +address: "localhost" +# SQL database's username. +# Accepted values: any string +username: "" +# SQL database's password. +# Accepted values: any string +password: "" ################# # BUKKIT/SPIGOT # ################# -bukkit: - # Nicko's messages prefix. - # Accepted values: any string - prefix: "§8[§6Nicko§8] " +# Nicko's messages prefix. +# Accepted values: any string +prefix: "§8[§6Nicko§8] " - # Indicates wherever the data will be stored - # locally through a .json file or a (My)SQL database. - # Accepted values: false (Disabled), true (Enabled) - local: true +# Indicates wherever the data will be stored +# locally through a .json file or a (My)SQL database. +# Accepted values: false (Disabled), true (Enabled) +local: true - # Enables Bungeecord support, switching - # servers will transfer player's disguise. - # Accepted values: false (Disabled), true (Enabled) - bungeecord: false +# Enables Bungeecord support, switching +# servers will transfer player's disguise. +# Accepted values: false (Disabled), true (Enabled) +bungeecord: false - # Nicko will copy the English locale as "lang.yml" - # and will use the translations in that file when "Server Custom" - # is selected as the player's locale. - # Accepted values: false (Disabled), true (Enabled) - customLocale: false \ No newline at end of file +# Nicko will copy the English locale as "lang.yml" +# and will use the translations in that file when "Server Custom" +# is selected as the player's locale. +# Accepted values: false (Disabled), true (Enabled) +customLocale: false \ No newline at end of file