diff --git a/pom.xml b/pom.xml index b936fc5c..59f70db7 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.William278 HuskHomes - 2.3 + 2.3.1 jar HuskHomes diff --git a/src/main/java/me/william278/huskhomes2/data/DataManager.java b/src/main/java/me/william278/huskhomes2/data/DataManager.java index 7f04dbcc..4ec492e5 100644 --- a/src/main/java/me/william278/huskhomes2/data/DataManager.java +++ b/src/main/java/me/william278/huskhomes2/data/DataManager.java @@ -179,17 +179,16 @@ public class DataManager { return null; } - // Return an integer from the player table from a player ID + // Return if the player is teleporting public static Boolean getPlayerTeleporting(Player p) { Connection conn; PreparedStatement ps; - Integer playerID = getPlayerId(p.getUniqueId()); ResultSet rs; try { conn = getConnection(); - ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `player_id`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?);"); + ps.setString(1, p.getUniqueId().toString()); rs = ps.executeQuery(); if (rs != null) { if (rs.next()) { @@ -198,7 +197,7 @@ public class DataManager { return null; } } else { - Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null"); + Bukkit.getLogger().severe("Failed to retrieve if player was teleporting"); return null; } } catch (SQLException ex) { @@ -234,7 +233,7 @@ public class DataManager { // Set how many home slots a player has private static void setPlayerHomeSlots(Player p, int slots) { - database.setPlayerHomeSlots(getPlayerId(p.getUniqueId()), slots); + database.setPlayerHomeSlots(p.getUniqueId(), slots); } // Return how many home slots a player has @@ -251,13 +250,12 @@ public class DataManager { public static List getPlayerHomes(String playerName) { Connection conn; PreparedStatement ps; - Integer playerID = getPlayerId(playerName); ResultSet rs; try { conn = getConnection(); - ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?));"); + ps.setString(1, playerName); rs = ps.executeQuery(); if (rs != null) { List playerHomes = new ArrayList<>(); @@ -266,7 +264,7 @@ public class DataManager { TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); if (teleportationPoint != null) { playerHomes.add(new Home(teleportationPoint, - playerName, getPlayerUUID(playerID), + playerName, getPlayerUUID(rs.getInt("player_id")), rs.getString("name"), rs.getString("description"), rs.getBoolean("public"))); @@ -455,18 +453,15 @@ public class DataManager { } public static void updateHomePrivacy(String ownerName, String homeName, boolean isPublic) { - Integer playerID = getPlayerId(ownerName); - database.setHomePrivacy(homeName, playerID, isPublic); + database.setHomePrivacy(homeName, ownerName, isPublic); } public static void updateHomeName(String ownerName, String homeName, String newName) { - Integer playerID = getPlayerId(ownerName); - database.setHomeName(homeName, playerID, newName); + database.setHomeName(homeName, ownerName, newName); } public static void updateHomeDescription(String ownerName, String homeName, String newDescription) { - Integer playerID = getPlayerId(ownerName); - database.setHomeDescription(homeName, playerID, newDescription); + database.setHomeDescription(homeName, ownerName, newDescription); } public static void updateHomeLocation(String ownerName, String homeName, Location newLocation) { @@ -492,13 +487,12 @@ public class DataManager { public static Home getHome(String ownerUsername, String homeName) { Connection conn; PreparedStatement ps; - Integer playerID = getPlayerId(ownerUsername); ResultSet rs; try { conn = getConnection(); - ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=? AND `name`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?) AND `name`=?;"); + ps.setString(1, ownerUsername); ps.setString(2, homeName); rs = ps.executeQuery(); if (rs != null) { @@ -508,7 +502,7 @@ public class DataManager { if (teleportationPoint != null) { return new Home(teleportationPoint, ownerUsername, - getPlayerUUID(playerID), + getPlayerUUID(rs.getInt("player_id")), rs.getString("name"), rs.getString("description"), rs.getBoolean("public")); @@ -557,17 +551,11 @@ public class DataManager { Connection conn; PreparedStatement ps; ResultSet rs; - Integer playerID = getPlayerId(ownerName); - - // Return false if the player is invalid - if (playerID == null) { - return false; - } try { conn = getConnection(); - ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=? AND `name`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?) AND `name`=?;"); + ps.setString(1, ownerName); ps.setString(2, homeName); rs = ps.executeQuery(); if (rs != null) { @@ -625,10 +613,7 @@ public class DataManager { rs.getDouble("z"), rs.getFloat("yaw"), rs.getFloat("pitch"), rs.getString("server")); } else { - Bukkit.getLogger().severe("An SQL exception occurred in retrieving a teleportation location."); - Bukkit.getLogger().severe("location ID: " + locationID.toString()); - Bukkit.getLogger().severe("SELECT * FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=" + locationID.toString() + ";"); - return null; + throw new IllegalArgumentException("Could not return a teleportationPoint from the locations data table at (ID#" + locationID + ")"); } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -656,24 +641,14 @@ public class DataManager { // Update a player's last position location on SQL public static void setPlayerLastPosition(Player p, TeleportationPoint point) { - Integer playerID = getPlayerId(p.getUniqueId()); - if (playerID != null) { - deletePlayerLastPosition(p); - database.setTeleportationLastPosition(playerID, point); - } else { - Bukkit.getLogger().warning("Failed to update player last position records for " + p.getName()); - } + deletePlayerLastPosition(p); + database.setTeleportationLastPosition(p.getUniqueId(), point); } // Update a player's destination location on SQL public static void setPlayerDestinationLocation(String playerName, TeleportationPoint point) { - Integer playerID = getPlayerId(playerName); - if (playerID != null) { - deletePlayerDestination(playerName); - database.setTeleportationDestination(playerID, point); - } else { - Bukkit.getLogger().warning("Failed to update player destination records for " + playerName); - } + deletePlayerDestination(playerName); + database.setTeleportationDestination(playerName, point); } // Update a player's destination location on SQL @@ -682,36 +657,25 @@ public class DataManager { } public static void setPlayerTeleporting(Player p, boolean value) { - Integer playerID = getPlayerId(p.getUniqueId()); - if (playerID != null) { - database.setPlayerTeleporting(playerID, value); - } else { - Bukkit.getLogger().warning("Failed to set a player as teleporting (" + p.getName() + ")"); - } + database.setPlayerTeleporting(p.getUniqueId(), value); } public static void clearPlayerDestination(String playerName) { - Integer playerID = getPlayerId(playerName); - if (playerID != null) { - deletePlayerDestination(playerName); - database.clearPlayerDestination(playerID); - } + deletePlayerDestination(playerName); + database.clearPlayerDestination(playerName); } public static void updateRtpCooldown(Player p) { - Integer playerID = getPlayerId(p.getUniqueId()); - if (playerID != null) { - long currentTime = Instant.now().getEpochSecond(); - int newCooldownTime = (int) currentTime + (60 * HuskHomes.getSettings().getRtpCooldown()); - database.setRtpCooldown(playerID, newCooldownTime); - } + long currentTime = Instant.now().getEpochSecond(); + int newCooldownTime = (int) currentTime + (60 * HuskHomes.getSettings().getRtpCooldown()); + database.setRtpCooldown(p.getUniqueId(), newCooldownTime); } public static void deletePlayerDestination(String playerName) { Integer destinationID = getPlayerInteger(playerName, "dest_location_id"); if (destinationID != null) { database.deleteTeleportationPoint(destinationID); - database.clearPlayerDestination(getPlayerId(playerName)); + database.clearPlayerDestination(playerName); } } @@ -723,7 +687,7 @@ public class DataManager { Integer lastPositionID = getPlayerInteger(p, "last_location_id"); if (lastPositionID != null) { database.deleteTeleportationPoint(lastPositionID); - database.clearPlayerLastPosition(getPlayerId(p.getUniqueId())); + database.clearPlayerLastPosition(p.getUniqueId()); } } @@ -747,7 +711,7 @@ public class DataManager { Integer playerID = getPlayerId(p.getUniqueId()); if (playerID != null) { deleteHomeTeleportLocation(playerID, homeName); - database.deleteHome(homeName, playerID); + database.deleteHome(homeName, p.getName()); } else { Bukkit.getLogger().warning("Player ID returned null when deleting a home"); } @@ -827,9 +791,8 @@ public class DataManager { } public static void checkPlayerNameChange(Player p) { - Integer playerID = getPlayerId(p.getUniqueId()); - if (!getPlayerUsername(playerID).equals(p.getName())) { - database.updatePlayerUsername(playerID, p.getName()); + if (!getPlayerUsername(getPlayerId(p.getUniqueId())).equals(p.getName())) { + database.updatePlayerUsername(p.getUniqueId(), p.getName()); } } } \ No newline at end of file diff --git a/src/main/java/me/william278/huskhomes2/data/Database.java b/src/main/java/me/william278/huskhomes2/data/Database.java index 41d10ce4..7f3b3ace 100644 --- a/src/main/java/me/william278/huskhomes2/data/Database.java +++ b/src/main/java/me/william278/huskhomes2/data/Database.java @@ -130,7 +130,9 @@ public abstract class Database { } // Delete a warp - public void deleteHome(String homeName, int playerID) { + // Insert a player into the database + + public void deleteHome(String homeName, String ownerUsername) { Connection conn; PreparedStatement ps; @@ -138,17 +140,15 @@ public abstract class Database { conn = getSQLConnection(); // Delete the home with the given name and player ID - ps = conn.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `name`=? AND `player_id`=?;"); + ps = conn.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);"); ps.setString(1, homeName); - ps.setInt(2, playerID); + ps.setString(2, ownerUsername); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); } } - - // Insert a player into the database public void addPlayer(Player p) { Connection conn; PreparedStatement ps; @@ -211,60 +211,60 @@ public abstract class Database { } } - public void updatePlayerUsername(int playerID, String newName) { + public void updatePlayerUsername(UUID uuid, String newName) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `username`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `username`=? WHERE `user_uuid`=?;"); ps.setString(1, newName); - ps.setInt(2, playerID); + ps.setString(2, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); } } - public void setPlayerTeleporting(int playerID, boolean value) { + public void setPlayerTeleporting(UUID uuid, boolean value) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_teleporting`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_teleporting`=? WHERE `user_uuid`=?);"); ps.setBoolean(1, value); - ps.setInt(2, playerID); + ps.setString(2, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); } } - public void setPlayerHomeSlots(int playerID, int newValue) { + public void setPlayerHomeSlots(UUID uuid, int newValue) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `home_slots`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `home_slots`=? WHERE `user_uuid`=?;"); ps.setInt(1, newValue); - ps.setInt(2, playerID); + ps.setString(2, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); } } - public void setRtpCooldown(int playerID, int newTime) { + public void setRtpCooldown(UUID uuid, int newTime) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `user_uuid`=?;"); ps.setInt(1, newTime); - ps.setInt(2, playerID); + ps.setString(2, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -289,7 +289,7 @@ public abstract class Database { } // Update the location of a home - public void setHomePrivacy(String homeName, int ownerID, boolean isPublic) { + public void setHomePrivacy(String homeName, String ownerName, boolean isPublic) { Connection conn; PreparedStatement ps; @@ -297,10 +297,10 @@ 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.getSettings().getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);"); ps.setBoolean(1, isPublic); ps.setString(2, homeName); - ps.setInt(3, ownerID); + ps.setString(3, ownerName); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -350,7 +350,7 @@ public abstract class Database { } } // Update the description of a home - public void setHomeDescription(String homeName, int ownerID, String newDescription) { + public void setHomeDescription(String homeName, String ownerName, String newDescription) { Connection conn; PreparedStatement ps; @@ -358,10 +358,10 @@ public abstract class Database { conn = getSQLConnection(); // Update the home description - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);"); ps.setString(1, newDescription); ps.setString(2, homeName); - ps.setInt(3, ownerID); + ps.setString(3, ownerName); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -387,7 +387,7 @@ public abstract class Database { } // Update the name of a home - public void setHomeName(String oldHomeName, int ownerID, String newHomeName) { + public void setHomeName(String oldHomeName, String ownerName, String newHomeName) { Connection conn; PreparedStatement ps; @@ -395,10 +395,10 @@ public abstract class Database { conn = getSQLConnection(); // Update the home name - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);"); ps.setString(1, newHomeName); ps.setString(2, oldHomeName); - ps.setInt(3, ownerID); + ps.setString(3, ownerName); ps.executeUpdate(); } catch (SQLException ex) { @@ -426,7 +426,7 @@ public abstract class Database { } // Update a player's last position teleport point - public void setTeleportationLastPosition(int playerID, TeleportationPoint point) { + public void setTeleportationLastPosition(UUID uuid, TeleportationPoint point) { Connection conn; PreparedStatement ps; @@ -437,9 +437,9 @@ public abstract class Database { Integer locationID = addTeleportationPoint(point, conn); // Set the destination location with the location_id of the last inserted teleport point - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=? WHERE `user_uuid`=?;"); ps.setInt(1, locationID); - ps.setInt(2, playerID); + ps.setString(2, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -447,7 +447,7 @@ public abstract class Database { } // Update a player's destination teleport point - public void setTeleportationDestination(int playerID, TeleportationPoint point) { + public void setTeleportationDestination(String username, TeleportationPoint point) { Connection conn; PreparedStatement ps; @@ -458,9 +458,9 @@ public abstract class Database { Integer locationID = addTeleportationPoint(point, conn); // Set the destination location with the location_id of the last inserted teleport point - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=? WHERE `player_id`=?;"); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=? WHERE `username`=?;"); ps.setInt(1, locationID); - ps.setInt(2, playerID); + ps.setString(2, username); ps.executeUpdate(); } catch (SQLException ex) { @@ -469,14 +469,14 @@ public abstract class Database { } // Clear a player's destination - public void clearPlayerDestination(int playerID) { + public void clearPlayerDestination(String playerName) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `player_id`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `username`=?;"); + ps.setString(1, playerName); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex); @@ -484,14 +484,14 @@ public abstract class Database { } // Clear a player's last position - public void clearPlayerLastPosition(int playerID) { + public void clearPlayerLastPosition(UUID uuid) { Connection conn; PreparedStatement ps; try { conn = getSQLConnection(); - ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `player_id`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `user_uuid`=?;"); + ps.setString(1, uuid.toString()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);