Skip to content

Commit

Permalink
Use separate file for database configuration (#2356)
Browse files Browse the repository at this point in the history
* Migrate database configuration to separate file

* Don't write file if not in use
  • Loading branch information
Jikoo authored Sep 7, 2024
1 parent c7151e4 commit 1e5f48a
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@
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;
import java.util.HashMap;
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 1e5f48a

Please sign in to comment.