implement settings as Properties

This commit is contained in:
Hiajen Hiajen 2021-06-25 23:03:00 +02:00
parent df9060798d
commit 1d69f7d2a4
3 changed files with 52 additions and 4 deletions

View file

@ -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() {

View file

@ -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");

View file

@ -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;
}
}