mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Start work on tpOffline, add database column & modify upgrade script for 2.7
Signed-off-by: BuildTools <[email protected]>
This commit is contained in:
1 parent
6ee2b2ce4d
commit
8b104a04cd
7 files changed
+101
-11
No files matched your search
@@ -68,6 +68,17 @@ public final class HuskHomes extends JavaPlugin {
|
||||
ignoringTeleportRequests.remove(uuid);
|
||||
}
|
||||
|
||||
// Ignoring teleport requests handler
|
||||
private static final HashSet<UUID> 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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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" +
|
||||
");",
|
||||
|
||||
@@ -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" +
|
||||
");",
|
||||
|
||||
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user