From 1e5f48ab446f1de03c8b5477dbf3f08923ca0996 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 7 Sep 2024 15:28:01 -0400 Subject: [PATCH] Use separate file for database configuration (#2356) * Migrate database configuration to separate file * Don't write file if not in use --- .../GriefPrevention/GriefPrevention.java | 66 +++++++++++++++++-- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 73ae7b7..7def7c4 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -58,7 +58,10 @@ import org.jetbrains.annotations.Nullable; import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -66,12 +69,14 @@ import java.util.HashSet; import java.util.List; import java.util.Map.Entry; +import java.util.Properties; import java.util.Set; import java.util.UUID; import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -670,9 +675,7 @@ else if (world.getEnvironment() == Environment.NORMAL) this.config_pvp_protectPets = config.getBoolean("GriefPrevention.PvP.ProtectPetsOutsideLandClaims", false); //optional database settings - this.databaseUrl = config.getString("GriefPrevention.Database.URL", ""); - this.databaseUserName = config.getString("GriefPrevention.Database.UserName", ""); - this.databasePassword = config.getString("GriefPrevention.Database.Password", ""); + loadDatabaseSettings(config); this.config_advanced_fixNegativeClaimblockAmounts = config.getBoolean("GriefPrevention.Advanced.fixNegativeClaimblockAmounts", true); this.config_advanced_claim_expiration_check_rate = config.getInt("GriefPrevention.Advanced.ClaimExpirationCheckRate", 60); @@ -799,10 +802,6 @@ else if (world.getEnvironment() == Environment.NORMAL) outConfig.set("GriefPrevention.HardModeZombiesBreakDoors", this.config_zombiesBreakDoors); outConfig.set("GriefPrevention.MobProjectilesChangeBlocks", this.config_mobProjectilesChangeBlocks); - outConfig.set("GriefPrevention.Database.URL", this.databaseUrl); - outConfig.set("GriefPrevention.Database.UserName", this.databaseUserName); - outConfig.set("GriefPrevention.Database.Password", this.databasePassword); - outConfig.set("GriefPrevention.UseBanCommand", this.config_ban_useCommand); outConfig.set("GriefPrevention.BanCommandPattern", this.config_ban_commandFormat); @@ -864,6 +863,59 @@ else if (world.getEnvironment() == Environment.NORMAL) } } + private void loadDatabaseSettings(@NotNull FileConfiguration legacyConfig) + { + File databasePropsFile = new File(DataStore.dataLayerFolderPath, "database.properties"); + Properties databaseProps = new Properties(); + + // If properties file exists, use it - old config has already been migrated. + if (databasePropsFile.exists() && databasePropsFile.isFile()) + { + try (FileReader reader = new FileReader(databasePropsFile, StandardCharsets.UTF_8)) + { + // Load properties from file. + databaseProps.load(reader); + + // Set values from loaded properties. + databaseUrl = databaseProps.getProperty("jdbcUrl", ""); + databaseUserName = databaseProps.getProperty("username", ""); + databasePassword = databaseProps.getProperty("password", ""); + } + catch (IOException e) + { + getLogger().log(Level.SEVERE, "Unable to read database.properties", e); + } + + return; + } + + // Otherwise, database details may not have been migrated from legacy configuration. + // Try to load them. + databaseUrl = legacyConfig.getString("GriefPrevention.Database.URL", ""); + databaseUserName = legacyConfig.getString("GriefPrevention.Database.UserName", ""); + databasePassword = legacyConfig.getString("GriefPrevention.Database.Password", ""); + + // If not in use already, database settings are "secret" to discourage adoption until datastore is rewritten. + if (databaseUrl.isBlank()) { + return; + } + + // Set properties to loaded values. + databaseProps.setProperty("jdbcUrl", databaseUrl); + databaseProps.setProperty("username", databaseUserName); + databaseProps.setProperty("password", databasePassword); + + // Write properties file for future usage. + try (FileWriter writer = new FileWriter(databasePropsFile, StandardCharsets.UTF_8)) + { + databaseProps.store(writer, null); + } + catch (IOException e) + { + getLogger().log(Level.SEVERE, "Unable to write database.properties", e); + } + } + private ClaimsMode configStringToClaimsMode(String configSetting) { if (configSetting.equalsIgnoreCase("Survival"))