From 98893814f7642e438a8720f85df0b33a36d50639 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 10 Aug 2021 13:43:11 +0100 Subject: [PATCH] Refactoring, improve database system and add creation timestamp to homes and warps --- .../william278/huskhomes2/EditingHandler.java | 34 +- .../me/william278/huskhomes2/HuskHomes.java | 2 +- .../william278/huskhomes2/MessageManager.java | 2 +- .../huskhomes2/api/HuskHomesAPI.java | 13 +- .../huskhomes2/commands/EditHomeCommand.java | 23 +- .../huskhomes2/commands/EditWarpCommand.java | 11 +- .../huskhomes2/commands/HomeCommand.java | 10 +- .../huskhomes2/data/DataManager.java | 838 ++++++++---------- .../william278/huskhomes2/data/SQL/MySQL.java | 2 + .../huskhomes2/data/SQL/SQLite.java | 2 + .../huskhomes2/listeners/PlayerListener.java | 4 +- .../listeners/PluginMessageListener.java | 2 +- .../migrators/EssentialsMigrator.java | 5 +- .../huskhomes2/migrators/UpgradeDatabase.java | 33 +- .../huskhomes2/teleport/TeleportManager.java | 4 +- .../huskhomes2/teleport/TimedTeleport.java | 2 +- .../huskhomes2/teleport/points/Home.java | 81 +- .../huskhomes2/teleport/points/SetPoint.java | 79 ++ .../huskhomes2/teleport/points/Warp.java | 53 +- src/main/resources/config.yml | 2 +- .../{Languages => languages}/en-gb.yml | 1 + .../{Languages => languages}/it-it.yml | 1 + .../resources/{Languages => languages}/pl.yml | 1 + .../{Languages => languages}/zh-cn.yml | 1 + 24 files changed, 549 insertions(+), 657 deletions(-) create mode 100644 src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java rename src/main/resources/{Languages => languages}/en-gb.yml (99%) rename src/main/resources/{Languages => languages}/it-it.yml (99%) rename src/main/resources/{Languages => languages}/pl.yml (99%) rename src/main/resources/{Languages => languages}/zh-cn.yml (99%) diff --git a/src/main/java/me/william278/huskhomes2/EditingHandler.java b/src/main/java/me/william278/huskhomes2/EditingHandler.java index 96f43ce2..fc7304e0 100644 --- a/src/main/java/me/william278/huskhomes2/EditingHandler.java +++ b/src/main/java/me/william278/huskhomes2/EditingHandler.java @@ -2,6 +2,7 @@ package me.william278.huskhomes2; import de.themoep.minedown.MineDown; import me.william278.huskhomes2.teleport.points.Home; +import me.william278.huskhomes2.teleport.points.SetPoint; import me.william278.huskhomes2.teleport.points.Warp; import org.bukkit.entity.Player; @@ -9,15 +10,7 @@ public class EditingHandler { public static void showEditHomeOptions(Player p, Home home) { MessageManager.sendMessage(p, "edit_home_title", home.getName()); - MessageManager.sendMessage(p, "edit_description", MineDown.escape(home.getDescription().replace("]", "]").replace("[", "[").replace("(", "❲").replace(")", "❳"))); - MessageManager.sendMessage(p, "edit_location", - Integer.toString((int) home.getX()), - Integer.toString((int) home.getY()), - Integer.toString((int) home.getZ())); - MessageManager.sendMessage(p, "edit_world", home.getWorldName()); - if (HuskHomes.getSettings().doBungee()) { - MessageManager.sendMessage(p, "edit_server", home.getServer()); - } + showPointDetails(p, home); if (!home.isPublic()) { MessageManager.sendMessage(p, "edit_home_privacy_private"); } else { @@ -37,17 +30,22 @@ public class EditingHandler { public static void showEditWarpOptions(Player p, Warp warp) { MessageManager.sendMessage(p, "edit_warp_title", warp.getName()); - MessageManager.sendMessage(p, "edit_description", MineDown.escape(warp.getDescription().replace("]", "]").replace("[", "[").replace("(", "❲").replace(")", "❳"))); - MessageManager.sendMessage(p, "edit_location", - Integer.toString((int) warp.getX()), - Integer.toString((int) warp.getY()), - Integer.toString((int) warp.getZ())); - MessageManager.sendMessage(p, "edit_world", warp.getWorldName()); - if (HuskHomes.getSettings().doBungee()) { - MessageManager.sendMessage(p, "edit_server", warp.getServer()); - } + showPointDetails(p, warp); MessageManager.sendMessage(p, "edit_warp_options", warp.getName()); } + private static void showPointDetails(Player p, SetPoint point) { + MessageManager.sendMessage(p, "edit_description", MineDown.escape(point.getDescription().replace("]", "]").replace("[", "[").replace("(", "❲").replace(")", "❳"))); + MessageManager.sendMessage(p, "edit_timestamp", point.getFormattedCreationTime()); + MessageManager.sendMessage(p, "edit_location", + Integer.toString((int) point.getX()), + Integer.toString((int) point.getY()), + Integer.toString((int) point.getZ())); + MessageManager.sendMessage(p, "edit_world", point.getWorldName()); + if (HuskHomes.getSettings().doBungee()) { + MessageManager.sendMessage(p, "edit_server", point.getServer()); + } + } + } \ No newline at end of file diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 5de01a60..f32445c3 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -229,7 +229,7 @@ public final class HuskHomes extends JavaPlugin { Metrics metrics = new Metrics(this, METRICS_PLUGIN_ID); metrics.addCustomChart(new SimplePie("bungee_mode", () -> Boolean.toString(getSettings().doBungee()))); metrics.addCustomChart(new SimplePie("language", () -> getSettings().getLanguage())); - metrics.addCustomChart(new SimplePie("database_type", () -> getSettings().getDatabaseType())); + metrics.addCustomChart(new SimplePie("database_type", () -> getSettings().getDatabaseType().toLowerCase())); metrics.addCustomChart(new SimplePie("using_economy", () -> Boolean.toString(getSettings().doEconomy()))); metrics.addCustomChart(new SimplePie("using_map", () -> Boolean.toString(getSettings().doMapIntegration()))); if (getSettings().doMapIntegration()) { diff --git a/src/main/java/me/william278/huskhomes2/MessageManager.java b/src/main/java/me/william278/huskhomes2/MessageManager.java index ef1ec37d..db69a283 100644 --- a/src/main/java/me/william278/huskhomes2/MessageManager.java +++ b/src/main/java/me/william278/huskhomes2/MessageManager.java @@ -64,7 +64,7 @@ public class MessageManager { Change the appearance/text of messages in the plugin using this config.\s This config makes use of MineDown formatting, with extensive support for custom colors & formats.\s For formatting help, see: https://github.com/Phoenix616/MineDown or check the HuskHomes Wiki."""); - InputStream defaultMessageFile = plugin.getResource("Languages/" + language + ".yml"); + InputStream defaultMessageFile = plugin.getResource("languages/" + language + ".yml"); if (defaultMessageFile != null) { YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultMessageFile, StandardCharsets.UTF_8)); config.setDefaults(yamlConfiguration); diff --git a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java index ae843feb..9f063304 100644 --- a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java +++ b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java @@ -13,7 +13,6 @@ import org.bukkit.entity.Player; import java.sql.Connection; import java.sql.SQLException; import java.util.List; -import java.util.UUID; import java.util.logging.Level; /** @@ -55,7 +54,7 @@ public class HuskHomesAPI { */ public Home getHome(String ownerUsername, String homeName) { try { - return DataManager.getHome(ownerUsername, homeName, huskHomes.getInstance().getConnection()); + return DataManager.getHome(ownerUsername, homeName, HuskHomes.getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -70,7 +69,7 @@ public class HuskHomesAPI { */ public Warp getWarp(String warpName) { try { - return DataManager.getWarp(warpName, huskHomes.getInstance().getConnection()); + return DataManager.getWarp(warpName, HuskHomes.getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -95,7 +94,7 @@ public class HuskHomesAPI { */ public List getHomes(String ownerUsername) { try { - return DataManager.getPlayerHomes(ownerUsername, huskHomes.getInstance().getConnection()); + return DataManager.getPlayerHomes(ownerUsername, HuskHomes.getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -137,7 +136,7 @@ public class HuskHomesAPI { */ public List getPublicHomes() { try { - return DataManager.getPublicHomes(huskHomes.getInstance().getConnection()); + return DataManager.getPublicHomes(HuskHomes.getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -160,7 +159,7 @@ public class HuskHomesAPI { */ public List getWarps() { try { - return DataManager.getWarps(huskHomes.getInstance().getConnection()); + return DataManager.getWarps(HuskHomes.getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -229,7 +228,7 @@ public class HuskHomesAPI { * @see TeleportationPoint */ public void teleportPlayer(Player player, String targetPlayerName, boolean timed) { - Connection connection = huskHomes.getInstance().getConnection(); + Connection connection = HuskHomes.getConnection(); Bukkit.getScheduler().runTask(huskHomes, () -> { try { if (timed) { diff --git a/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java b/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java index 9d56143c..e2c18a95 100644 --- a/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java @@ -64,16 +64,7 @@ public class EditHomeCommand extends CommandBase implements TabCompleter { case "description" -> { if (args.length >= 3) { // Get the new description - StringBuilder newDescription = new StringBuilder(); - for (int i = 2; i < args.length; i++) { - if (i > 2) { - newDescription.append(" ").append(args[i]); - } else { - newDescription = new StringBuilder(args[i]); - } - } - String newDescriptionString = newDescription.toString(); - editHomeDescription(p, homeName, newDescriptionString, connection); + editHomeDescription(p, homeName, buildDescription(args), connection); } else { MessageManager.sendMessage(p, "error_invalid_syntax", "/edithome description "); } @@ -93,6 +84,18 @@ public class EditHomeCommand extends CommandBase implements TabCompleter { }); } + public static String buildDescription(String[] args) { + StringBuilder newDescription = new StringBuilder(); + for (int i = 2; i < args.length; i++) { + if (i > 2) { + newDescription.append(" ").append(args[i]); + } else { + newDescription = new StringBuilder(args[i]); + } + } + return newDescription.toString(); + } + private void editHomeLocation(Player p, String homeName, Connection connection) throws SQLException { Location newLocation = p.getLocation(); TeleportationPoint newTeleportLocation = new TeleportationPoint(newLocation, HuskHomes.getSettings().getServerID()); diff --git a/src/main/java/me/william278/huskhomes2/commands/EditWarpCommand.java b/src/main/java/me/william278/huskhomes2/commands/EditWarpCommand.java index 8d4eb5dc..cb8d3aa0 100644 --- a/src/main/java/me/william278/huskhomes2/commands/EditWarpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/EditWarpCommand.java @@ -59,16 +59,7 @@ public class EditWarpCommand extends CommandBase implements TabCompleter { case "description" -> { if (args.length >= 3) { // Get the new description - StringBuilder newDescription = new StringBuilder(); - for (int i = 2; i < args.length; i++) { - if (i > 2) { - newDescription.append(" ").append(args[i]); - } else { - newDescription = new StringBuilder(args[i]); - } - } - String newDescriptionString = newDescription.toString(); - editWarpDescription(p, warpName, newDescriptionString, connection); + editWarpDescription(p, warpName, EditHomeCommand.buildDescription(args), connection); } else { MessageManager.sendMessage(p, "error_invalid_syntax", "/editwarp description "); } diff --git a/src/main/java/me/william278/huskhomes2/commands/HomeCommand.java b/src/main/java/me/william278/huskhomes2/commands/HomeCommand.java index a13c9478..9567b8fc 100644 --- a/src/main/java/me/william278/huskhomes2/commands/HomeCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/HomeCommand.java @@ -41,12 +41,10 @@ public class HomeCommand extends CommandBase { } } else { List homes = DataManager.getPlayerHomes(p.getName(), connection); - if (homes != null) { - if (homes.size() == 1) { - // Teleport the player if they only have one home - TeleportManager.queueTimedTeleport(p, homes.get(0), connection); - return; - } + if (homes.size() == 1) { + // Teleport the player if they only have one home + TeleportManager.queueTimedTeleport(p, homes.get(0), connection); + return; } HomeListCommand.displayPlayerHomeList(p, 1); } diff --git a/src/main/java/me/william278/huskhomes2/data/DataManager.java b/src/main/java/me/william278/huskhomes2/data/DataManager.java index 7c894f04..228f1d4c 100644 --- a/src/main/java/me/william278/huskhomes2/data/DataManager.java +++ b/src/main/java/me/william278/huskhomes2/data/DataManager.java @@ -18,58 +18,50 @@ public class DataManager { // Return a player's ID from their UUID private static Integer getPlayerId(UUID uuid, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;")) { + statement.setString(1, uuid.toString()); + ResultSet resultSet = statement.executeQuery(); - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;"); - ps.setString(1, uuid.toString()); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final int playerID = rs.getInt("player_id"); - ps.close(); + if (resultSet.next()) { + final int playerID = resultSet.getInt("player_id"); + statement.close(); return playerID; } + } - ps.close(); return null; } - // Return a player's ID from their username + // Return a player's ID from their UUID public static Integer getPlayerId(String playerUsername, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?;"); - ps.setString(1, playerUsername); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final int returnable = rs.getInt("player_id"); - ps.close(); - return returnable; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?;")) { + statement.setString(1, playerUsername); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final int playerID = resultSet.getInt("player_id"); + statement.close(); + return playerID; } + } - ps.close(); return null; } // Return an integer from the player table from a player ID private static Integer getPlayerInteger(Integer playerID, String column, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `player_id`=?;"); - ps.setInt(1, playerID); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final int returnable = rs.getInt(column); - ps.close(); - return returnable; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `player_id`=?;")) { + statement.setInt(1, playerID); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final int playerInteger = resultSet.getInt(column); + statement.close(); + return playerInteger; } + } - ps.close(); return null; } @@ -78,66 +70,51 @@ public class DataManager { return getPlayerInteger(getPlayerId(p.getUniqueId(), connection), column, connection); } - // Return an integer from the player table from a player ID + // Return a string from the player table from a player ID private static String getPlayerString(Integer playerID, String column, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `player_id`=?;"); - ps.setInt(1, playerID); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final String player = rs.getString(column); - ps.close(); - return player; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `player_id`=?;")) { + statement.setInt(1, playerID); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final String playerString = resultSet.getString(column); + statement.close(); + return playerString; } - } else { - Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null"); + } - ps.close(); return null; } // Return if the player is teleporting - public static Boolean getPlayerTeleporting(Player p, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;"); - ps.setString(1, p.getUniqueId().toString()); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final boolean isTeleporting = rs.getBoolean("is_teleporting"); - ps.close(); + public static Boolean isPlayerTeleporting(UUID uuid, Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;")) { + statement.setString(1, uuid.toString()); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final boolean isTeleporting = resultSet.getBoolean("is_teleporting"); + statement.close(); return isTeleporting; } - } else { - Bukkit.getLogger().severe("Failed to retrieve if player was teleporting"); + } - ps.close(); return null; } // Return if the player is ignoring teleport requests - public static Boolean getPlayerIgnoringRequests(Player p, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;"); - ps.setString(1, p.getUniqueId().toString()); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final boolean isIgnoringRequests = rs.getBoolean("is_ignoring_requests"); - ps.close(); + public static Boolean isPlayerIgnoringRequests(UUID uuid, Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;")) { + statement.setString(1, uuid.toString()); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final boolean isIgnoringRequests = resultSet.getBoolean("is_ignoring_requests"); + statement.close(); return isIgnoringRequests; } - } else { - Bukkit.getLogger().severe("Failed to retrieve if player was ignoring teleport requests"); + } - ps.close(); return null; } @@ -145,7 +122,7 @@ public class DataManager { public static UUID getPlayerUUID(int playerID, Connection connection) throws SQLException { String uuidString = getPlayerString(playerID, "user_uuid", connection); if (uuidString != null) { - return UUID.fromString(uuidString); + return UUID.fromString(uuidString); } else { return null; } @@ -158,12 +135,7 @@ public class DataManager { // Return how many homes the player has set public static int getPlayerHomeCount(Player p, Connection connection) throws SQLException { - List playerHomes = getPlayerHomes(p.getName(), connection); - if (playerHomes != null) { - return playerHomes.size(); - } else { - return 0; - } + return getPlayerHomes(p.getName(), connection).size(); } // Increment the number of home slots a player has @@ -177,122 +149,111 @@ public class DataManager { } // Return how many home slots a player has - public static Long getPlayerRtpCooldown(Player p, Connection connection) throws SQLException { + public static Long getPlayerRtpCoolDown(Player p, Connection connection) throws SQLException { return (long) getPlayerInteger(p, "rtp_cooldown", connection); } - // Return a player's homes. + // Returns a list of a player's homes public static List getPlayerHomes(String playerName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.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) { - final List playerHomes = new ArrayList<>(); - while (rs.next()) { - int locationID = rs.getInt("location_id"); + final List playerHomes = new ArrayList<>(); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);")) { + statement.setString(1, playerName); + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + int locationID = resultSet.getInt("location_id"); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); playerHomes.add(new Home(teleportationPoint, - playerName, getPlayerUUID(rs.getInt("player_id"), connection), - rs.getString("name"), - rs.getString("description"), - rs.getBoolean("public"))); + playerName, + getPlayerUUID(resultSet.getInt("player_id"), connection), + resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getBoolean("public"), + resultSet.getTimestamp("creation_time").getTime())); } - ps.close(); - return playerHomes; + } - ps.close(); - return null; + return playerHomes; } // Return all the public homes public static List getPublicHomes(Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `public`;"); - rs = ps.executeQuery(); final List publicHomes = new ArrayList<>(); - if (rs != null) { - while (rs.next()) { - int playerID = rs.getInt("player_id"); - int locationID = rs.getInt("location_id"); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `public`;")) { + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + int playerID = resultSet.getInt("player_id"); + int locationID = resultSet.getInt("location_id"); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); - publicHomes.add(new Home(teleportationPoint, getPlayerUsername(playerID, connection), - getPlayerUUID(playerID, connection), rs.getString("name"), - rs.getString("description"), true)); + publicHomes.add(new Home(teleportationPoint, + getPlayerUsername(playerID, connection), + getPlayerUUID(playerID, connection), + resultSet.getString("name"), + resultSet.getString("description"), true, + resultSet.getTimestamp("creation_time").getTime())); } - ps.close(); + } - ps.close(); return publicHomes; } // Return an array of all the warps public static List getWarps(Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + ";"); - rs = ps.executeQuery(); final List warps = new ArrayList<>(); - if (rs != null) { - while (rs.next()) { - int locationID = rs.getInt("location_id"); - TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); - warps.add(new Warp(teleportationPoint, - rs.getString("name"), - rs.getString("description"))); + try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " ORDER BY `name` ASC;")) { + ResultSet resultSet = statement.executeQuery(); + if (resultSet != null) { + while (resultSet.next()) { + int locationID = resultSet.getInt("location_id"); + TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); + warps.add(new Warp(teleportationPoint, + resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("creation_time").getTime())); + } + } else { + Bukkit.getLogger().severe("Result set of warps returned null!"); } - } else { - Bukkit.getLogger().severe("Result set of warps returned null!"); } - ps.close(); return warps; } // Return a warp with a given name (warp names are unique) public static Warp getWarp(String name, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;"); - ps.setString(1, name); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - int locationID = rs.getInt("location_id"); + try (PreparedStatement statement = (connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;"))) { + statement.setString(1, name); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + int locationID = resultSet.getInt("location_id"); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); return new Warp(teleportationPoint, - rs.getString("name"), - rs.getString("description")); + resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("creation_time").getTime()); } } - ps.close(); return null; } // Obtain the teleportation location ID from a home public static Integer getHomeLocationID(int ownerID, String homeName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=? AND `name`=?;"); - ps.setInt(1, ownerID); - ps.setString(2, homeName); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final int locationID = rs.getInt("location_id"); - ps.close(); - return locationID; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=? AND `name`=?;")) { + statement.setInt(1, ownerID); + statement.setString(2, homeName); + ResultSet resultSet = statement.executeQuery(); + if (resultSet != null) { + if (resultSet.next()) { + final int locationID = resultSet.getInt("location_id"); + statement.close(); + return locationID; + } + } else { + Bukkit.getLogger().severe("Failed to obtain home teleportation location ID"); } - } else { - Bukkit.getLogger().severe("Failed to obtain home teleportation location ID"); } - ps.close(); return null; } @@ -315,22 +276,18 @@ public class DataManager { // Get the ID of the teleportation point of the warp public static Integer getWarpLocationID(String warpName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;"); - ps.setString(1, warpName); - rs = ps.executeQuery(); - if (rs != null) { - if (rs.next()) { - final int locationID = rs.getInt("location_id"); - ps.close(); - return locationID; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;")) { + statement.setString(1, warpName); + ResultSet resultSet = statement.executeQuery(); + if (resultSet != null) { + if (resultSet.next()) { + final int locationID = resultSet.getInt("location_id"); + statement.close(); + return locationID; + } } - } else { - Bukkit.getLogger().severe("Failed to obtain warp teleportation location ID"); } - ps.close(); return null; } @@ -340,199 +297,157 @@ public class DataManager { if (warpLocationID != null) { deleteTeleportationPoint(warpLocationID, connection); - PreparedStatement ps; // Delete the warp with the given name - ps = connection.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;"); - ps.setString(1, warpName); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "DELETE FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;")) { + statement.setString(1, warpName); + statement.executeUpdate(); + } } } + // Delete the home with the given name and player ID public static void deleteHome(String homeName, String ownerUsername, Connection connection) throws SQLException { - PreparedStatement ps; - - // Delete the home with the given name and player ID - ps = connection.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.setString(2, ownerUsername); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);")) { + statement.setString(1, homeName); + statement.setString(2, ownerUsername); + statement.executeUpdate(); + } } public static void addPlayer(Player p, Connection connection) throws SQLException { - PreparedStatement ps; - - ps = connection.prepareStatement("INSERT INTO " + HuskHomes.getSettings().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, Home.getFreeHomes(p)); - ps.setInt(4, 0); - ps.setBoolean(5, false); - - ps.executeUpdate(); - ps.close(); + addPlayer(p.getUniqueId(), p.getName(), connection); } // Insert a player into the database public static void addPlayer(UUID playerUUID, String playerName, Connection connection) throws SQLException { - PreparedStatement ps; - - ps = connection.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);"); - ps.setString(1, playerUUID.toString()); - ps.setString(2, playerName); - ps.setInt(3, HuskHomes.getSettings().getFreeHomeSlots()); - ps.setInt(4, 0); - ps.setBoolean(5, false); - - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,0,?);")) { + statement.setString(1, playerUUID.toString()); + statement.setString(2, playerName); + statement.setInt(3, HuskHomes.getSettings().getFreeHomeSlots()); + statement.setBoolean(4, false); + statement.executeUpdate(); + } } public static void updatePlayerUsername(UUID uuid, String newName, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `username`=? WHERE `user_uuid`=?;"); - ps.setString(1, newName); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `username`=? WHERE `user_uuid`=?;")) { + statement.setString(1, newName); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } } private static void setPlayerTeleportingData(UUID uuid, boolean value, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_teleporting`=? WHERE `user_uuid`=?;"); - ps.setBoolean(1, value); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_teleporting`=? WHERE `user_uuid`=?;")) { + statement.setBoolean(1, value); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } } private static void setPlayerIgnoringRequestsData(UUID uuid, boolean value, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_ignoring_requests`=? WHERE `user_uuid`=?;"); - ps.setBoolean(1, value); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_ignoring_requests`=? WHERE `user_uuid`=?;")) { + statement.setBoolean(1, value); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } } public static void setPlayerHomeSlots(UUID uuid, int newValue, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `home_slots`=? WHERE `user_uuid`=?;"); - ps.setInt(1, newValue); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `home_slots`=? WHERE `user_uuid`=?;")) { + statement.setInt(1, newValue); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } } public static void setRtpCoolDown(UUID uuid, int newTime, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `user_uuid`=?;"); - ps.setInt(1, newTime); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `user_uuid`=?;")) { + statement.setInt(1, newTime); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } } - // Update the location of a home + // Update the privacy of a home public static void setHomePrivacy(String homeName, String ownerName, boolean isPublic, Connection connection) throws SQLException { - PreparedStatement ps; - - // Set the home location ID to the new teleport point for the given home - ps = connection.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.setString(3, ownerName); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);")) { + statement.setBoolean(1, isPublic); + statement.setString(2, homeName); + statement.setString(3, ownerName); + statement.executeUpdate(); + } } // Update the location of a home public static void setWarpTeleportPoint(String warpName, TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps; - - // Add the teleportation point - Integer locationID = addTeleportationPoint(point, connection); - - if (locationID != null) { - // Set the warp location ID to the new teleport point - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `location_id`=? WHERE `name`=?;"); - ps.setInt(1, locationID); - ps.setString(2, warpName); - ps.executeUpdate(); - ps.close(); + // Set the warp location ID to a new teleport point + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `location_id`=? WHERE `name`=?;")) { + statement.setInt(1, addTeleportationPoint(point, connection)); + statement.setString(2, warpName); + statement.executeUpdate(); } } // Update the description of a home public static void setHomeDescription(String homeName, String ownerName, String newDescription, Connection connection) throws SQLException { - PreparedStatement ps; - - // Update the home description - ps = connection.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.setString(3, ownerName); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);")) { + statement.setString(1, newDescription); + statement.setString(2, homeName); + statement.setString(3, ownerName); + statement.executeUpdate(); + } } // Update the description of a warp public static void setWarpDescription(String warpName, String newDescription, Connection connection) throws SQLException { - PreparedStatement ps; - - // Update the warp description - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `description`=? WHERE `name`=?;"); - ps.setString(1, newDescription); - ps.setString(2, warpName); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `description`=? WHERE `name`=?;")) { + statement.setString(1, newDescription); + statement.setString(2, warpName); + statement.executeUpdate(); + } } // Update the name of a home public static void setHomeName(String oldHomeName, String ownerName, String newHomeName, Connection connection) throws SQLException { - PreparedStatement ps; - - // Update the home name - ps = connection.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.setString(3, ownerName); - - ps.executeUpdate(); - + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);")) { + statement.setString(1, newHomeName); + statement.setString(2, oldHomeName); + statement.setString(3, ownerName); + statement.executeUpdate(); + } } // Update the name of a warp public static void setWarpName(String oldWarpName, String newWarpName, Connection connection) throws SQLException { - PreparedStatement ps; - - // Update the warp name - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `name`=? WHERE `name`=?;"); - ps.setString(1, newWarpName); - ps.setString(2, oldWarpName); - - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `name`=? WHERE `name`=?;")) { + statement.setString(1, newWarpName); + statement.setString(2, oldWarpName); + statement.executeUpdate(); + } } // Update a player's destination teleport point public static void setTeleportationDestinationData(String username, TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps; - - // Add the teleportation point - Integer locationID = addTeleportationPoint(point, connection); - - if (locationID != null) { + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=? WHERE `username`=?;")) { // Set the destination location with the location_id of the last inserted teleport point - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=? WHERE `username`=?;"); - ps.setInt(1, locationID); - ps.setString(2, username); - ps.executeUpdate(); - ps.close(); + statement.setInt(1, addTeleportationPoint(point, connection)); + statement.setString(2, username); + statement.executeUpdate(); } } @@ -580,48 +495,38 @@ public class DataManager { // Return a home with a given owner username and home name public static Home getHome(String ownerUsername, String homeName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - - ps = connection.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) { - if (rs.next()) { - final int locationID = rs.getInt("location_id"); - final TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); - final Home home = new Home(teleportationPoint, - ownerUsername, - getPlayerUUID(rs.getInt("player_id"), connection), - rs.getString("name"), - rs.getString("description"), - rs.getBoolean("public")); - ps.close(); - return home; + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?) AND `name`=?;")) { + statement.setString(1, ownerUsername); + statement.setString(2, homeName); + ResultSet resultSet = statement.executeQuery(); + if (resultSet != null) { + if (resultSet.next()) { + final int locationID = resultSet.getInt("location_id"); + final TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection); + final Home home = new Home(teleportationPoint, + ownerUsername, + getPlayerUUID(resultSet.getInt("player_id"), connection), + resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getBoolean("public"), + resultSet.getTimestamp("creation_time").getTime()); + statement.close(); + return home; + } } - } else { - Bukkit.getLogger().severe("Home returned null; perhaps player ID was null?"); } - ps.close(); return null; } public static boolean warpExists(String warpName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;"); - ps.setString(1, warpName); - rs = ps.executeQuery(); - if (rs != null) { - final boolean warpExists = rs.next(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;")) { + statement.setString(1, warpName); + ResultSet resultSet = statement.executeQuery(); + final boolean warpExists = resultSet.next(); + statement.close(); return warpExists; - } else { - Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a warp exists from the table."); - ps.close(); - return false; } } @@ -630,24 +535,15 @@ public class DataManager { } public static Boolean homeExists(String ownerName, String homeName, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - - - ps = connection.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) { - final boolean homeExists = rs.next(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?) AND `name`=?;")) { + statement.setString(1, ownerName); + statement.setString(2, homeName); + ResultSet resultSet = statement.executeQuery(); + final boolean homeExists = resultSet.next(); + statement.close(); return homeExists; - } else { - Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a home exists from the table."); - ps.close(); - return false; } - } public static Boolean playerExists(Player player, Connection connection) throws SQLException { @@ -655,41 +551,36 @@ public class DataManager { } public static Boolean playerExists(UUID playerUUID, Connection connection) throws SQLException { - PreparedStatement ps; - ResultSet rs; - String playerUUIDString = playerUUID.toString(); - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;"); - ps.setString(1, playerUUIDString); - rs = ps.executeQuery(); - if (rs != null) { - final boolean exists = rs.next(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `user_uuid`=?;")) { + statement.setString(1, playerUUID.toString()); + ResultSet resultSet = statement.executeQuery(); + final boolean exists = resultSet.next(); + statement.close(); return exists; - } else { - Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a player exists from the table."); - ps.close(); - return false; } } public static TeleportationPoint getTeleportationPoint(Integer locationID, Connection connection) throws SQLException, IllegalArgumentException { - PreparedStatement ps; - ResultSet rs; - - ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=?;"); - ps.setInt(1, locationID); - rs = ps.executeQuery(); - if (rs.next()) { - final TeleportationPoint point = new TeleportationPoint(rs.getString("world"), - rs.getDouble("x"), rs.getDouble("y"), - rs.getDouble("z"), rs.getFloat("yaw"), - rs.getFloat("pitch"), rs.getString("server")); - ps.close(); - return point; - } else { - ps.close(); - throw new IllegalArgumentException("Could not return a teleportationPoint from the locations data table at (ID#" + locationID + ")"); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=?;")) { + statement.setInt(1, locationID); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + final TeleportationPoint point = new TeleportationPoint( + resultSet.getString("world"), + resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + resultSet.getString("server")); + statement.close(); + return point; + } else { + statement.close(); + throw new IllegalArgumentException("Could not return a teleportationPoint from the locations data table at (ID#" + locationID + ")"); + } } } @@ -726,39 +617,32 @@ public class DataManager { // Insert a teleportation point, returns generated id public static Integer addTeleportationPoint(TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps = connection.prepareStatement("INSERT INTO " + HuskHomes.getSettings().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()) { - final int pointID = rs.getInt(1); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + HuskHomes.getSettings().getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);", Statement.RETURN_GENERATED_KEYS)) { + statement.setString(1, point.getWorldName()); + statement.setString(2, point.getServer()); + statement.setDouble(3, point.getX()); + statement.setDouble(4, point.getY()); + statement.setDouble(5, point.getZ()); + statement.setFloat(6, point.getYaw()); + statement.setFloat(7, point.getPitch()); + statement.executeUpdate(); + ResultSet resultSet = statement.getGeneratedKeys(); + resultSet.next(); + final int pointID = resultSet.getInt(1); + statement.close(); return pointID; } - return null; } // Update a player's offline position teleport point public static void setTeleportationOfflinePositionData(UUID uuid, TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps; - // Add the teleportation point - Integer locationID = addTeleportationPoint(point, connection); - - if (locationID != null) { - // Set the destination location with the location_id of the last inserted teleport point - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `offline_location_id`=? WHERE `user_uuid`=?;"); - ps.setInt(1, locationID); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + // Set the destination location with the location_id of the newly inserted teleport point + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `offline_location_id`=? WHERE `user_uuid`=?;")) { + statement.setInt(1, addTeleportationPoint(point, connection)); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); } } @@ -770,17 +654,12 @@ public class DataManager { // Update a player's last position teleport point public static void setTeleportationLastPositionData(UUID uuid, TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps; - // Add the teleportation point - Integer locationID = addTeleportationPoint(point, connection); - - if (locationID != null) { - // Set the destination location with the location_id of the last inserted teleport point - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=? WHERE `user_uuid`=?;"); - ps.setInt(1, locationID); - ps.setString(2, uuid.toString()); - ps.executeUpdate(); - ps.close(); + // Set the destination location with the location_id of the last inserted teleport point + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=? WHERE `user_uuid`=?;")) { + statement.setInt(1, addTeleportationPoint(point, connection)); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); } } @@ -814,7 +693,7 @@ public class DataManager { setPlayerIgnoringRequestsData(p.getUniqueId(), ignoringRequests, connection); } - public static void updateRtpCooldown(Player p, Connection connection) throws SQLException { + public static void updateRtpCoolDown(Player p, Connection connection) throws SQLException { long currentTime = Instant.now().getEpochSecond(); int newCoolDownTime = (int) currentTime + (60 * HuskHomes.getSettings().getRtpCoolDown()); setRtpCoolDown(p.getUniqueId(), newCoolDownTime, connection); @@ -830,11 +709,11 @@ public class DataManager { // Delete a teleportation point from SQL public static void deleteTeleportationPoint(int locationID, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=?;"); - ps.setInt(1, locationID); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "DELETE FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=?;")) { + statement.setInt(1, locationID); + statement.executeUpdate(); + } } public static void deletePlayerLastPosition(Player p, Connection connection) throws SQLException { @@ -855,82 +734,67 @@ public class DataManager { // Update the location of a home public static void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point, Connection connection) throws SQLException { - PreparedStatement ps; - // Add the teleportation point - Integer locationID = addTeleportationPoint(point, connection); - - if (locationID != null) { - // Set the home location ID to the new teleport point for the given home - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `location_id`=? WHERE `name`=? AND `player_id`=?;"); - ps.setInt(1, locationID); - ps.setString(2, homeName); - ps.setInt(3, ownerID); - ps.executeUpdate(); - ps.close(); + // Set the home location ID to the new teleport point for the given home + try (PreparedStatement statement = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `location_id`=? WHERE `name`=? AND `player_id`=?;")) { + statement.setInt(1, addTeleportationPoint(point, connection)); + statement.setString(2, homeName); + statement.setInt(3, ownerID); + statement.executeUpdate(); } } // Clear a player's destination public static void clearPlayerDestData(String playerName, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `username`=?;"); - ps.setString(1, playerName); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = (connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `username`=?;"))) { + statement.setString(1, playerName); + statement.executeUpdate(); + } } // Clear a player's last position public static void clearPlayerLastData(UUID uuid, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `user_uuid`=?;"); - ps.setString(1, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `user_uuid`=?;")) { + statement.setString(1, uuid.toString()); + statement.executeUpdate(); + } } // Clear a player's last position public static void clearPlayerOfflineData(UUID uuid, Connection connection) throws SQLException { - PreparedStatement ps; - ps = connection.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `offline_location_id`=NULL WHERE `user_uuid`=?;"); - ps.setString(1, uuid.toString()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `offline_location_id`=NULL WHERE `user_uuid`=?;")) { + statement.setString(1, uuid.toString()); + statement.executeUpdate(); + } } + // Insert the warp with the location_id of the last inserted teleport point public static void addWarp(Warp warp, Connection connection) throws SQLException { - PreparedStatement ps; - // Add the teleportation point - Integer locationID = addTeleportationPoint(warp, connection); - - if (locationID != null) { - // Insert the warp with the location_id of the last inserted teleport point - ps = connection.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getWarpsDataTable() + " (location_id,name,description) VALUES(?,?,?);"); - ps.setInt(1, locationID); - ps.setString(2, warp.getName()); - ps.setString(3, warp.getDescription()); - ps.executeUpdate(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + HuskHomes.getSettings().getWarpsDataTable() + " (location_id,name,description,creation_time) VALUES(?,?,?,?);")) { + statement.setInt(1, addTeleportationPoint(warp, connection)); + statement.setString(2, warp.getName()); + statement.setString(3, warp.getDescription()); + statement.setTimestamp(4, new Timestamp(System.currentTimeMillis())); + statement.executeUpdate(); } } public static void addHome(Home home, UUID playerUUID, Connection connection) throws SQLException { Integer playerID = getPlayerId(playerUUID, connection); if (playerID != null) { - PreparedStatement ps; - Integer locationID = addTeleportationPoint(home, connection); - - if (locationID != null) { - ps = connection.prepareStatement("INSERT INTO " + HuskHomes.getSettings().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(); - ps.close(); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + HuskHomes.getSettings().getHomesDataTable() + " (player_id,location_id,name,description,public,creation_time) VALUES(?,?,?,?,?,?);")) { + statement.setInt(1, playerID); + statement.setInt(2, addTeleportationPoint(home, connection)); + statement.setString(3, home.getName()); + statement.setString(4, home.getDescription()); + statement.setBoolean(5, home.isPublic()); + statement.setTimestamp(6, new Timestamp(System.currentTimeMillis())); + statement.executeUpdate(); } - } else { - Bukkit.getLogger().warning("Player ID was null; failed to add a home for a player!"); } } diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java index 6d9c4fc4..8566ac6a 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java @@ -48,6 +48,7 @@ public class MySQL extends Database { "`name` varchar(16) NOT NULL," + "`description` varchar(255) NOT NULL," + "`public` boolean NOT NULL," + + "`creation_time` timestamp NOT NULL," + "PRIMARY KEY (`player_id`, `name`)," + "FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + "FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + @@ -57,6 +58,7 @@ public class MySQL extends Database { "`location_id` integer NOT NULL," + "`name` varchar(16) NOT NULL UNIQUE," + "`description` varchar(255) NOT NULL," + + "`creation_time` timestamp NOT NULL," + "PRIMARY KEY (`location_id`)," + "FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + ");" diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java index 306b7877..96a23e0d 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java @@ -50,6 +50,7 @@ public class SQLite extends Database { "`name` varchar(16) NOT NULL," + "`description` varchar(255) NOT NULL," + "`public` boolean NOT NULL," + + "`creation_time` timestamp NOT NULL," + "PRIMARY KEY (`player_id`, `name`)," + "FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + "FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + @@ -59,6 +60,7 @@ public class SQLite extends Database { "`location_id` integer NOT NULL," + "`name` varchar(16) NOT NULL UNIQUE," + "`description` varchar(255) NOT NULL," + + "`creation_time` timestamp NOT NULL," + "PRIMARY KEY (`location_id`)," + "FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + ");" diff --git a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java index 10e1cb68..f1b2b132 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java @@ -64,7 +64,7 @@ public class PlayerListener implements Listener { HomeCommand.Tab.updatePlayerHomeCache(p); } - final Boolean isIgnoringRequests = DataManager.getPlayerIgnoringRequests(p, connection); + final Boolean isIgnoringRequests = DataManager.isPlayerIgnoringRequests(p.getUniqueId(), connection); if (isIgnoringRequests != null) { if (isIgnoringRequests) { HuskHomes.setIgnoringTeleportRequests(p.getUniqueId()); @@ -74,7 +74,7 @@ public class PlayerListener implements Listener { // If bungee mode, check if the player joined the server from a teleport and act accordingly if (HuskHomes.getSettings().doBungee()) { - final Boolean isTeleporting = DataManager.getPlayerTeleporting(p, connection); + final Boolean isTeleporting = DataManager.isPlayerTeleporting(p.getUniqueId(), connection); if (isTeleporting != null) { if (isTeleporting) { TeleportManager.teleportPlayer(p); diff --git a/src/main/java/me/william278/huskhomes2/listeners/PluginMessageListener.java b/src/main/java/me/william278/huskhomes2/listeners/PluginMessageListener.java index 527364a7..e00bb3a2 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PluginMessageListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PluginMessageListener.java @@ -113,7 +113,7 @@ public class PluginMessageListener implements org.bukkit.plugin.messaging.Plugin } } - @Override + @Override @SuppressWarnings("UnstableApiUsage") public void onPluginMessageReceived(String channel, Player player, byte[] message) { // Return if the message is not a Bungee message if (!channel.equals("BungeeCord")) { diff --git a/src/main/java/me/william278/huskhomes2/migrators/EssentialsMigrator.java b/src/main/java/me/william278/huskhomes2/migrators/EssentialsMigrator.java index 8931076e..6a79a8c7 100644 --- a/src/main/java/me/william278/huskhomes2/migrators/EssentialsMigrator.java +++ b/src/main/java/me/william278/huskhomes2/migrators/EssentialsMigrator.java @@ -17,6 +17,7 @@ import org.bukkit.configuration.file.FileConfiguration; import java.io.File; import java.sql.Connection; import java.sql.SQLException; +import java.time.Instant; import java.util.Set; import java.util.UUID; import java.util.logging.Level; @@ -94,7 +95,7 @@ public class EssentialsMigrator { continue; } DataManager.addHome(new Home(new TeleportationPoint(worldID, x, y, z, yaw, pitch, targetServer), - playerName, playerUUID, homeName, homeDescription, false), playerUUID, connection); + playerName, playerUUID, homeName, homeDescription, false, Instant.now().getEpochSecond()), playerUUID, connection); plugin.getLogger().info("→ Migrated home " + homeName); } catch (NullPointerException | IllegalArgumentException e) { @@ -152,7 +153,7 @@ public class EssentialsMigrator { } DataManager.addWarp(new Warp(new TeleportationPoint(worldID, x, y, z, yaw, pitch, targetServer), - warpName, MessageManager.getRawMessage("warp_default_description")), connection); + warpName, MessageManager.getRawMessage("warp_default_description"), Instant.now().getEpochSecond()), connection); plugin.getLogger().info("→ Migrated warp " + warpName); } catch (NullPointerException | IllegalArgumentException e) { plugin.getLogger().warning("✖ Failed to migrate warp " + warpFile.getName()); diff --git a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java index c5542a0a..620f9a7e 100644 --- a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java +++ b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java @@ -5,7 +5,8 @@ import org.bukkit.Bukkit; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.util.logging.Level; +import java.sql.Timestamp; +import java.time.Instant; public class UpgradeDatabase { @@ -35,6 +36,36 @@ public class UpgradeDatabase { } }); } + if (plugin.getConfig().getInt("config_file_version", 1) <= 6) { + plugin.getLogger().info("Detected that the database might need updating. Running database upgrade..."); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( + "ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable() + + " ADD `creation_time` timestamp NOT NULL DEFAULT ?;")) { + tableUpdateStatement.setTimestamp(1, new Timestamp(Instant.now().getEpochSecond())); + tableUpdateStatement.executeUpdate(); + plugin.getLogger().info("Database update complete!"); + } catch (SQLException e) { + plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); + } finally { + try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( + "ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable() + + " ADD `creation_time` timestamp NOT NULL DEFAULT ?;")) { + tableUpdateStatement.setTimestamp(1, new Timestamp(Instant.now().getEpochSecond())); + tableUpdateStatement.executeUpdate(); + plugin.getLogger().info("Database update complete!"); + } catch (SQLException e) { + plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); + } finally { + // Update the config file version + Bukkit.getScheduler().runTask(plugin, () -> { + plugin.getConfig().set("config_file_version", 8); + plugin.saveConfig(); + }); + } + } + }); + } } } diff --git a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java index f61c18ad..422c63d6 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java @@ -138,7 +138,7 @@ public class TeleportManager { public static void queueRandomTeleport(Player player, Connection connection) throws SQLException { if (!player.hasPermission("huskhomes.rtp.bypass_cooldown")) { long currentTime = Instant.now().getEpochSecond(); - long coolDownTime = DataManager.getPlayerRtpCooldown(player, connection); + long coolDownTime = DataManager.getPlayerRtpCoolDown(player, connection); if (currentTime < coolDownTime) { long timeRemaining = coolDownTime - currentTime; long timeRemainingMinutes = timeRemaining / 60; @@ -168,7 +168,7 @@ public class TeleportManager { } } teleportPlayer(player, randomPoint, connection); - DataManager.updateRtpCooldown(player, connection); + DataManager.updateRtpCoolDown(player, connection); return; } new TimedTeleport(player).begin(); diff --git a/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java b/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java index 83379d2b..8371f30d 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java @@ -127,7 +127,7 @@ public class TimedTeleport { } } TeleportManager.teleportPlayer(player, randomPoint, connection); - DataManager.updateRtpCooldown(player, connection); + DataManager.updateRtpCoolDown(player, connection); } } } catch (SQLException e) { diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/Home.java b/src/main/java/me/william278/huskhomes2/teleport/points/Home.java index b9ca389a..201ef3e4 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/Home.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/Home.java @@ -13,12 +13,9 @@ import java.util.UUID; * An object representing a Player's home in-game * @author William */ -public class Home extends TeleportationPoint { +public class Home extends SetPoint { - private String name; - private String description; private boolean isPublic; - private UUID ownerUUID; private String ownerUsername; @@ -28,14 +25,12 @@ public class Home extends TeleportationPoint { * @param server The Bungee server ID of the home * @param homeOwner The Player who owns the home * @param name The name of the home - * @param isPublic Whether or not the home is public + * @param isPublic Whether the home is public */ public Home(Location location, String server, Player homeOwner, String name, boolean isPublic) { - super(location, server); - this.name = name; + super(location, server, name, MessageManager.getRawMessage("home_default_description", homeOwner.getName())); this.ownerUUID = homeOwner.getUniqueId(); this.ownerUsername = homeOwner.getName(); - this.description = MessageManager.getRawMessage("home_default_description", ownerUsername); this.isPublic = isPublic; } @@ -47,14 +42,12 @@ public class Home extends TeleportationPoint { * @param ownerUUID The UUID string of the owner * @param name The name of the home * @param description The description of the home - * @param isPublic Whether or not the home is public + * @param isPublic Whether the home is public */ - public Home(TeleportationPoint teleportationPoint, String ownerUsername, String ownerUUID, String name, String description, boolean isPublic) throws IllegalArgumentException { - super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server); + public Home(TeleportationPoint teleportationPoint, String ownerUsername, String ownerUUID, String name, String description, boolean isPublic, long creationTime) throws IllegalArgumentException { + super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server, name, description, creationTime); this.ownerUsername = ownerUsername; this.ownerUUID = UUID.fromString(ownerUUID); - this.name = name; - this.description = description; this.isPublic = isPublic; } @@ -65,19 +58,17 @@ public class Home extends TeleportationPoint { * @param ownerUUID The {@link UUID} of the owner * @param name The name of the home * @param description The description of the home - * @param isPublic Whether or not the home is public + * @param isPublic Whether the home is public */ - public Home(TeleportationPoint teleportationPoint, String ownerUsername, UUID ownerUUID, String name, String description, boolean isPublic) { - super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server); + public Home(TeleportationPoint teleportationPoint, String ownerUsername, UUID ownerUUID, String name, String description, boolean isPublic, long creationTime) { + super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server, name, description, creationTime); this.ownerUsername = ownerUsername; this.ownerUUID = ownerUUID; - this.name = name; - this.description = description; this.isPublic = isPublic; } /** - * Returns whether or not the Home is public + * Returns whether the Home is public * @return if the Home is public */ public boolean isPublic() { @@ -130,73 +121,41 @@ public class Home extends TeleportationPoint { this.ownerUsername = player.getName(); } - /** - * Returns the name of the home - * @return the Home name - */ - public String getName() { - return name; - } - - /** - * Returns the description of the home - * @return the Home description - */ - public String getDescription() { - return description; - } - - /** - * Set the name of the home - * @param name the new Home name - */ - public void setName(String name) { - this.name = name; - } - - /** - * Set the description of the home - * @param description the new Home description - */ - public void setDescription(String description) { - this.description = description; - } - // Returns the maximum number of set homes a player can make public static int getSetHomeLimit(Player p) { p.recalculatePermissions(); - int maxSethomes = -1; + int maxSetHomes = -1; for (PermissionAttachmentInfo permissionAI : p.getEffectivePermissions()) { String permission = permissionAI.getPermission(); if (permission.contains("huskhomes.max_sethomes.")) { try { - if (Integer.parseInt(permission.split("\\.")[2]) > maxSethomes) { - maxSethomes = Integer.parseInt(permission.split("\\.")[2]); + if (Integer.parseInt(permission.split("\\.")[2]) > maxSetHomes) { + maxSetHomes = Integer.parseInt(permission.split("\\.")[2]); } } catch (Exception ignored) { } } } - if (maxSethomes != -1) { - return maxSethomes; + if (maxSetHomes != -1) { + return maxSetHomes; } return HuskHomes.getSettings().getMaximumHomes(); } // Returns the number of set homes a player can set for free public static int getFreeHomes(Player p) { - int freeSethomes = -1; + int freeSetHomes = -1; for (PermissionAttachmentInfo permissionAI : p.getEffectivePermissions()) { String permission = permissionAI.getPermission(); if (permission.contains("huskhomes.free_sethomes.")) { try { - if (Integer.parseInt(permission.split("\\.")[2]) > freeSethomes) { - freeSethomes = Integer.parseInt(permission.split("\\.")[2]); + if (Integer.parseInt(permission.split("\\.")[2]) > freeSetHomes) { + freeSetHomes = Integer.parseInt(permission.split("\\.")[2]); } } catch(Exception ignored) { } } } - if (freeSethomes != -1) { - return freeSethomes; + if (freeSetHomes != -1) { + return freeSetHomes; } return HuskHomes.getSettings().getFreeHomeSlots(); } diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java b/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java new file mode 100644 index 00000000..6c170d49 --- /dev/null +++ b/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java @@ -0,0 +1,79 @@ +package me.william278.huskhomes2.teleport.points; + +import org.bukkit.Location; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; + +public class SetPoint extends TeleportationPoint { + + private String name; + private String description; + private final long creationTime; + + public SetPoint(String worldName, double x, double y, double z, float yaw, float pitch, String server, String name, String description, long creationTime) { + super(worldName, x, y, z, yaw, pitch, server); + this.name = name; + this.description = description; + this.creationTime = creationTime; + } + + public SetPoint(Location location, String serverName, String name, String description) { + super(location, serverName); + this.name = name; + this.description = description; + this.creationTime = Instant.now().getEpochSecond(); + } + + /** + * Returns the name of the point + * @return the point name + */ + public String getName() { + return name; + } + + /** + * Returns the description of the point + * @return the point description + */ + public String getDescription() { + return description; + } + + /** + * Set the name of the point + * @param name the new point name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Set the description of the point + * @param description the new point description + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Get the creation time epoch seconds of the point + */ + public long getCreationTime() { + return creationTime; + } + + /** + * Return a formatted string displaying the time this point was created + * @return The formatted creation time string + */ + public String getFormattedCreationTime() { + return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT) + .withLocale(Locale.getDefault()) + .withZone(ZoneId.systemDefault()) + .format(Instant.ofEpochSecond(creationTime)); + } +} diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/Warp.java b/src/main/java/me/william278/huskhomes2/teleport/points/Warp.java index ed8e3310..4b82ef01 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/Warp.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/Warp.java @@ -11,10 +11,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo; * An object representing an in-game warp * @author William */ -public class Warp extends TeleportationPoint { - - private String name; - private String description; +public class Warp extends SetPoint { /** * An object representing an in-game warp @@ -23,9 +20,7 @@ public class Warp extends TeleportationPoint { * @param name The name of the Warp */ public Warp(Location location, String server, String name) { - super(location, server); - this.name = name; - this.description = MessageManager.getRawMessage("warp_default_description"); + super(location, server, name, MessageManager.getRawMessage("warp_default_description")); } /** @@ -34,51 +29,17 @@ public class Warp extends TeleportationPoint { * @param name The name of the Warp * @param description The description of the Warp */ - public Warp(TeleportationPoint teleportationPoint, String name, String description) { - super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server); - this.name = name; - this.description = description; + public Warp(TeleportationPoint teleportationPoint, String name, String description, long creationTime) { + super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server, name, description, creationTime); } /** - * Returns the description of the Warp - * @return the Warp's description - */ - public String getDescription() { - return description; - } - - /** - * Returns the name of the Warp - * @return the Warp's name - */ - public String getName() { - return name; - } - - /** - * Set the name of the Warp - * @param name the new Warp name - */ - public void setName(String name) { - this.name = name; - } - - /** - * Set the description of the Warp - * @param description the new Warp description - */ - public void setDescription(String description) { - this.description = description; - } - - /** - * Whether or not the player can use the warp + * Whether the player can use the warp * @param player The player - * @return boolean; whether or not they can use the warp + * @return boolean; whether they can use the warp */ public boolean canUse(Player player) { - return getWarpCanUse(player, name); + return getWarpCanUse(player, getName()); } // Returns if the player has permission to access the warp diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e41397b7..189c0b3e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -87,4 +87,4 @@ economy_integration: random_teleport: 20 back: 0 handle_vanished_players: true -config_file_version: 7 \ No newline at end of file +config_file_version: 8 \ No newline at end of file diff --git a/src/main/resources/Languages/en-gb.yml b/src/main/resources/languages/en-gb.yml similarity index 99% rename from src/main/resources/Languages/en-gb.yml rename to src/main/resources/languages/en-gb.yml index 6e976303..8b49cbf6 100644 --- a/src/main/resources/Languages/en-gb.yml +++ b/src/main/resources/languages/en-gb.yml @@ -42,6 +42,7 @@ edit_description: '• Description: &7%1%' edit_location: '• Location: &7%1%, %2%, %3%' edit_world: '• World: &7%1%' edit_server: '• Server: &7%1%' +edit_timestamp: '• Created: &7%1%' error_no_homes_set: '[Error:](#ff3300) [You haven''t set any homes!](#ff7e5e)' error_no_public_homes_set: '[Error:](#ff3300) [There aren''t any public homes set!](#ff7e5e)' error_no_warps_set: '[Error:](#ff3300) [There aren''t any warps set!](#ff7e5e)' diff --git a/src/main/resources/Languages/it-it.yml b/src/main/resources/languages/it-it.yml similarity index 99% rename from src/main/resources/Languages/it-it.yml rename to src/main/resources/languages/it-it.yml index bdc02444..dbf153d1 100644 --- a/src/main/resources/Languages/it-it.yml +++ b/src/main/resources/languages/it-it.yml @@ -42,6 +42,7 @@ edit_description: '• Descrizione: &7%1%' edit_location: '• Posizione: &7%1%, %2%, %3%' edit_world: '• Mondo: &7%1%' edit_server: '• Server: &7%1%' +edit_timestamp: '• Created: &7%1%' error_no_homes_set: '[Errore:](#ff3300) [Non hai impostato nessuna home! Scegli un luogo sicuro e digita /sethome](#ff7e5e)' error_no_public_homes_set: '[Errore:](#ff3300) [Non ci sono home pubbliche!](#ff7e5e)' error_no_warps_set: '[Errore:](#ff3300) [Non c’’è alcun warp!](#ff7e5e)' diff --git a/src/main/resources/Languages/pl.yml b/src/main/resources/languages/pl.yml similarity index 99% rename from src/main/resources/Languages/pl.yml rename to src/main/resources/languages/pl.yml index 8cef3c2f..f3c60210 100644 --- a/src/main/resources/Languages/pl.yml +++ b/src/main/resources/languages/pl.yml @@ -42,6 +42,7 @@ edit_description: '• Opis: &7%1%' edit_location: '• Lokalizacja: &7%1%, %2%, %3%' edit_world: '• Świat: &7%1%' edit_server: '• Serwer: &7%1%' +edit_timestamp: '• Created: &7%1%' error_no_homes_set: '[error:](#ff3300) [Nie masz żadnych domów!](#ff7e5e)' error_no_public_homes_set: '[error:](#ff3300) [Nie ma żadnych publicznych domów](#ff7e5e)' error_no_warps_set: '[error:](#ff3300) [Nie ma żadnych warpów!](#ff7e5e)' diff --git a/src/main/resources/Languages/zh-cn.yml b/src/main/resources/languages/zh-cn.yml similarity index 99% rename from src/main/resources/Languages/zh-cn.yml rename to src/main/resources/languages/zh-cn.yml index 29b9f9f6..75c36816 100644 --- a/src/main/resources/Languages/zh-cn.yml +++ b/src/main/resources/languages/zh-cn.yml @@ -42,6 +42,7 @@ edit_description: '• 描述: &7%1%' edit_location: '• 坐标: &7%1%, %2%, %3%' edit_world: '• 世界: &7%1%' edit_server: '• 服务器: &7%1%' +edit_timestamp: '• Created: &7%1%' error_no_homes_set: '[错误:](#ff3300) [你还没有设置任何一个家!](#ff7e5e)' error_no_public_homes_set: '[错误:](#ff3300) [没有公共的家被设置!](#ff7e5e)' error_no_warps_set: '[错误:](#ff3300) [没有地标被设置!](#ff7e5e)'