Added offline player handling

Signed-off-by: BuildTools <[email protected]>
This commit is contained in:
BuildTools committed 2021-07-12 23:12:00 +03:00
1 parent 8b104a04cd
commit 10210da792
8 files changed
+82 -15

No files matched your search

@@ -155,6 +155,7 @@ public final class HuskHomes extends JavaPlugin {
new SetHomeCommand().register(getCommand("sethome")).setTabCompleter(emptyTab); new SetHomeCommand().register(getCommand("sethome")).setTabCompleter(emptyTab);
new SetWarpCommand().register(getCommand("setwarp")).setTabCompleter(emptyTab); new SetWarpCommand().register(getCommand("setwarp")).setTabCompleter(emptyTab);
new BackCommand().register(getCommand("back")).setTabCompleter(emptyTab); new BackCommand().register(getCommand("back")).setTabCompleter(emptyTab);
new TpOfflineCommand().register(getCommand("tpoffline")).setTabCompleter(emptyTab);
// Update caches // Update caches
PublicHomeCommand.updatePublicHomeTabCache(); PublicHomeCommand.updatePublicHomeTabCache();
@@ -72,7 +72,7 @@ public class MessageManager {
} }
saveFile(config, f); saveFile(config, f);
messages.clear(); messages.clear();
for (String message : config.getConfigurationSection("").getKeys(false)) { for (String message : config.getKeys(false)) {
messages.put(message, StringEscapeUtils.unescapeJava(config.getString(message))); messages.put(message, StringEscapeUtils.unescapeJava(config.getString(message)));
} }
} }
@@ -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());
}
}
}
@@ -36,7 +36,7 @@ public class DataManager {
} }
// Return a player's ID from their username // 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; PreparedStatement ps;
ResultSet rs; ResultSet rs;
@@ -706,8 +706,8 @@ public class DataManager {
} }
} }
public static TeleportationPoint getPlayerOfflinePosition(Player p, Connection connection) throws SQLException { public static TeleportationPoint getPlayerOfflinePosition(int playerID, Connection connection) throws SQLException {
Integer locationID = getPlayerInteger(p, "offline_location_id", connection); Integer locationID = getPlayerInteger(playerID, "offline_location_id", connection);
if (locationID != null) { if (locationID != null) {
return getTeleportationPoint(locationID, connection); return getTeleportationPoint(locationID, connection);
} else { } else {
@@ -754,9 +754,9 @@ public class DataManager {
} }
// Update a player's last position location on SQL // Update a player's last position location on SQL
public static void setPlayerOfflinePosition(Player p, TeleportationPoint point, Connection connection) throws SQLException { public static void setPlayerOfflinePosition(UUID uuid, TeleportationPoint point, Connection connection) throws SQLException {
deletePlayerOfflinePosition(p, connection); deletePlayerOfflinePosition(uuid, connection);
setTeleportationOfflinePositionData(p.getUniqueId(), point, connection); setTeleportationOfflinePositionData(uuid, point, connection);
} }
// Update a player's last position teleport point // 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 { public static void setPlayerTeleporting(Player p, boolean teleporting, Connection connection) throws SQLException {
setPlayerTeleportingData(p.getUniqueId(), teleporting, connection);
if (teleporting) { if (teleporting) {
HuskHomes.setTeleporting(p.getUniqueId()); HuskHomes.setTeleporting(p.getUniqueId());
} else { } else {
HuskHomes.setNotTeleporting(p.getUniqueId()); HuskHomes.setNotTeleporting(p.getUniqueId());
} }
setPlayerTeleportingData(p.getUniqueId(), teleporting, connection);
} }
public static void setPlayerIgnoringRequests(Player p, boolean ignoringRequests, Connection connection) throws SQLException { 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 { public static void deletePlayerOfflinePosition(UUID uuid, Connection connection) throws SQLException {
Integer offlineLocationId = getPlayerInteger(p, "offline_location_id", connection); Integer offlineLocationId = getPlayerInteger(getPlayerId(uuid, connection), "offline_location_id", connection);
if (offlineLocationId != null) { if (offlineLocationId != null) {
deleteTeleportationPoint(offlineLocationId, connection); deleteTeleportationPoint(offlineLocationId, connection);
clearPlayerOfflineData(p.getUniqueId(), connection); clearPlayerOfflineData(uuid, connection);
} }
} }
@@ -8,6 +8,7 @@ import me.william278.huskhomes2.data.DataManager;
import me.william278.huskhomes2.teleport.TeleportManager; import me.william278.huskhomes2.teleport.TeleportManager;
import me.william278.huskhomes2.teleport.points.TeleportationPoint; import me.william278.huskhomes2.teleport.points.TeleportationPoint;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@@ -17,6 +18,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
@@ -102,11 +104,19 @@ public class PlayerListener implements Listener {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent e) { public void onPlayerQuit(PlayerQuitEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
if (HuskHomes.isTeleporting(p.getUniqueId())) { final UUID uuid = p.getUniqueId();
HuskHomes.setNotTeleporting(p.getUniqueId()); if (HuskHomes.isTeleporting(uuid)) {
final Location logOutLocation = p.getLocation();
HuskHomes.setNotTeleporting(uuid);
Connection connection = HuskHomes.getConnection(); Connection connection = HuskHomes.getConnection();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { 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.");
}
}); });
} }
} }
@@ -85,6 +85,20 @@ public class TeleportManager {
new TimedTeleport(player, point, TimedTeleport.TargetType.POINT).begin(); 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 { public static void queueBackTeleport(Player player, Connection connection) throws SQLException {
try { try {
TeleportationPoint lastPosition = DataManager.getPlayerLastPosition(player, connection); TeleportationPoint lastPosition = DataManager.getPlayerLastPosition(player, connection);
+2 -1
View File
@@ -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)' 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_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)' 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)' 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)'
+7
View File
@@ -100,6 +100,10 @@ commands:
usage: /tpignore usage: /tpignore
permission: huskhomes.tpignore permission: huskhomes.tpignore
aliases: [tpaignore, ignoretprequests] aliases: [tpaignore, ignoretprequests]
tpoffline:
description: Teleport to where a player was last online
usage: /tpoffline <player>
permission: huskhomes.tpoffline
rtp: rtp:
description: Teleport randomly into the wild description: Teleport randomly into the wild
usage: /rtp usage: /rtp
@@ -227,6 +231,9 @@ permissions:
huskhomes.tpignore: huskhomes.tpignore:
description: Allows you to ingore teleport requests description: Allows you to ingore teleport requests
default: true default: true
huskhomes.tpoffline:
description: Allows you to teleport to where players logged off
default: false
huskhomes.back.death: huskhomes.back.death:
description: Allows you to return to where you died description: Allows you to return to where you died
default: true default: true