From 1d69f7d2a4f0981934afd38513cc19dd481e64d1 Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Fri, 25 Jun 2021 23:03:00 +0200 Subject: [PATCH] implement settings as Properties --- src/main/java/net/saltymc/eaa/EaaMod.java | 15 ++++++++++- .../saltymc/eaa/util/database/Postgre.java | 15 ++++++++--- .../saltymc/eaa/util/io/PropertieLoader.java | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/saltymc/eaa/util/io/PropertieLoader.java diff --git a/src/main/java/net/saltymc/eaa/EaaMod.java b/src/main/java/net/saltymc/eaa/EaaMod.java index 2c50258..d90d58b 100644 --- a/src/main/java/net/saltymc/eaa/EaaMod.java +++ b/src/main/java/net/saltymc/eaa/EaaMod.java @@ -2,6 +2,7 @@ package net.saltymc.eaa; import net.saltymc.eaa.commands.*; +import net.saltymc.eaa.util.io.PropertieLoader; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -9,15 +10,27 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import java.util.Properties; + @Environment(EnvType.CLIENT) public final class EaaMod implements ClientModInitializer { - public static final Logger LOGGER = LogManager.getLogger("EAA-MOD"); + private static final String SETTINGS_FILE_NAME = "settings.properties"; + + public static final Logger LOGGER = LogManager.getLogger("EAA-MOD"); + private static Properties settings; public static Logger getLogger() { return LOGGER; } + public static Properties getSettings(){ + if (settings == null) + settings = PropertieLoader.loadProperties(SETTINGS_FILE_NAME); + + return settings; + } + @Override public void onInitializeClient() { diff --git a/src/main/java/net/saltymc/eaa/util/database/Postgre.java b/src/main/java/net/saltymc/eaa/util/database/Postgre.java index cdea712..107c25c 100644 --- a/src/main/java/net/saltymc/eaa/util/database/Postgre.java +++ b/src/main/java/net/saltymc/eaa/util/database/Postgre.java @@ -7,15 +7,24 @@ import com.mysql.cj.jdbc.Driver; public class Postgre { - private final String url = "jdbc:postgresql://localhost/myDB"; - private final String user = "postgres"; - private final String password = "root"; + private String url; + private String user; + private String password; + private String db_name; + + private final String driverName = "com.mysql.cj.jdbc.Driver"; + private static Postgre postgre; private Connection connection; public Postgre() { + this.url = EaaMod.getSettings().getProperty("url"); + this.user = EaaMod.getSettings().getProperty("user"); + this.password = EaaMod.getSettings().getProperty("password"); + this.db_name = EaaMod.getSettings().getProperty("db_name"); + try { //load driver / config driver EaaMod.LOGGER.debug("Lade DB Treiber"); diff --git a/src/main/java/net/saltymc/eaa/util/io/PropertieLoader.java b/src/main/java/net/saltymc/eaa/util/io/PropertieLoader.java new file mode 100644 index 0000000..0ac7266 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/util/io/PropertieLoader.java @@ -0,0 +1,26 @@ +package net.saltymc.eaa.util.io; + +import net.saltymc.eaa.EaaMod; + +import java.io.FileReader; +import java.io.FileWriter; +import java.util.Properties; + +public class PropertieLoader { + + public static Properties loadProperties(String fileName){ + Properties p = new Properties(); + + try (FileReader reader = new FileReader(System.getProperty("user.dir") + "/mods/" + fileName)){ + p.load(reader); + } catch (Exception e){ + try (FileWriter writer = new FileWriter(System.getProperty("user.dir") + "/mods/" + fileName)) { + p.store(writer, "init config file"); + } catch (Exception ex){ + EaaMod.getLogger().error("Cant save Properties" + fileName, e); + } + return loadProperties(fileName); + } + return p; + } +} \ No newline at end of file