Add basic DatabaseConnection to API
This commit is contained in:
parent
0d15c40cb7
commit
ee1181d4ad
|
|
@ -1,12 +1,13 @@
|
|||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
|
||||
public interface ChatAPI {
|
||||
|
||||
/*public static ChatAPI get() {
|
||||
return ChatImplementation.INSTANCE;
|
||||
}*/
|
||||
ChatAPI get();
|
||||
|
||||
LuckPerms getLuckPerms();
|
||||
|
||||
DatabaseConnection getDataBase();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,42 @@
|
|||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
|
||||
public class ChatImplementation implements ChatAPI{
|
||||
|
||||
//public static final ChatAPI INSTANCE = new ChatImplementation();
|
||||
private ChatAPI instance;
|
||||
|
||||
private LuckPerms luckPerms;
|
||||
private DatabaseConnection databaseConnection;
|
||||
|
||||
ChatImplementation() {
|
||||
instance = this;
|
||||
// init database
|
||||
// init depends//or set them the first time they are called?
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatAPI get() {
|
||||
if(instance == null)
|
||||
instance = new ChatImplementation();
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LuckPerms getLuckPerms() {
|
||||
if(luckPerms == null)
|
||||
luckPerms = LuckPermsProvider.get();
|
||||
return luckPerms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseConnection getDataBase() {
|
||||
if(databaseConnection == null)
|
||||
databaseConnection = new DatabaseConnection();
|
||||
return databaseConnection;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import com.alttd.chat.commands.GlobalAdminChat;
|
|||
import com.alttd.chat.commands.GlobalChat;
|
||||
import com.alttd.chat.commands.GlobalChatToggle;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.chat.handlers.ChatHandler;
|
||||
import com.alttd.chat.listeners.ChatListener;
|
||||
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.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -33,6 +32,7 @@ public class ChatPlugin {
|
|||
private final Path dataDirectory;
|
||||
|
||||
private ChatAPI chatAPI;
|
||||
private DatabaseConnection databaseConnection;
|
||||
private ChatHandler chatHandler;
|
||||
|
||||
@Inject
|
||||
|
|
@ -48,6 +48,11 @@ public class ChatPlugin {
|
|||
Config.init(getDataDirectory());
|
||||
loadCommands();
|
||||
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();
|
||||
server.getEventManager().register(this, new ChatListener());
|
||||
}
|
||||
|
|
@ -80,6 +85,8 @@ public class ChatPlugin {
|
|||
return chatAPI;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ChatHandler getChatHandler() {
|
||||
return chatHandler;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user