Maven + HikariCP

Switch the project to use Maven, and also switch to using HikariCP as
the database connection manager.
This commit is contained in:
Esophose 2018-03-01 01:58:04 -07:00
parent 671312c3cf
commit 5dc0cd7bb2
9 changed files with 213 additions and 237 deletions

4
.gitignore vendored
View file

@ -34,10 +34,10 @@ Icon
# Personal stuff
*.project
*.classpath
*.jardesc
/bin
/.settings
/doc
/doc
/target
# Files that might appear on external disk
.Spotlight-V100

91
pom.xml Normal file
View file

@ -0,0 +1,91 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.esophose.playerparticles</groupId>
<artifactId>PlayerParticles</artifactId>
<version>5</version>
<name>PlayerParticles</name>
<url>https://github.com/Esophose/PlayerParticles</url>
<description>Display particles around your player using customized styles and data!</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.zaxxer:HikariCP</include>
<include>org.slf4j:slf4j-api</include>
<include>org.slf4j:slf4j-nop</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<outputFile>C:\Users\Esophose\Desktop\1.12.2 Dev Server\plugins\update\PlayerParticles v5.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.4-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View file

@ -28,6 +28,8 @@ import com.esophose.playerparticles.gui.PlayerParticlesGui.GuiState;
import com.esophose.playerparticles.manager.ConfigManager;
import com.esophose.playerparticles.manager.MessageManager;
import com.esophose.playerparticles.manager.MessageManager.MessageType;
import com.esophose.playerparticles.manager.ParticleManager;
import com.esophose.playerparticles.manager.PermissionManager;
import com.esophose.playerparticles.particles.FixedParticleEffect;
import com.esophose.playerparticles.particles.ParticleEffect;
import com.esophose.playerparticles.particles.ParticleEffect.BlockData;
@ -35,8 +37,6 @@ import com.esophose.playerparticles.particles.ParticleEffect.ItemData;
import com.esophose.playerparticles.particles.ParticleEffect.NoteColor;
import com.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor;
import com.esophose.playerparticles.particles.ParticleEffect.ParticleProperty;
import com.esophose.playerparticles.manager.ParticleManager;
import com.esophose.playerparticles.manager.PermissionManager;
import com.esophose.playerparticles.styles.DefaultStyles;
import com.esophose.playerparticles.styles.api.ParticleStyle;
import com.esophose.playerparticles.styles.api.ParticleStyleManager;
@ -662,7 +662,7 @@ public class ParticleCommandExecutor implements CommandExecutor {
}
}
}
FixedParticleEffect fixedEffect = new FixedParticleEffect(p.getUniqueId(), // @formatter:off
ConfigManager.getInstance().getNextFixedEffectId(p.getUniqueId()),
p.getLocation().getWorld().getName(), xPos, yPos, zPos,

View file

@ -50,7 +50,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.esophose.playerparticles.gui.PlayerParticlesGui;
import com.esophose.playerparticles.library.MySQL;
import com.esophose.playerparticles.manager.DatabaseManager;
import com.esophose.playerparticles.manager.MessageManager;
import com.esophose.playerparticles.manager.ParticleManager;
import com.esophose.playerparticles.styles.DefaultStyles;
@ -66,9 +66,9 @@ public class PlayerParticles extends JavaPlugin {
public static String updateVersion = null;
/**
* The MySQL connection
* The MySQL database connection manager
*/
public static MySQL mySQL = null;
public static DatabaseManager mySQL = null;
/**
* Whether or not to use MySQL as determined in the config
@ -128,13 +128,7 @@ public class PlayerParticles extends JavaPlugin {
*/
public void onDisable() {
if (useMySQL) {
try {
if (mySQL.checkConnection()) {
mySQL.closeConnection();
}
} catch (SQLException ex) {
getLogger().warning("An error occurred while cleaning up the mySQL database connection.");
}
mySQL.closeConnection();
}
PlayerParticlesGui.forceCloseAllOpenGUIs();
@ -167,12 +161,7 @@ public class PlayerParticles extends JavaPlugin {
*/
private void checkDatabase() {
if (getConfig().getBoolean("database-enable")) {
String hostname = getConfig().getString("database-hostname");
String port = getConfig().getString("database-port");
String database = getConfig().getString("database-name");
String user = getConfig().getString("database-user-name");
String pass = getConfig().getString("database-user-password");
mySQL = new MySQL(hostname, port, database, user, pass);
mySQL = new DatabaseManager(getConfig());
useMySQL = true; // If something goes wrong this will be set to false
@ -199,7 +188,7 @@ public class PlayerParticles extends JavaPlugin {
"CREATE TABLE pp_data_note (uuid VARCHAR(36), note SMALLINT);"
);
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
getLogger().info("Failed to connect to the MySQL Database! Check to see if your login information is correct!");
getLogger().info("Additional information: " + e.getMessage());
useMySQL = false;

View file

@ -245,7 +245,7 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
/**
* Updates all color/note data inventories to display the rainbow icon
* Removes any players who are no longer online from playerGuiInventories
* Removes any players who are no longer online from playerGuiInventoriesd
*/
public void run() {
List<UUID> toRemoveList = new ArrayList<UUID>();
@ -288,7 +288,7 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
public static boolean isGuiDisabled() {
return !guiEnabled;
}
/**
* Forcefully closes all open PlayerParticles GUIs
* Used for when the plugin unloads so players can't take items from the GUI
@ -399,10 +399,11 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
dataIconMeta.setDisplayName(ChatColor.GREEN + "Data");
ParticleEffect pe = p.getParticleEffect();
String dataType = "data";
if (pe.hasProperty(ParticleProperty.COLORABLE)) if (pe == ParticleEffect.NOTE) dataType = "note " + dataType;
else dataType = "color " + dataType;
else if (pe.hasProperty(ParticleProperty.REQUIRES_DATA)) if (pe == ParticleEffect.ITEM_CRACK) dataType = "item " + dataType;
else dataType = "block " + dataType;
if (pe.hasProperty(ParticleProperty.COLORABLE)) // @formatter:off
if (pe == ParticleEffect.NOTE) dataType = "note " + dataType;
else dataType = "color " + dataType;
else if (pe.hasProperty(ParticleProperty.REQUIRES_DATA)) if (pe == ParticleEffect.ITEM_CRACK) dataType = "item " + dataType;
else dataType = "block " + dataType; // @formatter:on
dataIconMeta.setLore(Arrays.asList(MessageType.GUI_ICON_SET_YOUR.getMessageReplaced(dataType)));
if (p.getParticleSpawnData() == null && p.getParticleSpawnColor() == null) {
dataIconMeta.setLore(Arrays.asList(MessageType.GUI_NO_DATA.getMessage()));
@ -423,7 +424,6 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
private static void populateEffect(PPlayer p) {
Player player = p.getPlayer();
Inventory inventory = player.getOpenInventory().getTopInventory();
inventory.clear(); // Make sure the inventory is always empty before we start adding items
List<String> effectsUserHasPermissionFor = PermissionManager.getEffectsUserHasPermissionFor(player);

View file

@ -1,128 +0,0 @@
package com.esophose.playerparticles.library;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Abstract Database class, serves as a base for any connection method (MySQL,
* SQLite, etc.)
*
* @author -_Husky_-
* @author tips48
*/
public abstract class Database {
protected Connection connection;
/**
* Creates a new Database
*
*/
protected Database() {
this.connection = null;
}
/**
* Opens a connection with the database
*
* @return Opened connection
* @throws SQLException if the connection can not be opened
* @throws ClassNotFoundException if the driver cannot be found
*/
public abstract Connection openConnection() throws SQLException, ClassNotFoundException;
/**
* Checks if a connection is open with the database
*
* @return true if the connection is open
* @throws SQLException if the connection cannot be checked
*/
public boolean checkConnection() throws SQLException {
return connection != null && !connection.isClosed();
}
/**
* Gets the connection with the database
*
* @return Connection with the database, null if none
*/
public Connection getConnection() {
return connection;
}
/**
* Closes the connection with the database
*
* @return true if successful
* @throws SQLException if the connection cannot be closed
*/
public boolean closeConnection() throws SQLException {
if (connection == null) {
return false;
}
connection.close();
return true;
}
/**
* Executes a SQL Query<br>
*
* If the connection is closed, it will be opened
*
* @param query Query to be run
* @return the results of the query
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see
* {@link #openConnection()}
*/
public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException {
if (!checkConnection()) {
openConnection();
}
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query);
return result;
}
/**
* Executes an Update SQL Query<br>
* See {@link java.sql.Statement#executeUpdate(String)}<br>
* If the connection is closed, it will be opened
*
* Executes multiple updates broken up by semi-colons
*
* @param query Query to be run
* @return Result Code, see {@link java.sql.Statement#executeUpdate(String)}
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see
* {@link #openConnection()}
*/
public int updateSQL(String query) throws SQLException, ClassNotFoundException {
if (!checkConnection()) {
openConnection();
}
Statement statement = connection.createStatement();
int[] results;
if (query.indexOf(';') != -1) {
String[] queries = query.split(";");
for (String q : queries) {
statement.addBatch(q);
}
results = statement.executeBatch();
} else {
results = new int[] { statement.executeUpdate(query) };
}
statement.close();
return results[0];
}
}

View file

@ -1,64 +0,0 @@
package com.esophose.playerparticles.library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Connects to and uses a MySQL database
*
* @author -_Husky_-
* @author tips48
*/
public class MySQL extends Database {
private final String user;
private final String database;
private final String password;
private final String port;
private final String hostname;
/**
* Creates a new MySQL instance
*
* @param hostname Name of the host
* @param port Port number
* @param username Username
* @param password Password
*/
public MySQL(String hostname, String port, String username, String password) {
this(hostname, port, null, username, password);
}
/**
* Creates a new MySQL instance for a specific database
*
* @param hostname Name of the host
* @param port Port number
* @param database Database name
* @param username Username
* @param password Password
*/
public MySQL(String hostname, String port, String database, String username, String password) {
this.hostname = hostname;
this.port = port;
this.database = database;
this.user = username;
this.password = password;
}
@Override
public Connection openConnection() throws SQLException, ClassNotFoundException {
if (checkConnection()) {
return connection;
}
String connectionURL = "jdbc:mysql://" + this.hostname + ":" + this.port;
if (database != null) {
connectionURL = connectionURL + "/" + this.database;
}
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, this.user, this.password);
return connection;
}
}

View file

@ -186,7 +186,7 @@ public class ConfigManager {
saveNewPPlayer(pplayer);
return pplayer;
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -265,7 +265,7 @@ public class ConfigManager {
");"
);
} // @formatter:on
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -304,7 +304,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -326,7 +326,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -349,7 +349,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "', data = '" + particleItemData.getData() + "' WHERE uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -372,7 +372,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "', data = '" + particleBlockData.getData() + "' WHERE uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -396,7 +396,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -418,7 +418,7 @@ public class ConfigManager {
} else {
try {
PlayerParticles.mySQL.updateSQL("UPDATE pp_data_note SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE uuid = '" + playerUUID + "';");
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -511,7 +511,7 @@ public class ConfigManager {
");"
);
} // @formatter:on
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -548,7 +548,7 @@ public class ConfigManager {
"DELETE FROM pp_data_note WHERE uuid = '" + uuid + "';"
);
} // @formatter:on
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
return false;
}
@ -575,7 +575,7 @@ public class ConfigManager {
"DELETE FROM pp_data_note WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" +
"DELETE FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "';"
); // @formatter:on
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -650,7 +650,7 @@ public class ConfigManager {
}
return fixedEffects;
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -709,7 +709,7 @@ public class ConfigManager {
NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note"));
return new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData);
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -738,7 +738,7 @@ public class ConfigManager {
while (res.next()) {
ids.add(res.getInt(1));
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -768,7 +768,7 @@ public class ConfigManager {
if (res.next()) {
return res.getInt(1) >= maxFixedEffects;
} else return false;
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@ -792,7 +792,7 @@ public class ConfigManager {
while (res.next()) {
idsSet.add(res.getInt(1) + "");
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}

View file

@ -0,0 +1,88 @@
package com.esophose.playerparticles.manager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.bukkit.configuration.file.FileConfiguration;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class DatabaseManager {
private HikariDataSource hikari;
public DatabaseManager(FileConfiguration pluginConfig) {
String hostname = pluginConfig.getString("database-hostname");
String port = pluginConfig.getString("database-port");
String database = pluginConfig.getString("database-name");
String user = pluginConfig.getString("database-user-name");
String pass = pluginConfig.getString("database-user-password");
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + hostname + ":" + port + "/" + database);
config.setUsername(user);
config.setPassword(pass);
config.setMaximumPoolSize(10);
hikari = new HikariDataSource(config);
}
public void closeConnection() {
hikari.close();
}
public ResultSet querySQL(String query) throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
connection = hikari.getConnection();
statement = connection.createStatement();
result = statement.executeQuery(query);
} catch (SQLException ex) {
throw ex;
} finally {
try { if (connection != null) connection.close(); } catch (Exception ex) { };
try { if (statement != null) statement.close(); } catch (Exception ex) { };
}
return result;
}
public int updateSQL(String query) throws SQLException {
Connection connection = null;
Statement statement = null;
try {
connection = hikari.getConnection();
statement = connection.createStatement();
int[] results;
if (query.indexOf(';') != -1) {
String[] queries = query.split(";");
for (String q : queries) {
statement.addBatch(q);
}
results = statement.executeBatch();
} else {
results = new int[] { statement.executeUpdate(query) };
}
statement.close();
return results[0];
} catch (SQLException ex) {
throw ex;
} finally {
try { if (connection != null) connection.close(); } catch (Exception ex) { };
try { if (statement != null) statement.close(); } catch (Exception ex) { };
}
}
}