diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 92b651a9..5de01a60 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; /** @@ -144,6 +155,7 @@ public final class HuskHomes extends JavaPlugin { new SetHomeCommand().register(getCommand("sethome")).setTabCompleter(emptyTab); new SetWarpCommand().register(getCommand("setwarp")).setTabCompleter(emptyTab); new BackCommand().register(getCommand("back")).setTabCompleter(emptyTab); + new TpOfflineCommand().register(getCommand("tpoffline")).setTabCompleter(emptyTab); // Update caches PublicHomeCommand.updatePublicHomeTabCache(); diff --git a/src/main/java/me/william278/huskhomes2/MessageManager.java b/src/main/java/me/william278/huskhomes2/MessageManager.java index 512d6b5b..ef1ec37d 100644 --- a/src/main/java/me/william278/huskhomes2/MessageManager.java +++ b/src/main/java/me/william278/huskhomes2/MessageManager.java @@ -72,7 +72,7 @@ public class MessageManager { } saveFile(config, f); messages.clear(); - for (String message : config.getConfigurationSection("").getKeys(false)) { + for (String message : config.getKeys(false)) { messages.put(message, StringEscapeUtils.unescapeJava(config.getString(message))); } } 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/commands/TpOfflineCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpOfflineCommand.java new file mode 100644 index 00000000..0f85d3c9 --- /dev/null +++ b/src/main/java/me/william278/huskhomes2/commands/TpOfflineCommand.java @@ -0,0 +1,34 @@ +package me.william278.huskhomes2.commands; + +import me.william278.huskhomes2.HuskHomes; +import me.william278.huskhomes2.MessageManager; +import me.william278.huskhomes2.teleport.TeleportManager; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.logging.Level; + +public class TpOfflineCommand extends CommandBase { + + private static final HuskHomes plugin = HuskHomes.getInstance(); + + @Override + protected void onCommand(Player p, Command command, String label, String[] args) { + if (args.length == 1) { + final String targetPlayer = args[0]; + Connection connection = HuskHomes.getConnection(); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try { + TeleportManager.teleportToOfflinePlayer(p, targetPlayer, connection); + } catch (SQLException e) { + plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred teleporting you to a player's offline position"); + } + }); + } else { + MessageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); + } + } +} diff --git a/src/main/java/me/william278/huskhomes2/data/DataManager.java b/src/main/java/me/william278/huskhomes2/data/DataManager.java index c77e8545..cedf1921 100644 --- a/src/main/java/me/william278/huskhomes2/data/DataManager.java +++ b/src/main/java/me/william278/huskhomes2/data/DataManager.java @@ -36,7 +36,7 @@ public class DataManager { } // Return a player's ID from their username - private static Integer getPlayerId(String playerUsername, Connection connection) throws SQLException { + public static Integer getPlayerId(String playerUsername, Connection connection) throws SQLException { PreparedStatement ps; ResultSet rs; @@ -706,6 +706,19 @@ public class DataManager { } } + public static TeleportationPoint getPlayerOfflinePosition(int playerID, Connection connection) throws SQLException { + try { + Integer locationID = getPlayerInteger(playerID, "offline_location_id", connection); + if (locationID != null) { + return getTeleportationPoint(locationID, connection); + } else { + return null; + } + } catch (IllegalArgumentException e) { + 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 +741,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(UUID uuid, TeleportationPoint point, Connection connection) throws SQLException { + deletePlayerOfflinePosition(uuid, connection); + setTeleportationOfflinePositionData(uuid, 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 +796,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 { + if (teleporting) { + HuskHomes.setTeleporting(p.getUniqueId()); + } else { + HuskHomes.setNotTeleporting(p.getUniqueId()); + } + setPlayerTeleportingData(p.getUniqueId(), teleporting, connection); } - 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 +840,14 @@ public class DataManager { } } + public static void deletePlayerOfflinePosition(UUID uuid, Connection connection) throws SQLException { + Integer offlineLocationId = getPlayerInteger(getPlayerId(uuid, connection), "offline_location_id", connection); + if (offlineLocationId != null) { + deleteTeleportationPoint(offlineLocationId, connection); + clearPlayerOfflineData(uuid, 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 +883,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..a623f406 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java @@ -8,14 +8,17 @@ import me.william278.huskhomes2.data.DataManager; import me.william278.huskhomes2.teleport.TeleportManager; import me.william278.huskhomes2.teleport.points.TeleportationPoint; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.entity.Player; 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; +import java.util.UUID; import java.util.logging.Level; public class PlayerListener implements Listener { @@ -97,4 +100,25 @@ public class PlayerListener implements Listener { HuskHomes.getPlayerList().addPlayer(p.getName()); } } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + Player p = e.getPlayer(); + final UUID uuid = p.getUniqueId(); + if (!HuskHomes.isTeleporting(uuid)) { + final Location logOutLocation = p.getLocation(); + Connection connection = HuskHomes.getConnection(); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try { + DataManager.setPlayerOfflinePosition(uuid, + new TeleportationPoint(logOutLocation, HuskHomes.getSettings().getServerID()), + connection); + } catch (SQLException ex) { + Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a warp exists from the table."); + } + }); + } else { + HuskHomes.setNotTeleporting(uuid); + } + } } 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) { diff --git a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java index c6b9125f..f61c18ad 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java @@ -85,6 +85,21 @@ public class TeleportManager { new TimedTeleport(player, point, TimedTeleport.TargetType.POINT).begin(); } + public static void teleportToOfflinePlayer(Player player, String targetPlayer, Connection connection) throws SQLException { + final Integer playerID = DataManager.getPlayerId(targetPlayer, connection); + if (playerID == null) { + MessageManager.sendMessage(player, "error_player_not_found", targetPlayer); + return; + } + TeleportationPoint offlinePoint = DataManager.getPlayerOfflinePosition(playerID, connection); + if (offlinePoint == null) { + MessageManager.sendMessage(player, "error_no_offline_position", targetPlayer); + return; + } + MessageManager.sendMessage(player, "teleporting_offline_player", targetPlayer); + teleportPlayer(player, offlinePoint, connection); + } + public static void queueBackTeleport(Player player, Connection connection) throws SQLException { try { TeleportationPoint lastPosition = DataManager.getPlayerLastPosition(player, connection); diff --git a/src/main/resources/Languages/en-gb.yml b/src/main/resources/Languages/en-gb.yml index 95e23109..f37ff7ff 100644 --- a/src/main/resources/Languages/en-gb.yml +++ b/src/main/resources/Languages/en-gb.yml @@ -1,6 +1,7 @@ error_no_permission: '[Error:](#ff3300) [You do not have permission to execute that command.](#ff7e5e)' error_no_last_position: '[Error:](#ff3300) [You have no last position to return to!](#ff7e5e)' teleporting_last_position: '[Returning to your last position in](#00fb9a) [%1%](#00fb9a bold) [seconds](#00fb9a)' +teleporting_offline_player: '[Teleporting you to where %1% last logged out...](#00fb9a)' teleporting_countdown_start: '[You will be teleported in](#00fb9a) [%1%](#00fb9a bold) [seconds](#00fb9a)' teleporting_cancelled_movement: '[Teleportation cancelled; you moved!](#ff7e5e)' teleporting_cancelled_damage: '[Teleportation cancelled; you took damage!](#ff7e5e)' @@ -105,4 +106,5 @@ command_list_header: '\n[HuskHomes](#00fb9a bold) [| Command List](#00fb9a)' error_invalid_on_arrival: '[Error:](#ff3300) [Failed to finish your teleportation as the target world could not be found.](#ff7e5e)' tpignore_toggle_on: '[You are now](#00fb9a) [ignoring](#00fb9a bold) [incoming teleport requests.](#00fb9a) [(Click to toggle)](gray show_text=&7Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' tpignore_toggle_off: '[You are now](#00fb9a) [listening](#00fb9a bold) [to any incoming teleport requests.](#00fb9a) [(Click to toggle)](gray show_text=&7Click to ignore incoming teleport requests run_command=/huskhomes:tpignore)' -error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' \ No newline at end of file +error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' +error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last logged out.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/Languages/it-it.yml b/src/main/resources/Languages/it-it.yml index 29de9fcc..f6859272 100644 --- a/src/main/resources/Languages/it-it.yml +++ b/src/main/resources/Languages/it-it.yml @@ -1,6 +1,7 @@ error_no_permission: '[Errore:](#ff3300) [Non hai il permesso di eseguire questo comando.](#ff7e5e)' error_no_last_position: '[Errore:](#ff3300) [Non hai nessuna posizione precedente salvata!](#ff7e5e)' teleporting_last_position: '[Teletrasporto alla posizione precedente tra](#00fb9a) [%1%](#00fb9a bold) [secondi](#00fb9a)' +teleporting_offline_player: '[Teleporting you to where %1% last logged out...](#00fb9a)' teleporting_countdown_start: '[Verrai teletrasportato tra](#00fb9a) [%1%](#00fb9a bold) [secondi](#00fb9a)' teleporting_cancelled_movement: '[Teletrasporto cancellato: ti sei mosso!](#ff7e5e)' teleporting_cancelled_damage: '[Teletrasporto cancellato: hai preso danno!](#ff7e5e)' @@ -105,4 +106,5 @@ command_list_header: '\n[HuskHomes](#00fb9a bold) [| Comandi](#00fb9a)' error_invalid_on_arrival: '[Errore:](#ff3300) [Non sono riuscito a teletrasportarti perché il mondo non è caricato correttamente.](#ff7e5e)' tpignore_toggle_on: '[You are now](#00fb9a) [ignoring](#00fb9a bold) [incoming teleport requests.](#00fb9a) [(Click to toggle)](gray show_text=&7Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' tpignore_toggle_off: '[You are now](#00fb9a) [listening](#00fb9a bold) [to any incoming teleport requests.](#00fb9a) [(Click to toggle)](gray show_text=&7Click to ignore incoming teleport requests run_command=/huskhomes:tpignore)' -error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' \ No newline at end of file +error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests!](#ff7e5e) [(Click to toggle)](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' +error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last logged out.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8dfd23d3..a8c00024 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -100,6 +100,10 @@ commands: usage: /tpignore permission: huskhomes.tpignore aliases: [tpaignore, ignoretprequests] + tpoffline: + description: Teleport to where a player was last online + usage: /tpoffline + permission: huskhomes.tpoffline rtp: description: Teleport randomly into the wild usage: /rtp @@ -207,8 +211,6 @@ permissions: huskhomes.rtp.bypass_cooldown: description: Allows you to bypass the /rtp cooldown default: false - children: - huskhomes.rtp: true huskhomes.about: description: View information about the plugin default: true @@ -220,13 +222,16 @@ permissions: default: op huskhomes.tp: description: Allows you to directly teleport to players or locations - default: false + default: op huskhomes.tprequest: description: Allows you to request to teleport to a player default: true huskhomes.tpignore: description: Allows you to ingore teleport requests default: true + huskhomes.tpoffline: + description: Allows you to teleport to where players logged off + default: op huskhomes.back.death: description: Allows you to return to where you died default: true @@ -236,10 +241,6 @@ permissions: huskhomes.spawn.set: description: Allows you to set the spawn location default: op - children: - huskhomes.spawn: true huskhomes.edithome.public: description: Allows you to make a home public - default: true - children: - huskhomes.edithome: true \ No newline at end of file + default: true \ No newline at end of file