diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 92b651a9..e8ce43d8 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -68,6 +68,17 @@ public final class HuskHomes extends JavaPlugin { ignoringTeleportRequests.remove(uuid); } + // Ignoring teleport requests handler + private static final HashSet teleportingPlayers = new HashSet<>(); + public static boolean isTeleporting(UUID uuid) { return teleportingPlayers.contains(uuid); } + public static void setTeleporting(UUID uuid) { + teleportingPlayers.add(uuid); + } + public static void setNotTeleporting(UUID uuid) { + teleportingPlayers.remove(uuid); + } + + // Metrics ID for bStats integration private static final int METRICS_PLUGIN_ID = 8430; /** diff --git a/src/main/java/me/william278/huskhomes2/commands/TpIgnoreCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpIgnoreCommand.java index f04d9123..4f752112 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpIgnoreCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpIgnoreCommand.java @@ -15,21 +15,26 @@ public class TpIgnoreCommand extends CommandBase { @Override protected void onCommand(Player p, Command command, String label, String[] args) { + boolean ignoring; if (HuskHomes.isIgnoringTeleportRequests(p.getUniqueId())) { MessageManager.sendMessage(p, "tpignore_toggle_off"); - HuskHomes.setNotIgnoringTeleportRequests(p.getUniqueId()); - setIgnoringRequestsData(p, false); + ignoring = false; } else { MessageManager.sendMessage(p, "tpignore_toggle_on"); - HuskHomes.setIgnoringTeleportRequests(p.getUniqueId()); - setIgnoringRequestsData(p, true); + ignoring = true; } + setIgnoringRequestsData(p, ignoring); } - private void setIgnoringRequestsData(Player player, boolean isIgnoring) { + private void setIgnoringRequestsData(Player player, boolean ignoring) { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try { - DataManager.setPlayerIgnoringRequests(player, isIgnoring, HuskHomes.getConnection()); + DataManager.setPlayerIgnoringRequests(player, ignoring, HuskHomes.getConnection()); + if (ignoring) { + HuskHomes.setIgnoringTeleportRequests(player.getUniqueId()); + } else { + HuskHomes.setNotIgnoringTeleportRequests(player.getUniqueId()); + } } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/me/william278/huskhomes2/data/DataManager.java b/src/main/java/me/william278/huskhomes2/data/DataManager.java index c77e8545..7267fc58 100644 --- a/src/main/java/me/william278/huskhomes2/data/DataManager.java +++ b/src/main/java/me/william278/huskhomes2/data/DataManager.java @@ -706,6 +706,15 @@ public class DataManager { } } + public static TeleportationPoint getPlayerOfflinePosition(Player p, Connection connection) throws SQLException { + Integer locationID = getPlayerInteger(p, "offline_location_id", connection); + if (locationID != null) { + return getTeleportationPoint(locationID, connection); + } else { + return null; + } + } + // 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); @@ -728,6 +737,28 @@ public class DataManager { 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(); + } + } + + // Update a player's last position location on SQL + public static void setPlayerOfflinePosition(Player p, TeleportationPoint point, Connection connection) throws SQLException { + deletePlayerOfflinePosition(p, connection); + setTeleportationOfflinePositionData(p.getUniqueId(), point, connection); + } + // Update a player's last position teleport point public static void setTeleportationLastPositionData(UUID uuid, TeleportationPoint point, Connection connection) throws SQLException { PreparedStatement ps; @@ -761,12 +792,17 @@ public class DataManager { setPlayerDestinationLocation(p.getName(), point, connection); } - public static void setPlayerTeleporting(Player p, boolean value, Connection connection) throws SQLException { - setPlayerTeleportingData(p.getUniqueId(), value, connection); + public static void setPlayerTeleporting(Player p, boolean teleporting, Connection connection) throws SQLException { + setPlayerTeleportingData(p.getUniqueId(), teleporting, connection); + if (teleporting) { + HuskHomes.setTeleporting(p.getUniqueId()); + } else { + HuskHomes.setNotTeleporting(p.getUniqueId()); + } } - public static void setPlayerIgnoringRequests(Player p, boolean value, Connection connection) throws SQLException { - setPlayerIgnoringRequestsData(p.getUniqueId(), value, connection); + public static void setPlayerIgnoringRequests(Player p, boolean ignoringRequests, Connection connection) throws SQLException { + setPlayerIgnoringRequestsData(p.getUniqueId(), ignoringRequests, connection); } public static void updateRtpCooldown(Player p, Connection connection) throws SQLException { @@ -800,6 +836,14 @@ public class DataManager { } } + public static void deletePlayerOfflinePosition(Player p, Connection connection) throws SQLException { + Integer offlineLocationId = getPlayerInteger(p, "offline_location_id", connection); + if (offlineLocationId != null) { + deleteTeleportationPoint(offlineLocationId, connection); + clearPlayerOfflineData(p.getUniqueId(), connection); + } + } + // Update the location of a home public static void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point, Connection connection) throws SQLException { PreparedStatement ps; @@ -835,6 +879,15 @@ public class DataManager { ps.close(); } + // 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(); + } + public static void addWarp(Warp warp, Connection connection) throws SQLException { PreparedStatement ps; // Add the teleportation point 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 1c3018a1..6d9c4fc4 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java @@ -33,9 +33,11 @@ public class MySQL extends Database { "`is_teleporting` boolean NOT NULL DEFAULT 0," + "`dest_location_id` integer NULL," + "`last_location_id` integer NULL," + + "`offline_location_id` integer NULL," + "`is_ignoring_requests` boolean NOT NULL DEFAULT 0," + "PRIMARY KEY (`player_id`)," + + "FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + "FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + "FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL 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 227548a1..118c8122 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java @@ -35,8 +35,10 @@ public class SQLite extends Database { "`is_teleporting` boolean NOT NULL DEFAULT 0," + "`dest_location_id` integer NULL," + "`last_location_id` integer NULL," + + "`offline_location_id` integer NULL," + "`is_ignoring_requests` boolean NOT NULL DEFAULT 0," + "PRIMARY KEY (`player_id`)," + + "FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + "FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + "FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL 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 3773f16a..04fbeaf4 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java @@ -13,6 +13,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.sql.Connection; import java.sql.SQLException; @@ -97,4 +98,16 @@ public class PlayerListener implements Listener { HuskHomes.getPlayerList().addPlayer(p.getName()); } } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + Player p = e.getPlayer(); + if (HuskHomes.isTeleporting(p.getUniqueId())) { + HuskHomes.setNotTeleporting(p.getUniqueId()); + Connection connection = HuskHomes.getConnection(); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + // Set player offline position + }); + } + } } diff --git a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java index 0685b63e..13f6a9ce 100644 --- a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java +++ b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java @@ -17,7 +17,11 @@ public class UpgradeDatabase { if (plugin.getConfig().getInt("config_file_version", 1) <= 5) { plugin.getLogger().info("Detected that the database needs updating. Running database upgrade..."); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - try (PreparedStatement statement = HuskHomes.getConnection().prepareStatement("ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0")) { + try (PreparedStatement statement = HuskHomes.getConnection().prepareStatement( + "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0, " + + "`offline_location_id` integer NULL DEFAULT NULL, " + + "FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION;")) { statement.executeUpdate(); plugin.getLogger().info("Database update complete!"); } catch (SQLException e) {