diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index e8ce43d8..5de01a60 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -155,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/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 7267fc58..b974e29a 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,8 +706,8 @@ public class DataManager { } } - public static TeleportationPoint getPlayerOfflinePosition(Player p, Connection connection) throws SQLException { - Integer locationID = getPlayerInteger(p, "offline_location_id", connection); + public static TeleportationPoint getPlayerOfflinePosition(int playerID, Connection connection) throws SQLException { + Integer locationID = getPlayerInteger(playerID, "offline_location_id", connection); if (locationID != null) { return getTeleportationPoint(locationID, connection); } else { @@ -754,9 +754,9 @@ public class DataManager { } // 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); + 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 @@ -793,12 +793,12 @@ public class DataManager { } 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()); } + setPlayerTeleportingData(p.getUniqueId(), teleporting, connection); } public static void setPlayerIgnoringRequests(Player p, boolean ignoringRequests, Connection connection) throws SQLException { @@ -836,11 +836,11 @@ public class DataManager { } } - public static void deletePlayerOfflinePosition(Player p, Connection connection) throws SQLException { - Integer offlineLocationId = getPlayerInteger(p, "offline_location_id", connection); + 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(p.getUniqueId(), connection); + clearPlayerOfflineData(uuid, connection); } } diff --git a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java index 04fbeaf4..2360a7e6 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java @@ -8,6 +8,7 @@ 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; @@ -17,6 +18,7 @@ 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 { @@ -102,11 +104,19 @@ public class PlayerListener implements Listener { @EventHandler public void onPlayerQuit(PlayerQuitEvent e) { Player p = e.getPlayer(); - if (HuskHomes.isTeleporting(p.getUniqueId())) { - HuskHomes.setNotTeleporting(p.getUniqueId()); + final UUID uuid = p.getUniqueId(); + if (HuskHomes.isTeleporting(uuid)) { + final Location logOutLocation = p.getLocation(); + HuskHomes.setNotTeleporting(uuid); Connection connection = HuskHomes.getConnection(); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - // Set player offline position + 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."); + } }); } } diff --git a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java index 524ca04b..904199ca 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java @@ -85,6 +85,20 @@ 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; + } + 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..34976fb9 100644 --- a/src/main/resources/Languages/en-gb.yml +++ b/src/main/resources/Languages/en-gb.yml @@ -105,4 +105,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 from](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8dfd23d3..a64d99cc 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 @@ -227,6 +231,9 @@ permissions: huskhomes.tpignore: description: Allows you to ingore teleport requests default: true + huskhomes.tpoffline: + description: Allows you to teleport to where players logged off + default: false huskhomes.back.death: description: Allows you to return to where you died default: true