Added mySQL support, RTP command and bStats metric collection

This commit is contained in:
William278 committed 2020-12-14 15:50:30 +00:00
1 parent 3d4e14d420
commit 18b244ed65
21 files changed
+941 -314

No files matched your search

@@ -1,7 +1,7 @@
package me.william278.huskhomes2.Data.SQLite;
package me.william278.huskhomes2.Data;
import me.william278.huskhomes2.Data.SQLite.Errors.Error;
import me.william278.huskhomes2.Data.SQLite.Errors.Errors;
import me.william278.huskhomes2.Data.ErrorLogging.Error;
import me.william278.huskhomes2.Data.ErrorLogging.Errors;
import me.william278.huskhomes2.Main;
import me.william278.huskhomes2.Objects.Home;
import me.william278.huskhomes2.Objects.TeleportationPoint;
@@ -29,7 +29,7 @@ public abstract class Database {
public void initialize() {
connection = getSQLConnection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `user_uuid`=?");
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerDataTable() + " WHERE `user_uuid`=?");
ResultSet rs = ps.executeQuery();
close(ps, rs);
@@ -41,7 +41,7 @@ public abstract class Database {
// Insert a teleportation point
public void addTeleportationPoint(TeleportationPoint point, Connection conn) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO huskhomes_location_data (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);");
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + Main.settings.getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);");
ps.setString(1, point.getWorldName());
ps.setString(2, point.getServer());
@@ -65,7 +65,7 @@ public abstract class Database {
conn = getSQLConnection();
addTeleportationPoint(home, conn);
ps = conn.prepareStatement("INSERT INTO huskhomes_home_data (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);");
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getHomesDataTable() + " (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);");
ps.setInt(1, playerID);
ps.setString(2, home.getName());
ps.setString(3, home.getDescription());
@@ -97,7 +97,7 @@ public abstract class Database {
addTeleportationPoint(warp, conn);
// Insert the warp with the location_id of the last inserted teleport point
ps = conn.prepareStatement("INSERT INTO huskhomes_warp_data (location_id,name,description) VALUES((SELECT last_insert_rowid()),?,?);");
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getWarpsDataTable() + " (location_id,name,description) VALUES((SELECT last_insert_rowid()),?,?);");
ps.setString(1, warp.getName());
ps.setString(2, warp.getDescription());
ps.executeUpdate();
@@ -124,7 +124,7 @@ public abstract class Database {
conn = getSQLConnection();
// Delete the warp with the given name
ps = conn.prepareStatement("DELETE FROM huskhomes_warp_data WHERE `name`=?;");
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getWarpsDataTable() + " WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
@@ -151,7 +151,7 @@ public abstract class Database {
conn = getSQLConnection();
// Delete the home with the given name and player ID
ps = conn.prepareStatement("DELETE FROM huskhomes_home_data WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getHomesDataTable() + " WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, playerID);
ps.executeUpdate();
@@ -178,7 +178,7 @@ public abstract class Database {
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO huskhomes_player_data (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps.setString(1, p.getUniqueId().toString());
ps.setString(2, p.getName());
ps.setInt(3, 5); //TODO: Update default home slots to be based on permission & config
@@ -200,15 +200,37 @@ public abstract class Database {
}
}
public void updatePlayerUsername(int playerID, String newName) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `username`=? WHERE `player_id`=?;");
ps.setString(1, newName);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
}
public void setPlayerTeleporting(int playerID, boolean value) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE huskhomes_player_data SET `is_teleporting`=? WHERE `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `is_teleporting`=? WHERE `player_id`=?;");
ps.setBoolean(1, value);
ps.setInt(2, playerID);
ps.executeUpdate();
@@ -226,6 +248,30 @@ public abstract class Database {
}
}
public void setRtpCooldown(int playerID, int newTime) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `player_id`=?;");
ps.setInt(1, newTime);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
}
// Delete a teleportation point from SQL
public void deleteTeleportationPoint(int locationID) {
Connection conn = null;
@@ -234,7 +280,7 @@ public abstract class Database {
try {
conn = getSQLConnection();
ps = conn.prepareStatement("DELETE FROM huskhomes_location_data WHERE `location_id`=?;");
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getLocationsDataTable() + " WHERE `location_id`=?;");
ps.setInt(1, locationID);
ps.executeUpdate();
@@ -261,7 +307,7 @@ public abstract class Database {
conn = getSQLConnection();
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `public`=? WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=?;");
ps.setBoolean(1, isPublic);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
@@ -292,7 +338,7 @@ public abstract class Database {
addTeleportationPoint(point, conn);
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, ownerID);
ps.executeUpdate();
@@ -322,7 +368,7 @@ public abstract class Database {
addTeleportationPoint(point, conn);
// Set the warp location ID to the new teleport point
ps = conn.prepareStatement("UPDATE huskhomes_warp_data SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsDataTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
} catch (SQLException ex) {
@@ -347,7 +393,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the home description
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newDescription);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
@@ -375,7 +421,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the warp description
ps = conn.prepareStatement("UPDATE huskhomes_warp_data SET `description`=? WHERE `name`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsDataTable() + " SET `description`=? WHERE `name`=?;");
ps.setString(1, newDescription);
ps.setString(2, warpName);
ps.executeUpdate();
@@ -402,7 +448,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the home name
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newHomeName);
ps.setString(2, oldHomeName);
ps.setInt(3, ownerID);
@@ -431,7 +477,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the warp name
ps = conn.prepareStatement("UPDATE huskhomes_warp_data SET `name`=? WHERE `name`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsDataTable() + " SET `name`=? WHERE `name`=?;");
ps.setString(1, newWarpName);
ps.setString(2, oldWarpName);
@@ -462,7 +508,31 @@ public abstract class Database {
addTeleportationPoint(point, conn);
// Set the destination location with the location_id of the last inserted teleport point
ps = conn.prepareStatement("UPDATE huskhomes_player_data SET `" + type + "_location_id`=(SELECT last_insert_rowid()) WHERE `player_id`=?;");
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `" + type + "_location_id`=(SELECT last_insert_rowid()) WHERE `player_id`=?;");
ps.setInt(1, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
}
// Clear a player's destination
public void clearPlayerDestination(int playerID) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `player_id`=?;");
ps.setInt(1, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
@@ -1,4 +1,4 @@
package me.william278.huskhomes2.Data.SQLite.Errors;
package me.william278.huskhomes2.Data.ErrorLogging;
import me.william278.huskhomes2.Main;
@@ -1,4 +1,4 @@
package me.william278.huskhomes2.Data.SQLite.Errors;
package me.william278.huskhomes2.Data.ErrorLogging;
public class Errors {
public static String sqlConnectionExecute(){
@@ -0,0 +1,101 @@
package me.william278.huskhomes2.Data;
import me.william278.huskhomes2.Main;
import me.william278.huskhomes2.dataManager;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
public class SQL extends Database {
final String SQLiteDatabaseName = "HuskHomesData";
public SQL(Main instance) {
super(instance);
}
// Initialise connection via mySQL
private Connection getMySQLConnection() {
String host = Main.settings.getMySQLhost();
int port = Main.settings.getMySQLport();
String database = Main.settings.getMySQLdatabase();
String username = Main.settings.getMySQLusername();
String password = Main.settings.getMySQLpassword();
try {
synchronized (Main.getInstance()) {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
}
} catch (SQLException e) {
Main.disablePlugin("Could not connect to the mySQL Database with the credentials provided!");
} catch (ClassNotFoundException e) {
Main.disablePlugin("A critical exception occurred when attempting to establish a mySQL connection.");
e.printStackTrace();
}
return null;
}
// Initialise connection via SQLite
private Connection getSQLiteConnection() {
File dataFolder = new File(plugin.getDataFolder(), SQLiteDatabaseName + ".db");
if (!dataFolder.exists()) {
try {
if (!dataFolder.createNewFile()) {
plugin.getLogger().log(Level.SEVERE, "File write error: " + SQLiteDatabaseName + ".db");
}
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "File write error: " + SQLiteDatabaseName + ".db");
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + SQLiteDatabaseName + ".db");
return connection;
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
} catch (ClassNotFoundException ex) {
plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library; please download and place this in the /lib folder.");
}
return null;
}
public Connection getSQLConnection() {
String dataStorageType = Main.settings.getStorageType().toLowerCase();
switch (dataStorageType) {
case "mysql":
return getMySQLConnection();
case "sqlite":
return getSQLiteConnection();
default:
Bukkit.getLogger().warning("An invalid data storage type was specified in config.yml; defaulting to SQLite");
return getSQLiteConnection();
}
}
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
s.execute(dataManager.createPlayerTable);
s.execute(dataManager.createLocationsTable);
s.execute(dataManager.createHomesTable);
s.execute(dataManager.createWarpsTable);
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
initialize();
}
}
@@ -1,65 +0,0 @@
package me.william278.huskhomes2.Data.SQLite;
import me.william278.huskhomes2.Main;
import me.william278.huskhomes2.dataManager;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
public class SQLite extends Database {
final String databaseName = "HuskHomesData";
public SQLite(Main instance) {
super(instance);
}
// SQL creation stuff, You can leave the blow stuff untouched.
public Connection getSQLConnection() {
File dataFolder = new File(plugin.getDataFolder(), databaseName + ".db");
if (!dataFolder.exists()) {
try {
if (!dataFolder.createNewFile()) {
plugin.getLogger().log(Level.SEVERE, "File write error: " + databaseName + ".db");
}
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "File write error: " + databaseName + ".db");
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + databaseName + ".db");
return connection;
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
} catch (ClassNotFoundException ex) {
plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library; please download and place this in the /lib folder.");
}
return null;
}
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
s.execute(dataManager.createPlayerTable);
s.execute(dataManager.createLocationsTable);
s.execute(dataManager.createHomesTable);
s.execute(dataManager.createWarpsTable);
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
initialize();
}
}