mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2025-02-19 21:21:20 +01:00
Refactor backend loading a bit
This commit is contained in:
parent
accb7a89b8
commit
8dd2f4e551
@ -15,6 +15,8 @@ import org.appledash.saneeconomy.updates.GithubVersionChecker;
|
|||||||
import org.appledash.saneeconomy.utils.DatabaseCredentials;
|
import org.appledash.saneeconomy.utils.DatabaseCredentials;
|
||||||
import org.appledash.saneeconomy.utils.I18n;
|
import org.appledash.saneeconomy.utils.I18n;
|
||||||
import org.appledash.saneeconomy.vault.VaultHook;
|
import org.appledash.saneeconomy.vault.VaultHook;
|
||||||
|
import org.bukkit.configuration.Configuration;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -25,6 +27,8 @@ import java.util.logging.Logger;
|
|||||||
/**
|
/**
|
||||||
* Created by AppleDash on 6/13/2016.
|
* Created by AppleDash on 6/13/2016.
|
||||||
* Blackjack is still best pony.
|
* Blackjack is still best pony.
|
||||||
|
*
|
||||||
|
* FIXME: Why is the backend and config loading so complex and why is it even in this class?
|
||||||
*/
|
*/
|
||||||
public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
||||||
private static SaneEconomy instance;
|
private static SaneEconomy instance;
|
||||||
@ -103,10 +107,8 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
|||||||
getLogger().info("Initialized currency: " + currency.getPluralName());
|
getLogger().info("Initialized currency: " + currency.getPluralName());
|
||||||
|
|
||||||
getLogger().info("Initializing economy storage backend...");
|
getLogger().info("Initializing economy storage backend...");
|
||||||
String backendType = getConfig().getString("backend.type");
|
|
||||||
String oldBackendType = getConfig().getString("old-backend.type", null);
|
|
||||||
|
|
||||||
EconomyStorageBackend backend = loadBackend(backendType, "backend");
|
EconomyStorageBackend backend = loadBackend(getConfig().getConfigurationSection("backend"));
|
||||||
|
|
||||||
if (backend == null) {
|
if (backend == null) {
|
||||||
getLogger().severe("Failed to load backend!");
|
getLogger().severe("Failed to load backend!");
|
||||||
@ -117,9 +119,9 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
|||||||
backend.reloadDatabase();
|
backend.reloadDatabase();
|
||||||
getLogger().info("Data loaded!");
|
getLogger().info("Data loaded!");
|
||||||
|
|
||||||
if (!Strings.isNullOrEmpty(oldBackendType)) {
|
if (!Strings.isNullOrEmpty(getConfig().getString("old-backend.type", null))) {
|
||||||
getLogger().info("Old backend detected, converting... (This may take a minute or two.)");
|
getLogger().info("Old backend detected, converting... (This may take a minute or two.)");
|
||||||
EconomyStorageBackend oldBackend = loadBackend(oldBackendType, "old-backend");
|
EconomyStorageBackend oldBackend = loadBackend(getConfig().getConfigurationSection("old-backend"));
|
||||||
if (oldBackend == null) {
|
if (oldBackend == null) {
|
||||||
getLogger().severe("Failed to load old backend!");
|
getLogger().severe("Failed to load old backend!");
|
||||||
return false;
|
return false;
|
||||||
@ -137,28 +139,21 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private EconomyStorageBackend loadBackend(String backendType, String configPrefix) {
|
private EconomyStorageBackend loadBackend(ConfigurationSection config) {
|
||||||
EconomyStorageBackend backend;
|
EconomyStorageBackend backend;
|
||||||
|
String backendType = config.getString("type");
|
||||||
|
|
||||||
if (backendType.equalsIgnoreCase("flatfile")) {
|
if (backendType.equalsIgnoreCase("flatfile")) {
|
||||||
String backendFileName = getConfig().getString(configPrefix + ".file", "economy.db");
|
String backendFileName = config.getString("file", "economy.db");
|
||||||
File backendFile = new File(getDataFolder(), backendFileName);
|
File backendFile = new File(getDataFolder(), backendFileName);
|
||||||
backend = new EconomyStorageBackendFlatfile(backendFile);
|
backend = new EconomyStorageBackendFlatfile(backendFile);
|
||||||
getLogger().info("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
|
getLogger().info("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
|
||||||
} else if (backendType.equalsIgnoreCase("mysql")) {
|
} else if (backendType.equalsIgnoreCase("mysql")) {
|
||||||
String backendHost = getConfig().getString(configPrefix + ".host");
|
EconomyStorageBackendMySQL mySQLBackend = new EconomyStorageBackendMySQL(loadCredentials(config));
|
||||||
int backendPort = getConfig().getInt(configPrefix + ".port", 3306);
|
|
||||||
String backendDb = getConfig().getString(configPrefix + ".database");
|
|
||||||
String backendUser = getConfig().getString(configPrefix + ".username");
|
|
||||||
String backendPass = getConfig().getString(configPrefix + ".password");
|
|
||||||
|
|
||||||
EconomyStorageBackendMySQL mySQLBackend = new EconomyStorageBackendMySQL(new DatabaseCredentials(
|
|
||||||
backendHost, backendPort, backendUser, backendPass, backendDb
|
|
||||||
));
|
|
||||||
|
|
||||||
backend = mySQLBackend;
|
backend = mySQLBackend;
|
||||||
|
|
||||||
getLogger().info("Initialized MySQL backend to host " + backendHost);
|
getLogger().info("Initialized MySQL backend.");
|
||||||
getLogger().info("Testing connection...");
|
getLogger().info("Testing connection...");
|
||||||
if (!mySQLBackend.testConnection()) {
|
if (!mySQLBackend.testConnection()) {
|
||||||
getLogger().severe("MySQL connection failed - cannot continue!");
|
getLogger().severe("MySQL connection failed - cannot continue!");
|
||||||
@ -181,6 +176,26 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
|||||||
newer.waitUntilFlushed();
|
newer.waitUntilFlushed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TransactionLogger loadTransactionLogger() {
|
||||||
|
if (!getConfig().getBoolean("transaction-logger.enabled", false)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseCredentials loadCredentials(ConfigurationSection config) {
|
||||||
|
String backendHost = config.getString("host");
|
||||||
|
int backendPort = config.getInt("port", 3306);
|
||||||
|
String backendDb = config.getString("database");
|
||||||
|
String backendUser = config.getString("username");
|
||||||
|
String backendPass = config.getString("password");
|
||||||
|
|
||||||
|
return new DatabaseCredentials(
|
||||||
|
backendHost, backendPort, backendUser, backendPass, backendDb
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private void loadCommands() {
|
private void loadCommands() {
|
||||||
getLogger().info("Initializing commands...");
|
getLogger().info("Initializing commands...");
|
||||||
COMMANDS.forEach((name, command) -> getCommand(name).setExecutor(command));
|
COMMANDS.forEach((name, command) -> getCommand(name).setExecutor(command));
|
||||||
|
Loading…
Reference in New Issue
Block a user