Should automatically add kvps when needed

This commit is contained in:
Paldiu 2022-03-01 15:41:10 -06:00
parent 4d624b4f3b
commit 9221442fc5
3 changed files with 50 additions and 21 deletions

View file

@ -6,6 +6,11 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import me.totalfreedom.tfguilds.command.*;
import me.totalfreedom.tfguilds.config.Config;
import me.totalfreedom.tfguilds.guild.Guild;
@ -34,7 +39,7 @@ public class TFGuilds extends JavaPlugin
public void onEnable()
{
plugin = this;
config = new Config("config.yml");
config = new Config(this,"config.yml");
sqlDatabase = new SQLDatabase(this);
User.loadAll();
Guild.loadAll();
@ -88,6 +93,19 @@ public class TFGuilds extends JavaPlugin
return commands;
}
private void forcedSQLPostLoad() {
ExecutorService ex = Executors.newCachedThreadPool();
Future<SQLDatabase> future = ex.submit(() -> new SQLDatabase(getPlugin()));
try
{
sqlDatabase = future.get();
}
catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
}
private void loadSubCommands()
{
subCommands.put("create", new CreateSubCommand());

View file

@ -22,10 +22,11 @@ public class Config extends YamlConfiguration
private final TFGuilds plugin;
private final String fileName;
public Config(String fileName)
public Config(TFGuilds plugin, String fileName)
{
this.fileName = fileName;
plugin = TFGuilds.getPlugin();
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), fileName);
if (!file.exists())
@ -67,10 +68,23 @@ public class Config extends YamlConfiguration
YamlConfiguration reader = YamlConfiguration.loadConfiguration(stream);
YamlConfiguration writer = YamlConfiguration.loadConfiguration(file);
AtomicBoolean shouldSave = new AtomicBoolean(false);
try {
reader.getKeys(true).forEach(key -> {
if (!writer.contains(key)) {
writer.set(key, reader.get(key));
}
if (!writer.contains(key)) writer.set(key, reader.get(key));
if (!shouldSave.get()) shouldSave.set(true);
});
if (shouldSave.get()) {
writer.save(file);
}
} catch (IOException ex) {
TFGuilds.getPlugin()
.getLogger()
.severe("Error attempting to verify configuration: \n"
+ ex.getMessage()
+ "\nCaused by: "
+ ex.getCause());
}
}
}

View file

@ -22,20 +22,17 @@ public class SQLDatabase
}
try
{
switch (ConfigEntry.CONNECTION_TYPE.getString().toLowerCase())
if ("mysql".equals(ConfigEntry.CONNECTION_TYPE.getString().toLowerCase()))
{
case "sqlite":
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " "));
break;
case "mysql":
connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s",
ConfigEntry.MYSQL_HOST.getString(),
ConfigEntry.MYSQL_PORT.getInteger(),
ConfigEntry.MYSQL_DATABASE.getString()),
ConfigEntry.MYSQL_USERNAME.getString(),
password);
break;
default:
}
else
{
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " "));
}