Skip to content

Commit 57a7872

Browse files
authored
Update DatabaseManager.java
1 parent 6f4032a commit 57a7872

1 file changed

Lines changed: 89 additions & 9 deletions

File tree

src/main/java/ru/allfire/coreprotectionassistant/database/DatabaseManager.java

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55
import lombok.Getter;
6+
import org.bukkit.Bukkit;
67
import org.bukkit.block.Block;
78
import org.bukkit.entity.Player;
89
import ru.allfire.coreprotectionassistant.CoreProtectionAssistant;
@@ -362,9 +363,6 @@ public CompletableFuture<Void> logGriefAction(Player player, Block block) {
362363

363364
// ========== REPORTS STATISTICS ==========
364365

365-
/**
366-
* Получить общее количество жалоб на игрока
367-
*/
368366
public CompletableFuture<Integer> getReportsAgainstPlayer(UUID uuid) {
369367
return CompletableFuture.supplyAsync(() -> {
370368
String sql = "SELECT COUNT(*) FROM cpa_reports WHERE target_uuid = ?";
@@ -386,9 +384,6 @@ public CompletableFuture<Integer> getReportsAgainstPlayer(UUID uuid) {
386384
});
387385
}
388386

389-
/**
390-
* Получить количество жалоб на игрока по категории
391-
*/
392387
public CompletableFuture<Integer> getReportsAgainstPlayerByCategory(UUID uuid, String category) {
393388
return CompletableFuture.supplyAsync(() -> {
394389
String sql = "SELECT COUNT(*) FROM cpa_reports WHERE target_uuid = ? AND category = ?";
@@ -411,9 +406,6 @@ public CompletableFuture<Integer> getReportsAgainstPlayerByCategory(UUID uuid, S
411406
});
412407
}
413408

414-
/**
415-
* Получить количество жалоб, отправленных игроком
416-
*/
417409
public CompletableFuture<Integer> getReportsFiledByPlayer(UUID uuid) {
418410
return CompletableFuture.supplyAsync(() -> {
419411
String sql = "SELECT COUNT(*) FROM cpa_reports WHERE reporter_uuid = ?";
@@ -435,6 +427,94 @@ public CompletableFuture<Integer> getReportsFiledByPlayer(UUID uuid) {
435427
});
436428
}
437429

430+
// ========== CLEANUP ==========
431+
432+
public void cleanupOldData() {
433+
var config = plugin.getConfigManager().getMainConfig();
434+
435+
cleanupTable("cpa_player_commands", config.getInt("cleanup.player_commands", 30));
436+
cleanupTable("cpa_staff_actions", config.getInt("cleanup.staff_actions", 90));
437+
cleanupTable("cpa_chat_violations", config.getInt("cleanup.chat_violations", 30));
438+
cleanupTable("cpa_apologies", config.getInt("cleanup.apologies", 30));
439+
cleanupTable("cpa_grief_actions", config.getInt("cleanup.grief_actions", 30));
440+
}
441+
442+
private void cleanupTable(String tableName, int days) {
443+
if (days <= 0) return;
444+
445+
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
446+
long cutoff = System.currentTimeMillis() - (days * 24L * 3600000);
447+
String sql = "DELETE FROM " + tableName + " WHERE timestamp < ?";
448+
449+
try (Connection conn = getConnection();
450+
PreparedStatement ps = conn.prepareStatement(sql)) {
451+
ps.setLong(1, cutoff);
452+
int deleted = ps.executeUpdate();
453+
454+
if (deleted > 0) {
455+
plugin.getLogger().info("Cleaned up " + deleted + " old records from " + tableName);
456+
}
457+
} catch (SQLException e) {
458+
plugin.getLogger().severe("Failed to cleanup " + tableName + ": " + e.getMessage());
459+
}
460+
});
461+
}
462+
463+
public void resetPlayerStats(UUID uuid, String type) {
464+
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
465+
try (Connection conn = getConnection()) {
466+
int totalDeleted = 0;
467+
468+
switch (type.toLowerCase()) {
469+
case "commands" -> {
470+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_player_commands WHERE player_uuid = ?", uuid);
471+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ?", uuid);
472+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_command_logs WHERE player_uuid = ?", uuid);
473+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_super_commands WHERE player_uuid = ?", uuid);
474+
}
475+
case "ban" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ? AND action = 'BAN'", uuid);
476+
case "mute" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ? AND action = 'MUTE'", uuid);
477+
case "kick" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ? AND action = 'KICK'", uuid);
478+
case "give" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ? AND action = 'GIVE'", uuid);
479+
case "gm" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ? AND action IN ('GAMEMODE', 'GM')", uuid);
480+
case "rating" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_abuse_scores WHERE player_uuid = ?", uuid);
481+
case "warn" -> totalDeleted = executeDelete(conn, "DELETE FROM cpa_warnings WHERE player_uuid = ?", uuid);
482+
case "free" -> {
483+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_warnings WHERE player_uuid = ?", uuid);
484+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_abuse_scores WHERE player_uuid = ?", uuid);
485+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_reports WHERE target_uuid = ?", uuid);
486+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_reports WHERE reporter_uuid = ?", uuid);
487+
}
488+
case "all" -> {
489+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_player_commands WHERE player_uuid = ?", uuid);
490+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_staff_actions WHERE staff_uuid = ?", uuid);
491+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_warnings WHERE player_uuid = ?", uuid);
492+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_abuse_scores WHERE player_uuid = ?", uuid);
493+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_reports WHERE target_uuid = ? OR reporter_uuid = ?", uuid, uuid);
494+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_chat_violations WHERE player_uuid = ?", uuid);
495+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_apologies WHERE player_uuid = ?", uuid);
496+
totalDeleted += executeDelete(conn, "DELETE FROM cpa_grief_actions WHERE player_uuid = ?", uuid);
497+
}
498+
}
499+
500+
if (totalDeleted > 0) {
501+
plugin.getLogger().info("Reset " + totalDeleted + " records for " + Bukkit.getOfflinePlayer(uuid).getName() + " (type: " + type + ")");
502+
}
503+
} catch (SQLException e) {
504+
plugin.getLogger().severe("Failed to reset player stats: " + e.getMessage());
505+
}
506+
});
507+
}
508+
509+
private int executeDelete(Connection conn, String sql, UUID... uuids) throws SQLException {
510+
try (PreparedStatement ps = conn.prepareStatement(sql)) {
511+
for (int i = 0; i < uuids.length; i++) {
512+
ps.setString(i + 1, uuids[i].toString());
513+
}
514+
return ps.executeUpdate();
515+
}
516+
}
517+
438518
// ========== QUERIES ==========
439519

440520
public CompletableFuture<Integer> getViolationCount(UUID uuid) {

0 commit comments

Comments
 (0)