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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.command.*;
import me.totalfreedom.tfguilds.config.Config; import me.totalfreedom.tfguilds.config.Config;
import me.totalfreedom.tfguilds.guild.Guild; import me.totalfreedom.tfguilds.guild.Guild;
@ -34,7 +39,7 @@ public class TFGuilds extends JavaPlugin
public void onEnable() public void onEnable()
{ {
plugin = this; plugin = this;
config = new Config("config.yml"); config = new Config(this,"config.yml");
sqlDatabase = new SQLDatabase(this); sqlDatabase = new SQLDatabase(this);
User.loadAll(); User.loadAll();
Guild.loadAll(); Guild.loadAll();
@ -88,6 +93,19 @@ public class TFGuilds extends JavaPlugin
return commands; 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() private void loadSubCommands()
{ {
subCommands.put("create", new CreateSubCommand()); subCommands.put("create", new CreateSubCommand());

View file

@ -22,10 +22,11 @@ public class Config extends YamlConfiguration
private final TFGuilds plugin; private final TFGuilds plugin;
private final String fileName; private final String fileName;
public Config(String fileName) public Config(TFGuilds plugin, String fileName)
{ {
this.fileName = fileName; this.fileName = fileName;
plugin = TFGuilds.getPlugin(); this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), fileName); this.file = new File(plugin.getDataFolder(), fileName);
if (!file.exists()) if (!file.exists())
@ -67,10 +68,23 @@ public class Config extends YamlConfiguration
YamlConfiguration reader = YamlConfiguration.loadConfiguration(stream); YamlConfiguration reader = YamlConfiguration.loadConfiguration(stream);
YamlConfiguration writer = YamlConfiguration.loadConfiguration(file); YamlConfiguration writer = YamlConfiguration.loadConfiguration(file);
reader.getKeys(true).forEach(key -> { AtomicBoolean shouldSave = new AtomicBoolean(false);
if (!writer.contains(key)) {
writer.set(key, reader.get(key)); try {
reader.getKeys(true).forEach(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,21 +22,18 @@ public class SQLDatabase
} }
try try
{ {
switch (ConfigEntry.CONNECTION_TYPE.getString().toLowerCase()) if ("mysql".equals(ConfigEntry.CONNECTION_TYPE.getString().toLowerCase()))
{ {
case "sqlite": connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s",
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " ")); ConfigEntry.MYSQL_HOST.getString(),
break; ConfigEntry.MYSQL_PORT.getInteger(),
case "mysql": ConfigEntry.MYSQL_DATABASE.getString()),
connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s", ConfigEntry.MYSQL_USERNAME.getString(),
ConfigEntry.MYSQL_HOST.getString(), password);
ConfigEntry.MYSQL_PORT.getInteger(), }
ConfigEntry.MYSQL_DATABASE.getString()), else
ConfigEntry.MYSQL_USERNAME.getString(), {
password); connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " "));
break;
default:
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " "));
} }
createTables(); createTables();