Rename packages to lower case

This commit is contained in:
Daniil Z committed 2021-02-28 23:12:14 +03:00
1 parent eed97287b3
commit d61d76f3f3
76 files changed
+166 -171

No files matched your search

@@ -0,0 +1,505 @@
package me.william278.huskhomes2.data;
import me.william278.huskhomes2.data.logging.Error;
import me.william278.huskhomes2.data.logging.Errors;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.objects.Home;
import me.william278.huskhomes2.objects.TeleportationPoint;
import me.william278.huskhomes2.objects.Warp;
import me.william278.huskhomes2.permissionHomeLimits;
import org.bukkit.entity.Player;
import java.sql.*;
import java.util.UUID;
import java.util.logging.Level;
public abstract class Database {
HuskHomes plugin;
Connection connection;
public Database(HuskHomes instance) {
plugin = instance;
}
public abstract Connection getSQLConnection();
public abstract void load();
public void initialize() {
connection = getSQLConnection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.settings.getPlayerDataTable() + ";");
ResultSet rs = ps.executeQuery();
close(ps, rs);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
}
}
// Insert a teleportation point, returns generated id
public Integer addTeleportationPoint(TeleportationPoint point, Connection conn) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + HuskHomes.settings.getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);", Statement.RETURN_GENERATED_KEYS);
ps.setString(1, point.getWorldName());
ps.setString(2, point.getServer());
ps.setDouble(3, point.getX());
ps.setDouble(4, point.getY());
ps.setDouble(5, point.getZ());
ps.setFloat(6, point.getYaw());
ps.setFloat(7, point.getPitch());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
return null;
}
// Insert a home into the database
public void addHome(Home home, int playerID) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
Integer locationID = addTeleportationPoint(home, conn);
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.settings.getHomesDataTable() + " (player_id,location_id,name,description,public) VALUES(?,?,?,?,?);");
ps.setInt(1, playerID);
ps.setInt(2, locationID);
ps.setString(3, home.getName());
ps.setString(4, home.getDescription());
ps.setBoolean(5, home.isPublic());
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Insert a warp into the database
public void addWarp(Warp warp) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Add the teleportation point
Integer locationID = addTeleportationPoint(warp, conn);
// Insert the warp with the location_id of the last inserted teleport point
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.settings.getWarpsDataTable() + " (location_id,name,description) VALUES(?,?,?);");
ps.setInt(1, locationID);
ps.setString(2, warp.getName());
ps.setString(3, warp.getDescription());
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Delete a warp
public void deleteWarp(String warpName) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Delete the warp with the given name
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.settings.getWarpsDataTable() + " WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Delete a warp
public void deleteHome(String homeName, int playerID) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Delete the home with the given name and player ID
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.settings.getHomesDataTable() + " WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Insert a player into the database
public void addPlayer(Player p) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.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, permissionHomeLimits.getFreeHomes(p));
ps.setInt(4, 0);
ps.setBoolean(5, false);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Insert a player into the database
public void addPlayer(UUID playerUUID, String playerName) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.settings.getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps.setString(1, playerUUID.toString());
ps.setString(2, playerName);
ps.setInt(3, HuskHomes.settings.getFreeHomeSlots());
ps.setInt(4, 0);
ps.setBoolean(5, false);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Insert a player into the database
public void addPlayer(UUID playerUUID, String playerName, int homeSlots) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.settings.getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps.setString(1, playerUUID.toString());
ps.setString(2, playerName);
ps.setInt(3, homeSlots);
ps.setInt(4, 0);
ps.setBoolean(5, false);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
public void updatePlayerUsername(int playerID, String newName) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.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);
}
}
public void setPlayerTeleporting(int playerID, boolean value) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `is_teleporting`=? WHERE `player_id`=?;");
ps.setBoolean(1, value);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
public void setPlayerHomeSlots(int playerID, int newValue) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `home_slots`=? WHERE `player_id`=?;");
ps.setInt(1, newValue);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
public void setRtpCooldown(int playerID, int newTime) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.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);
}
}
// Delete a teleportation point from SQL
public void deleteTeleportationPoint(int locationID) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.settings.getLocationsDataTable() + " WHERE `location_id`=?;");
ps.setInt(1, locationID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the location of a home
public void setHomePrivacy(String homeName, int ownerID, boolean isPublic) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=?;");
ps.setBoolean(1, isPublic);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the location of a home
public void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Add the teleportation point
Integer locationID = addTeleportationPoint(point, conn);
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getHomesDataTable() + " SET `location_id`=? WHERE `name`=? AND `player_id`=?;");
ps.setInt(1, locationID);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the location of a home
public void setWarpTeleportPoint(String warpName, TeleportationPoint point) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Add the teleportation point
Integer locationID = addTeleportationPoint(point, conn);
// Set the warp location ID to the new teleport point
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getWarpsDataTable() + " SET `location_id`=? WHERE `name`=?;");
ps.setInt(1, locationID);
ps.setString(2, warpName);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the description of a home
public void setHomeDescription(String homeName, int ownerID, String newDescription) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Update the home description
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newDescription);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the description of a warp
public void setWarpDescription(String warpName, String newDescription) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Update the warp description
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getWarpsDataTable() + " SET `description`=? WHERE `name`=?;");
ps.setString(1, newDescription);
ps.setString(2, warpName);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the name of a home
public void setHomeName(String oldHomeName, int ownerID, String newHomeName) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Update the home name
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newHomeName);
ps.setString(2, oldHomeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update the name of a warp
public void setWarpName(String oldWarpName, String newWarpName) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Update the warp name
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getWarpsDataTable() + " SET `name`=? WHERE `name`=?;");
ps.setString(1, newWarpName);
ps.setString(2, oldWarpName);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update a player's last position teleport point
public void setTeleportationLastPosition(int playerID, TeleportationPoint point) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Add the teleportation point
Integer locationID = addTeleportationPoint(point, conn);
// Set the destination location with the location_id of the last inserted teleport point
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `last_location_id`=? WHERE `player_id`=?;");
ps.setInt(1, locationID);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Update a player's destination teleport point
public void setTeleportationDestination(int playerID, TeleportationPoint point) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
// Add the teleportation point
Integer locationID = addTeleportationPoint(point, conn);
// Set the destination location with the location_id of the last inserted teleport point
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `dest_location_id`=? WHERE `player_id`=?;");
ps.setInt(1, locationID);
ps.setInt(2, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Clear a player's destination
public void clearPlayerDestination(int playerID) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `player_id`=?;");
ps.setInt(1, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
// Clear a player's last position
public void clearPlayerLastPosition(int playerID) {
Connection conn;
PreparedStatement ps;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("UPDATE " + HuskHomes.settings.getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `player_id`=?;");
ps.setInt(1, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
}
}
public void close(PreparedStatement ps, ResultSet rs) {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException ex) {
Error.close(plugin, ex);
}
}
}
@@ -0,0 +1,110 @@
package me.william278.huskhomes2.data;
import me.william278.huskhomes2.HuskHomes;
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 SQL extends Database {
final String SQLiteDatabaseName = "HuskHomesData";
private Connection connection;
private void setConnection(Connection connection) {
this.connection = connection;
}
public SQL(HuskHomes instance) {
super(instance);
}
// Initialise connection via mySQL
private void getMySQLConnection() {
String host = HuskHomes.settings.getMySQLhost();
int port = HuskHomes.settings.getMySQLport();
String database = HuskHomes.settings.getMySQLdatabase();
String username = HuskHomes.settings.getMySQLusername();
String password = HuskHomes.settings.getMySQLpassword();
try {
synchronized (HuskHomes.getInstance()) {
Class.forName("com.mysql.jdbc.Driver");
setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password));
}
} catch (SQLException e) {
HuskHomes.disablePlugin("[!] Could not connect to the mySQL Database with the credentials provided!\n[!] Check that your host IP address and port are valid and that your username and password are valid and that the user has the correct access permissions on the database.");
} catch (ClassNotFoundException e) {
HuskHomes.disablePlugin("[!] A critical exception occurred when attempting to establish a connection to the mySQL database!");
e.printStackTrace();
}
}
// Initialise connection via SQLite
private void getSQLiteConnection() {
File dataFolder = new File(plugin.getDataFolder(), SQLiteDatabaseName + ".db");
if (!dataFolder.exists()) {
try {
if (!dataFolder.createNewFile()) {
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + SQLiteDatabaseName + ".db (file already exists)");
}
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + SQLiteDatabaseName + ".db (" + e.getCause() + ")");
}
}
try {
Class.forName("org.sqlite.JDBC");
setConnection(DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + SQLiteDatabaseName + ".db"));
} 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.");
}
}
public Connection getSQLConnection() {
try {
if (connection == null || connection.isClosed()) {
String dataStorageType = HuskHomes.settings.getStorageType().toLowerCase();
switch (dataStorageType) {
case "mysql":
getMySQLConnection();
break;
case "sqlite":
getSQLiteConnection();
break;
default:
plugin.getLogger().log(Level.WARNING, "An invalid data storage type was specified in config.yml; defaulting to SQLite");
getSQLiteConnection();
break;
}
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection");
}
return connection;
}
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
s.execute(dataManager.createLocationsTable);
s.execute(dataManager.createPlayerTable);
s.execute(dataManager.createHomesTable);
s.execute(dataManager.createWarpsTable);
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
initialize();
}
}
@@ -0,0 +1,15 @@
package me.william278.huskhomes2.data.logging;
import me.william278.huskhomes2.HuskHomes;
import java.util.logging.Level;
public class Error {
public static void execute(HuskHomes plugin, Exception ex){
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
}
public static void close(HuskHomes plugin, Exception ex){
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
}
}
@@ -0,0 +1,16 @@
package me.william278.huskhomes2.data.logging;
public class Errors {
public static String sqlConnectionExecute(){
return "Couldn't execute MySQL statement: ";
}
public static String sqlConnectionClose(){
return "Failed to close MySQL connection: ";
}
public static String noSQLConnection(){
return "Unable to retrieve MYSQL connection: ";
}
public static String noTableFound(){
return "Database Error: No Table Found";
}
}