Initial SQLite database support
This commit is contained in:
parent
0778e579d0
commit
02cfe9623a
|
@ -1,6 +1,7 @@
|
|||
package party._2a03.mc;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.registry.CommandRegistry;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import java.io.File;
|
||||
|
@ -15,6 +16,7 @@ import party._2a03.mc.command.HeadCommand;
|
|||
import party._2a03.mc.command.HomeCommand;
|
||||
import party._2a03.mc.command.SpawnCommand;
|
||||
import party._2a03.mc.util.Config;
|
||||
import party._2a03.mc.util.Database;
|
||||
|
||||
public class MinecraftTweaks2a03 implements ModInitializer {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
@ -34,13 +36,18 @@ public class MinecraftTweaks2a03 implements ModInitializer {
|
|||
}
|
||||
}
|
||||
Config.initConfig(configDir.toFile());
|
||||
Database.init(configDir.toFile());
|
||||
try {
|
||||
Config.loadConfig();
|
||||
Database.open();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error(e);
|
||||
return;
|
||||
}
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(minecraftServer -> {
|
||||
Database.close();
|
||||
});
|
||||
LOGGER.info("Registering 2a03.party commands");
|
||||
CommandRegistry.INSTANCE.register(false, dispatcher -> {
|
||||
ConfigCommand.register(dispatcher);
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package party._2a03.mc.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Database {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static Connection conn = null;
|
||||
private static File file;
|
||||
|
||||
public static void init(File configDir) {
|
||||
file = new File(configDir, "2a03.db");
|
||||
}
|
||||
|
||||
public static void open() throws Exception {
|
||||
LOGGER.info("Loading 2a03.party database");
|
||||
String url = "jdbc:sqlite:";
|
||||
url = url.concat(file.getPath());
|
||||
conn = DriverManager.getConnection(url);
|
||||
LOGGER.info("Database loaded");
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
LOGGER.info("Unloading 2a03.party database");
|
||||
try {
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue