Implement basic files and database management
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-10-22 16:29:32 +02:00
parent 175444970e
commit ccc45333f8
3 changed files with 118 additions and 1 deletions

View File

@ -6,6 +6,10 @@ import org.bukkit.plugin.java.JavaPlugin;
import wtf.beatrice.nounspicker.commands.NounsCommand;
import wtf.beatrice.nounspicker.objects.PAPIManager;
import wtf.beatrice.nounspicker.utils.ConsoleLogger;
import wtf.beatrice.nounspicker.utils.DatabaseManager;
import wtf.beatrice.nounspicker.utils.FileManager;
import java.io.File;
public class NounsPicker extends JavaPlugin
{
@ -28,7 +32,7 @@ public class NounsPicker extends JavaPlugin
logger.log("PlaceholderAPI integration loaded!");
} else {
logger.err("Missing PlaceholderAPI! The plugin cannot continue.");
pluginManager.disablePlugin(this);
disable();
return;
}
@ -36,6 +40,26 @@ public class NounsPicker extends JavaPlugin
getCommand("nouns").setExecutor(new NounsCommand(this));
logger.log("Commands registered!");
logger.log("Checking files...");
FileManager fileManager = new FileManager(this);
if (!fileManager.routineCheck())
{
logger.err("Error during routine file check! The plugin will halt.");
disable();
return;
}
logger.log("Files checked!");
logger.log("Connecting to database...");
String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite";
DatabaseManager dbManager = new DatabaseManager(dbFilePath);
if(dbManager.initialize())
{
logger.log("Database connection initialized!");
} else {
logger.err("Error initializing database connection!");
}
logger.log("Plugin loaded!");
}
@ -43,4 +67,9 @@ public class NounsPicker extends JavaPlugin
{
return instance;
}
private void disable()
{
pluginManager.disablePlugin(this);
}
}

View File

@ -0,0 +1,52 @@
package wtf.beatrice.nounspicker.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseManager
{
private final static String sqliteURL = "jdbc:sqlite:%path%";
private Connection dbConnection = null;
private ConsoleLogger logger;
private final String dbPath;
public DatabaseManager(String dbPath)
{
this.dbPath = dbPath;
logger = new ConsoleLogger(getClass());
}
public boolean initialize()
{
String url = sqliteURL.replace("%path%", dbPath);
if (dbConnection != null)
{
try {
if(!dbConnection.isClosed())
{
dbConnection.close();
}
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
dbConnection = null;
}
try {
dbConnection = DriverManager.getConnection(url);
logger.log("Database connection established!");
return true;
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
}
}

View File

@ -0,0 +1,36 @@
package wtf.beatrice.nounspicker.utils;
import wtf.beatrice.nounspicker.NounsPicker;
import java.io.File;
public class FileManager
{
private final NounsPicker plugin;
private final ConsoleLogger logger;
public FileManager(NounsPicker plugin)
{
this.plugin = plugin;
logger = new ConsoleLogger(getClass());
}
public boolean routineCheck()
{
File dataDir = plugin.getDataFolder();
logger.log("Checking plugin directory...");
if(!dataDir.exists())
{
logger.log("Creating plugin directory!");
if (dataDir.mkdirs())
{
logger.log("Plugin directory created!");
} else {
logger.err("Error creating plugin directory!");
return false;
}
}
return true;
}
}