Add basic DatabaseConnection to API

This commit is contained in:
len 2021-05-10 10:35:47 +02:00
parent 0d15c40cb7
commit ee1181d4ad
4 changed files with 119 additions and 6 deletions

View File

@ -1,12 +1,13 @@
package com.alttd.chat; package com.alttd.chat;
import com.alttd.chat.database.DatabaseConnection;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
public interface ChatAPI { public interface ChatAPI {
/*public static ChatAPI get() { ChatAPI get();
return ChatImplementation.INSTANCE;
}*/
LuckPerms getLuckPerms(); LuckPerms getLuckPerms();
DatabaseConnection getDataBase();
} }

View File

@ -1,23 +1,42 @@
package com.alttd.chat; package com.alttd.chat;
import com.alttd.chat.database.DatabaseConnection;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.LuckPermsProvider;
public class ChatImplementation implements ChatAPI{ public class ChatImplementation implements ChatAPI{
//public static final ChatAPI INSTANCE = new ChatImplementation(); private ChatAPI instance;
private LuckPerms luckPerms; private LuckPerms luckPerms;
private DatabaseConnection databaseConnection;
ChatImplementation() { ChatImplementation() {
instance = this;
// init database // init database
// init depends//or set them the first time they are called? // init depends//or set them the first time they are called?
} }
@Override
public ChatAPI get() {
if(instance == null)
instance = new ChatImplementation();
return instance;
}
@Override @Override
public LuckPerms getLuckPerms() { public LuckPerms getLuckPerms() {
if(luckPerms == null) if(luckPerms == null)
luckPerms = LuckPermsProvider.get(); luckPerms = LuckPermsProvider.get();
return luckPerms; return luckPerms;
} }
@Override
public DatabaseConnection getDataBase() {
if(databaseConnection == null)
databaseConnection = new DatabaseConnection();
return databaseConnection;
}
} }

View File

@ -0,0 +1,86 @@
package com.alttd.chat.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static DatabaseConnection instance;
private Connection connection;
private final String drivers, ip, port, database, username, password;
/**
* Sets information for the database and opens the connection.
*/
public DatabaseConnection() {
/*this.drivers = Config.drivers;
this.ip = Config.ip;
this.port = Config.port;
this.database = Config.database;
this.username = Config.username;
this.password = Config.password;*/
// temp to make compile, remove when config is added
this.drivers = this.ip = this.port = this.database = this.username = this.password = "";
instance = this;
try {
instance.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Opens the connection if it's not already open.
* @throws SQLException If it can't create the connection.
*/
public void openConnection() throws SQLException {
if (connection != null && !connection.isClosed()) {
return;
}
synchronized (this) {
if (connection != null && !connection.isClosed()) {
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
connection = DriverManager.getConnection(
"jdbc:" + drivers + "://" + ip + ":" + port + "/" + database + "?autoReconnect=true", username,
password);
}
}
/**
* Returns the connection for the database
* @return Returns the connection.
*/
public static Connection getConnection() {
try {
instance.openConnection();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return instance.connection;
}
/**
* Sets the connection for this instance
*/
public boolean initialize() {
instance = new DatabaseConnection();
return connection != null;
}
}

View File

@ -4,6 +4,7 @@ import com.alttd.chat.commands.GlobalAdminChat;
import com.alttd.chat.commands.GlobalChat; import com.alttd.chat.commands.GlobalChat;
import com.alttd.chat.commands.GlobalChatToggle; import com.alttd.chat.commands.GlobalChatToggle;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.handlers.ChatHandler; import com.alttd.chat.handlers.ChatHandler;
import com.alttd.chat.listeners.ChatListener; import com.alttd.chat.listeners.ChatListener;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -13,8 +14,6 @@ import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.File; import java.io.File;
@ -33,6 +32,7 @@ public class ChatPlugin {
private final Path dataDirectory; private final Path dataDirectory;
private ChatAPI chatAPI; private ChatAPI chatAPI;
private DatabaseConnection databaseConnection;
private ChatHandler chatHandler; private ChatHandler chatHandler;
@Inject @Inject
@ -48,6 +48,11 @@ public class ChatPlugin {
Config.init(getDataDirectory()); Config.init(getDataDirectory());
loadCommands(); loadCommands();
chatAPI = new ChatImplementation(); chatAPI = new ChatImplementation();
databaseConnection = chatAPI.getDataBase();
if (!databaseConnection.initialize()) {
// todo should we do this in the API or in the implementation?
return;
}
chatHandler = new ChatHandler(); chatHandler = new ChatHandler();
server.getEventManager().register(this, new ChatListener()); server.getEventManager().register(this, new ChatListener());
} }
@ -80,6 +85,8 @@ public class ChatPlugin {
return chatAPI; return chatAPI;
} }
public ChatHandler getChatHandler() { public ChatHandler getChatHandler() {
return chatHandler; return chatHandler;
} }