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 SetWarpCommand().register(getCommand("setwarp")).setTabCompleter(emptyTab);
new BackCommand().register(getCommand("back")).setTabCompleter(emptyTab);
new TpOfflineCommand().register(getCommand("tpoffline")).setTabCompleter(emptyTab);
// Update caches
PublicHomeCommand.updatePublicHomeTabCache();
@@ -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)));
}
}
@@ -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
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);
}
}
@@ -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.");
}
});
}
}
@@ -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);