Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friend System work #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ The Core plugin for the Minecraft Minigame server, The Grubnest
2. Look at the To-do column and find a task that is assigned to you
3. Find an epic task. Epic tasks are tasks that have several subtasks that you must complete. Start here
4. Complete your task. For an epic task, start here but for each separate task issue
5. Create a new branch off of the `development`branch for your task. If it's a bug, the branch name should start with "hotfix". If it's an enhancement it should start with "feature".
5. Fork the development branch
5. Create a new branch off of the `development`branch you just cloned. If it's a bug, the branch name should start with "hotfix". If it's an enhancement it should start with "feature".
6. Make any changes you need to implement the issue
7. Create a Pull Request to merge your changes into the `development` branch.
8. Wait for your code to be approved. If you want to work on another issue in the meantime that requires work you did in this issue, when you make your new branch for the next task, make it off of the branch that is under review and not right off `development`. You can rebase the branch after it is merged.
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<scope>provided</scope>
<classifier>remapped-mojang</classifier>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.grubnest.game.core.DatabaseHandler;

import com.grubnest.game.core.DatabaseHandler.Utils.Deactivated;
import com.grubnest.game.core.DatabaseHandler.Utils.Disabler;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/** Handles connection from mysql and uses HikariCP
* @author tamilpp25
* @version 1.0 at 15-5-2022
*/
public class ConnectionPoolManager implements Deactivated {
private final MySQLData data;
private HikariDataSource dataSource;


public ConnectionPoolManager(MySQLData data) {
this.data = data;
setupPool();
Disabler.getInstance().registerDeactivated(this);
}

/**
* Setup mysql connection Pool with HikariCP to get multiple connections.
*/
private void setupPool() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(
"jdbc:mysql://" +
data.HOST +
":" +
data.PORT+
"/" +
data.DATABASE
);
config.setDriverClassName("com.mysql.jdbc.Driver");
config.setUsername(data.USERNAME);
config.setPassword(data.PASSWORD);
config.setMinimumIdle(data.minimumConnections);
config.setMaximumPoolSize(data.maximumConnections);
config.setConnectionTimeout(data.connectionTimeout);
config.setConnectionTestQuery("SELECT 1");
dataSource = new HikariDataSource(config);
}

/**
* Get MySQL Pool connection
* @return Sql Connection
* @throws SQLException if some error..
*/
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

/**
* Temp close method can close using try-with statement too
* Recommending using try-with statement to close automatically!
* @param conn Connection
* @param ps PreparedStatement
* @param res ResultSet
*/
public void close(Connection conn, PreparedStatement ps, ResultSet res) {
if (conn != null) try { conn.close(); } catch (SQLException ignored) {}
if (ps != null) try { ps.close(); } catch (SQLException ignored) {}
if (res != null) try { res.close(); } catch (SQLException ignored) {}
}

/**
* Close pool when everything done...
*/
public void closePool() {
if (dataSource != null && !dataSource.isClosed()) {
dataSource.close();
}
}

/**
* Auto disable classes on disable if multiple instance are there too
*/
@Override
public void onDisable() {
closePool();
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/grubnest/game/core/DatabaseHandler/MySQL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.grubnest.game.core.DatabaseHandler;

import com.grubnest.game.core.GrubnestCorePlugin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* Main class for handling mysql data
* make your methods for getting / setting here in async!
* new MySQL class can be initialized outside main class too!
*
* Recommend using getMySQL method from main class as it properly does the work and closes connection on disable!
* make a new instance of this if you really know what you are doing!
*
* @author tamilpp25
* @version 1.0 at 15-5-2022
*/
public class MySQL extends ConnectionPoolManager {
public MySQL(MySQLData data) {
super(data);
}

/**
* This is an example table query for creating queries make a similar method like this!
* MAKE SURE that all queries are run async so that it doesn't freeze the main thread
* you don't have to worry about closing a connection since its auto closed by
*/
public void testTableQuery() {
GrubnestCorePlugin.getInstance().getServer().getScheduler().runTaskAsynchronously(GrubnestCorePlugin.getInstance(),()->{
try (Connection conn = getConnection()){
PreparedStatement statement = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS `Test` " +
"(" +
"UUID varchar(30)" +
")"
);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
});
}

/**
* Close pool on plugin Disable
*/
public void onDisable(){
closePool();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.grubnest.game.core.DatabaseHandler;

/** MysqlData object to store credentials etc
* @author tamilpp25
* @version 1.0 at 15-5-2022
*/
public class MySQLData {
public final String HOST;
public final String USERNAME;
public final String PASSWORD;
public final String PORT;
public final String DATABASE;

public final int minimumConnections;
public final int maximumConnections;
public final long connectionTimeout;

public MySQLData(String host, String username, String password, String port, String database, int minimumConnections, int maximumConnections, long connectionTimeout) {
HOST = host;
USERNAME = username;
PASSWORD = password;
PORT = port;
DATABASE = database;
this.minimumConnections = minimumConnections;
this.maximumConnections = maximumConnections;
this.connectionTimeout = connectionTimeout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.grubnest.game.core.DatabaseHandler.Utils;

/**
* Utility class to prevent Memory leak.
* @author tamilpp25
* @version 1.0 at 15-5-2022
*/
public interface Deactivated {
void onDisable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.grubnest.game.core.DatabaseHandler.Utils;

import java.util.ArrayList;
import java.util.List;

/**
* Disabler instance to auto disable any used instance of sql
* @author tamilpp25
* @version 1.0 at 15-5-2022
*/
public class Disabler {
private static Disabler instance = null;
private final List<Deactivated> toDisable = new ArrayList<>();

public static Disabler getInstance() {
if (instance != null) return instance;
return instance = new Disabler();
}

public void disableAll() {
for (Deactivated toDeactivated : toDisable)
toDeactivated.onDisable();
}

public void disable(Deactivated sqlinstance) {
sqlinstance.onDisable();
}

public void registerDeactivated(Deactivated pDeactivated) {
toDisable.add(pDeactivated);
}

}
38 changes: 37 additions & 1 deletion src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
package com.grubnest.game.core;

import com.grubnest.game.core.DatabaseHandler.MySQL;
import com.grubnest.game.core.DatabaseHandler.MySQLData;
import com.grubnest.game.core.DatabaseHandler.Utils.Disabler;
import org.bukkit.plugin.java.JavaPlugin;

public class GrubnestCorePlugin extends JavaPlugin {

private MySQL sql;
private static GrubnestCorePlugin instance;
/**
* Runs when plugin is enabled
*/
@Override
public void onEnable() {
instance = this;

//Register Plugin messaging channels on enable
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessage());

getConfig().options().copyDefaults(true);
saveConfig();
sql = new MySQL(dataInitializer());
}

/**
* Runs when plugin is disabled
*/
@Override
public void onDisable() {
Disabler.getInstance().disableAll();
//Unregister channels on disable
this.getServer().getMessenger().unregisterOutgoingPluginChannel(this);
this.getServer().getMessenger().unregisterIncomingPluginChannel(this);
}

private MySQLData dataInitializer(){
String host = getConfig().getString("Database.hostname");
String port = getConfig().getString("Database.port");
String database = getConfig().getString("Database.database");
String username = getConfig().getString("Database.username");
String password = getConfig().getString("Database.password");

int minimumConnections = getConfig().getInt("Database.minimumConnections");
int maximumConnections = getConfig().getInt("Database.maximumConnections");
long connectionTimeout = getConfig().getLong("Database.connectionTimeout");
return new MySQLData(host,username,password,port,database,minimumConnections,maximumConnections,connectionTimeout);
}

public MySQL getMySQL(){
return sql;
}

public static GrubnestCorePlugin getInstance(){
return instance;
}
}
Loading