From ee1181d4adae20e0780d719aad6080708257b524 Mon Sep 17 00:00:00 2001 From: len <40720638+destro174@users.noreply.github.com> Date: Mon, 10 May 2021 10:35:47 +0200 Subject: [PATCH] Add basic DatabaseConnection to API --- api/src/main/java/com/alttd/chat/ChatAPI.java | 7 +- .../com/alttd/chat/ChatImplementation.java | 21 ++++- .../chat/database/DatabaseConnection.java | 86 +++++++++++++++++++ .../main/java/com/alttd/chat/ChatPlugin.java | 11 ++- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/com/alttd/chat/database/DatabaseConnection.java diff --git a/api/src/main/java/com/alttd/chat/ChatAPI.java b/api/src/main/java/com/alttd/chat/ChatAPI.java index e377084..35d3eca 100644 --- a/api/src/main/java/com/alttd/chat/ChatAPI.java +++ b/api/src/main/java/com/alttd/chat/ChatAPI.java @@ -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(); } diff --git a/api/src/main/java/com/alttd/chat/ChatImplementation.java b/api/src/main/java/com/alttd/chat/ChatImplementation.java index f9dae8b..249b60a 100644 --- a/api/src/main/java/com/alttd/chat/ChatImplementation.java +++ b/api/src/main/java/com/alttd/chat/ChatImplementation.java @@ -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; + } + + } diff --git a/api/src/main/java/com/alttd/chat/database/DatabaseConnection.java b/api/src/main/java/com/alttd/chat/database/DatabaseConnection.java new file mode 100644 index 0000000..eb62ea8 --- /dev/null +++ b/api/src/main/java/com/alttd/chat/database/DatabaseConnection.java @@ -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; + } + +} \ No newline at end of file diff --git a/velocity/src/main/java/com/alttd/chat/ChatPlugin.java b/velocity/src/main/java/com/alttd/chat/ChatPlugin.java index 8a9eaf6..10f563d 100644 --- a/velocity/src/main/java/com/alttd/chat/ChatPlugin.java +++ b/velocity/src/main/java/com/alttd/chat/ChatPlugin.java @@ -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; }