Make the purge progress run more balanced (Fixes #696)

This commit is contained in:
games647 2016-05-09 13:09:40 +02:00
parent b728b297b8
commit 0bd6ac5cc8
10 changed files with 255 additions and 115 deletions

View File

@ -44,6 +44,11 @@ import fr.xephi.authme.settings.SettingsMigrationService;
import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.DatabaseSettings; import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.EmailSettings;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_PASSWORD;
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.PurgeSettings; import fr.xephi.authme.settings.properties.PurgeSettings;
@ -51,6 +56,7 @@ import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.settings.properties.SettingsFieldRetriever; import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
import fr.xephi.authme.settings.propertymap.PropertyMap; import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.task.PurgeTask;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.FileUtils; import fr.xephi.authme.util.FileUtils;
@ -58,6 +64,18 @@ import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.MigrationService; import fr.xephi.authme.util.MigrationService;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -70,20 +88,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_PASSWORD;
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
/** /**
* The AuthMe main class. * The AuthMe main class.
*/ */
@ -688,33 +692,21 @@ public class AuthMe extends JavaPlugin {
if (!newSettings.getProperty(PurgeSettings.USE_AUTO_PURGE) || autoPurging) { if (!newSettings.getProperty(PurgeSettings.USE_AUTO_PURGE) || autoPurging) {
return; return;
} }
autoPurging = true; autoPurging = true;
getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
@Override
public void run() {
ConsoleLogger.info("AutoPurging the Database..."); ConsoleLogger.info("AutoPurging the Database...");
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -newSettings.getProperty(PurgeSettings.DAYS_BEFORE_REMOVE_PLAYER)); calendar.add(Calendar.DATE, -newSettings.getProperty(PurgeSettings.DAYS_BEFORE_REMOVE_PLAYER));
long until = calendar.getTimeInMillis(); long until = calendar.getTimeInMillis();
List<String> cleared = database.autoPurgeDatabase(until); Set<String> cleared = database.autoPurgeDatabase(until);
if (CollectionUtils.isEmpty(cleared)) { if (CollectionUtils.isEmpty(cleared)) {
return; return;
} }
ConsoleLogger.info("AutoPurging the Database: " + cleared.size() + " accounts removed!"); ConsoleLogger.info("AutoPurging the Database: " + cleared.size() + " accounts removed!");
if (newSettings.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES) && pluginHooks.isEssentialsAvailable()) ConsoleLogger.info("Purging user accounts...");
dataManager.purgeEssentials(cleared); new PurgeTask(plugin, newSettings, Bukkit.getConsoleSender(), cleared).runTaskTimer(plugin, 0, 1);
if (newSettings.getProperty(PurgeSettings.REMOVE_PLAYER_DAT))
dataManager.purgeDat(cleared);
if (newSettings.getProperty(PurgeSettings.REMOVE_LIMITED_CREATIVE_INVENTORIES))
dataManager.purgeLimitedCreative(cleared);
if (newSettings.getProperty(PurgeSettings.REMOVE_ANTI_XRAY_FILE))
dataManager.purgeAntiXray(cleared);
if (newSettings.getProperty(PurgeSettings.REMOVE_PERMISSIONS))
dataManager.purgePermissions(cleared);
ConsoleLogger.info("AutoPurge Finished!");
autoPurging = false;
}
});
} }
// Return the spawn location of a player // Return the spawn location of a player
@ -825,4 +817,7 @@ public class AuthMe extends JavaPlugin {
return pluginHooks; return pluginHooks;
} }
public void notifyAutoPurgeEnd() {
this.autoPurging = false;
}
} }

View File

@ -6,16 +6,14 @@ import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.PurgeSettings; import fr.xephi.authme.settings.properties.PurgeSettings;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Server; import org.bukkit.Server;
import javax.inject.Inject; import javax.inject.Inject;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static fr.xephi.authme.util.StringUtils.makePath; import static fr.xephi.authme.util.StringUtils.makePath;
import java.util.Set;
/** /**
*/ */
@ -34,25 +32,14 @@ public class DataManager {
DataManager() { } DataManager() { }
private List<OfflinePlayer> getOfflinePlayers(List<String> names) { public void purgeAntiXray(Set<String> cleared) {
List<OfflinePlayer> result = new ArrayList<>();
for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
for (String name : names) {
if (name.equalsIgnoreCase(op.getName())) {
result.add(op);
}
}
}
return result;
}
public void purgeAntiXray(List<String> cleared) {
int i = 0; int i = 0;
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData" File dataFolder = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData"
+ File.separator + "PlayerData"); + File.separator + "PlayerData");
if (!dataFolder.exists() || !dataFolder.isDirectory()) { if (!dataFolder.exists() || !dataFolder.isDirectory()) {
return; return;
} }
for (String file : dataFolder.list()) { for (String file : dataFolder.list()) {
if (cleared.contains(file.toLowerCase())) { if (cleared.contains(file.toLowerCase())) {
File playerFile = new File(dataFolder, file); File playerFile = new File(dataFolder, file);
@ -61,10 +48,11 @@ public class DataManager {
} }
} }
} }
ConsoleLogger.info("AutoPurge: Removed " + i + " AntiXRayData Files"); ConsoleLogger.info("AutoPurge: Removed " + i + " AntiXRayData Files");
} }
public synchronized void purgeLimitedCreative(List<String> cleared) { public synchronized void purgeLimitedCreative(Set<String> cleared) {
int i = 0; int i = 0;
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" File dataFolder = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative"
+ File.separator + "inventories"); + File.separator + "inventories");
@ -101,17 +89,18 @@ public class DataManager {
ConsoleLogger.info("AutoPurge: Removed " + i + " LimitedCreative Survival, Creative and Adventure files"); ConsoleLogger.info("AutoPurge: Removed " + i + " LimitedCreative Survival, Creative and Adventure files");
} }
public synchronized void purgeDat(List<String> cleared) { public synchronized void purgeDat(Set<OfflinePlayer> cleared) {
int i = 0; int i = 0;
File dataFolder = new File(server.getWorldContainer(), File dataFolder = new File(server.getWorldContainer()
makePath(settings.getProperty(PurgeSettings.DEFAULT_WORLD), "players")); , makePath(settings.getProperty(PurgeSettings.DEFAULT_WORLD), "players"));
List<OfflinePlayer> offlinePlayers = getOfflinePlayers(cleared);
for (OfflinePlayer player : offlinePlayers) { for (OfflinePlayer offlinePlayer : cleared) {
File playerFile = new File(dataFolder, Utils.getUUIDorName(player) + ".dat"); File playerFile = new File(dataFolder, Utils.getUUIDorName(offlinePlayer) + ".dat");
if (playerFile.delete()) { if (playerFile.delete()) {
i++; i++;
} }
} }
ConsoleLogger.info("AutoPurge: Removed " + i + " .dat Files"); ConsoleLogger.info("AutoPurge: Removed " + i + " .dat Files");
} }
@ -120,7 +109,7 @@ public class DataManager {
* *
* @param cleared List of String * @param cleared List of String
*/ */
public void purgeEssentials(List<String> cleared) { public void purgeEssentials(Set<OfflinePlayer> cleared) {
int i = 0; int i = 0;
File essentialsDataFolder = pluginHooks.getEssentialsDataFolder(); File essentialsDataFolder = pluginHooks.getEssentialsDataFolder();
if (essentialsDataFolder == null) { if (essentialsDataFolder == null) {
@ -132,9 +121,9 @@ public class DataManager {
if (!userDataFolder.exists() || !userDataFolder.isDirectory()) { if (!userDataFolder.exists() || !userDataFolder.isDirectory()) {
return; return;
} }
List<OfflinePlayer> offlinePlayers = getOfflinePlayers(cleared);
for (OfflinePlayer player : offlinePlayers) { for (OfflinePlayer offlinePlayer : cleared) {
File playerFile = new File(userDataFolder, Utils.getUUIDorName(player) + ".yml"); File playerFile = new File(userDataFolder, Utils.getUUIDorName(offlinePlayer) + ".yml");
if (playerFile.exists() && playerFile.delete()) { if (playerFile.exists() && playerFile.delete()) {
i++; i++;
} }
@ -145,10 +134,12 @@ public class DataManager {
// TODO: What is this method for? Is it correct? // TODO: What is this method for? Is it correct?
// TODO: Make it work with OfflinePlayers group data. // TODO: Make it work with OfflinePlayers group data.
public synchronized void purgePermissions(List<String> cleared) { public synchronized void purgePermissions(Set<OfflinePlayer> cleared) {
for (String name : cleared) { for (OfflinePlayer offlinePlayer : cleared) {
String name = offlinePlayer.getName();
permissionsManager.removeAllGroups(bukkitService.getPlayerExact(name)); permissionsManager.removeAllGroups(bukkitService.getPlayerExact(name));
} }
ConsoleLogger.info("AutoPurge: Removed permissions from " + cleared.size() + " player(s)."); ConsoleLogger.info("AutoPurge: Removed permissions from " + cleared.size() + " player(s).");
} }
} }

View File

@ -5,14 +5,18 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.properties.PurgeSettings; import fr.xephi.authme.task.PurgeTask;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.ArrayList; import org.bukkit.ChatColor;
import java.util.List;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
/** /**
* Command for purging data of banned players. Depending on the settings * Command for purging data of banned players. Depending on the settings
@ -35,24 +39,17 @@ public class PurgeBannedPlayersCommand implements ExecutableCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the list of banned players // Get the list of banned players
List<String> bannedPlayers = new ArrayList<>(); Set<String> bannedPlayers = new HashSet<>();
for (OfflinePlayer offlinePlayer : bukkitService.getBannedPlayers()) { for (OfflinePlayer offlinePlayer : bukkitService.getBannedPlayers()) {
bannedPlayers.add(offlinePlayer.getName().toLowerCase()); bannedPlayers.add(offlinePlayer.getName().toLowerCase());
} }
//todo: note this should may run async because it may executes a SQL-Query
// Purge the banned players // Purge the banned players
dataSource.purgeBanned(bannedPlayers); dataSource.purgeBanned(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES)
&& pluginHooks.isEssentialsAvailable())
plugin.dataManager.purgeEssentials(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_PLAYER_DAT))
plugin.dataManager.purgeDat(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_LIMITED_CREATIVE_INVENTORIES))
plugin.dataManager.purgeLimitedCreative(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_ANTI_XRAY_FILE))
plugin.dataManager.purgeAntiXray(bannedPlayers);
// Show a status message // Show a status message
sender.sendMessage("[AuthMe] Database has been purged correctly"); sender.sendMessage(ChatColor.GOLD + "Purging user accounts...");
new PurgeTask(plugin, plugin.getSettings(), sender, bannedPlayers).runTaskTimer(plugin, 0, 1);
} }
} }

View File

@ -5,13 +5,15 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.properties.PurgeSettings; import fr.xephi.authme.task.PurgeTask;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Command for purging the data of players which have not been since for a given number * Command for purging the data of players which have not been since for a given number
@ -27,6 +29,9 @@ public class PurgeCommand implements ExecutableCommand {
@Inject @Inject
private PluginHooks pluginHooks; private PluginHooks pluginHooks;
@Inject
private BukkitService bukkitService;
@Inject @Inject
private AuthMe plugin; private AuthMe plugin;
@ -56,24 +61,13 @@ public class PurgeCommand implements ExecutableCommand {
calendar.add(Calendar.DATE, -days); calendar.add(Calendar.DATE, -days);
long until = calendar.getTimeInMillis(); long until = calendar.getTimeInMillis();
//todo: note this should may run async because it may executes a SQL-Query
// Purge the data, get the purged values // Purge the data, get the purged values
List<String> purged = dataSource.autoPurgeDatabase(until); Set<String> purged = dataSource.autoPurgeDatabase(until);
// Show a status message // Show a status message
sender.sendMessage(ChatColor.GOLD + "Deleted " + purged.size() + " user accounts"); sender.sendMessage(ChatColor.GOLD + "Deleted " + purged.size() + " user accounts");
sender.sendMessage(ChatColor.GOLD + "Purging user accounts...");
// Purge other data new PurgeTask(plugin, plugin.getSettings(), sender, purged).runTaskTimer(plugin, 0, 1);
if (commandService.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES)
&& pluginHooks.isEssentialsAvailable())
plugin.dataManager.purgeEssentials(purged);
if (commandService.getProperty(PurgeSettings.REMOVE_PLAYER_DAT))
plugin.dataManager.purgeDat(purged);
if (commandService.getProperty(PurgeSettings.REMOVE_LIMITED_CREATIVE_INVENTORIES))
plugin.dataManager.purgeLimitedCreative(purged);
if (commandService.getProperty(PurgeSettings.REMOVE_ANTI_XRAY_FILE))
plugin.dataManager.purgeAntiXray(purged);
// Show a status message
sender.sendMessage(ChatColor.GREEN + "[AuthMe] Database has been purged correctly");
} }
} }

View File

@ -15,6 +15,7 @@ import fr.xephi.authme.security.crypts.HashedPassword;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -137,11 +138,12 @@ public class CacheDataSource implements DataSource {
} }
@Override @Override
public List<String> autoPurgeDatabase(long until) { public Set<String> autoPurgeDatabase(long until) {
List<String> cleared = source.autoPurgeDatabase(until); Set<String> cleared = source.autoPurgeDatabase(until);
for (String name : cleared) { for (String name : cleared) {
cachedAuths.invalidate(name); cachedAuths.invalidate(name);
} }
return cleared; return cleared;
} }
@ -187,7 +189,7 @@ public class CacheDataSource implements DataSource {
} }
@Override @Override
public synchronized void purgeBanned(final List<String> banned) { public synchronized void purgeBanned(final Set<String> banned) {
source.purgeBanned(banned); source.purgeBanned(banned);
cachedAuths.invalidateAll(banned); cachedAuths.invalidateAll(banned);
} }

View File

@ -4,6 +4,7 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Interface for manipulating {@link PlayerAuth} objects from a data source. * Interface for manipulating {@link PlayerAuth} objects from a data source.
@ -74,7 +75,7 @@ public interface DataSource {
* @param until The minimum last login * @param until The minimum last login
* @return The account names that have been removed * @return The account names that have been removed
*/ */
List<String> autoPurgeDatabase(long until); Set<String> autoPurgeDatabase(long until);
/** /**
* Remove a user record from the database. * Remove a user record from the database.
@ -126,7 +127,7 @@ public interface DataSource {
* *
* @param banned the list of players to delete * @param banned the list of players to delete
*/ */
void purgeBanned(List<String> banned); void purgeBanned(Set<String> banned);
/** /**
* Return the data source type. * Return the data source type.

View File

@ -17,7 +17,9 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Deprecated flat file datasource. The only method guaranteed to work is {@link FlatFile#getAllAuths()} * Deprecated flat file datasource. The only method guaranteed to work is {@link FlatFile#getAllAuths()}
@ -227,11 +229,11 @@ public class FlatFile implements DataSource {
} }
@Override @Override
public List<String> autoPurgeDatabase(long until) { public Set<String> autoPurgeDatabase(long until) {
BufferedReader br = null; BufferedReader br = null;
BufferedWriter bw = null; BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>(); ArrayList<String> lines = new ArrayList<>();
List<String> cleared = new ArrayList<>(); Set<String> cleared = new HashSet<>();
try { try {
br = new BufferedReader(new FileReader(source)); br = new BufferedReader(new FileReader(source));
String line; String line;
@ -256,6 +258,7 @@ public class FlatFile implements DataSource {
silentClose(br); silentClose(br);
silentClose(bw); silentClose(bw);
} }
return cleared; return cleared;
} }
@ -414,7 +417,7 @@ public class FlatFile implements DataSource {
} }
@Override @Override
public void purgeBanned(List<String> banned) { public void purgeBanned(Set<String> banned) {
BufferedReader br = null; BufferedReader br = null;
BufferedWriter bw = null; BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>(); ArrayList<String> lines = new ArrayList<>();

View File

@ -24,7 +24,9 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
*/ */
@ -610,8 +612,8 @@ public class MySQL implements DataSource {
} }
@Override @Override
public synchronized List<String> autoPurgeDatabase(long until) { public synchronized Set<String> autoPurgeDatabase(long until) {
List<String> list = new ArrayList<>(); Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;"; String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;"; String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
try (Connection con = getConnection(); try (Connection con = getConnection();
@ -628,6 +630,7 @@ public class MySQL implements DataSource {
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} }
return list; return list;
} }
@ -738,7 +741,7 @@ public class MySQL implements DataSource {
} }
@Override @Override
public synchronized void purgeBanned(List<String> banned) { public synchronized void purgeBanned(Set<String> banned) {
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
for (String name : banned) { for (String name : banned) {

View File

@ -16,7 +16,9 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
*/ */
@ -285,8 +287,8 @@ public class SQLite implements DataSource {
} }
@Override @Override
public List<String> autoPurgeDatabase(long until) { public Set<String> autoPurgeDatabase(long until) {
List<String> list = new ArrayList<>(); Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;"; String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;"; String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
try (PreparedStatement selectPst = con.prepareStatement(select); try (PreparedStatement selectPst = con.prepareStatement(select);
@ -302,6 +304,7 @@ public class SQLite implements DataSource {
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} }
return list; return list;
} }
@ -425,7 +428,7 @@ public class SQLite implements DataSource {
} }
@Override @Override
public void purgeBanned(List<String> banned) { public void purgeBanned(Set<String> banned) {
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) { try (PreparedStatement pst = con.prepareStatement(sql)) {
for (String name : banned) { for (String name : banned) {

View File

@ -0,0 +1,151 @@
package fr.xephi.authme.task;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.PurgeSettings;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class PurgeTask extends BukkitRunnable {
//how many players we should check for each tick
private static final int INTERVALL_CHECK = 5;
private final AuthMe plugin;
private final NewSetting newSetting;
private final UUID sender;
private final Set<String> toPurge;
private final boolean autoPurging;
private final int totalPurgeCount;
private int currentPage = 0;
public PurgeTask(AuthMe plugin, NewSetting newSetting, CommandSender sender, Set<String> purged) {
this(plugin, newSetting, sender, purged, false);
}
public PurgeTask(AuthMe plugin, NewSetting newSetting, CommandSender sender, Set<String> purged
, boolean autoPurging) {
this.plugin = plugin;
this.newSetting = newSetting;
if (sender instanceof Player) {
this.sender = ((Player) sender).getUniqueId();
} else {
this.sender = null;
}
this.toPurge = purged;
this.totalPurgeCount = purged.size();
this.autoPurging = autoPurging;
//this is commented out because I assume all players in the database already have an lowercase name
// toPurge = new HashSet<>(purged.size());
//make a new list with lowercase names to make the username test based on a hash
// for (String username : purged) {
// toPurge.add(username.toLowerCase());
// }
}
@Override
public void run() {
if (toPurge.isEmpty()) {
//everything was removed
finish();
return;
}
Set<OfflinePlayer> playerPortion = new HashSet<OfflinePlayer>(INTERVALL_CHECK);
Set<String> namePortion = new HashSet<String>(INTERVALL_CHECK);
OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers();
for (int i = 0; i < INTERVALL_CHECK; i++) {
int nextPosition = (currentPage * INTERVALL_CHECK) + i;
if (offlinePlayers.length >= nextPosition) {
//no more offline players on this page
break;
}
OfflinePlayer offlinePlayer = offlinePlayers[nextPosition];
String offlineName = offlinePlayer.getName();
//remove to speed up later lookups
if (toPurge.remove(offlineName.toLowerCase())) {
playerPortion.add(offlinePlayer);
namePortion.add(offlinePlayer.getName());
}
}
if (!toPurge.isEmpty() && playerPortion.isEmpty()) {
ConsoleLogger.info("Finished lookup up offlinePlayers. Begin looking purging player names only");
//we went through all offlineplayers but there are still names remaining
namePortion.addAll(toPurge);
toPurge.clear();
}
currentPage++;
purgeData(playerPortion, namePortion);
if (currentPage % 20 == 0) {
int completed = totalPurgeCount - toPurge.size();
sendMessage("[AuthMe] Purge progress " + completed + '/' + totalPurgeCount);
}
}
private void purgeData(Set<OfflinePlayer> playerPortion, Set<String> namePortion) {
// Purge other data
if (newSetting.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES)
&& plugin.getPluginHooks().isEssentialsAvailable()) {
plugin.dataManager.purgeEssentials(playerPortion);
}
if (newSetting.getProperty(PurgeSettings.REMOVE_PLAYER_DAT)) {
plugin.dataManager.purgeDat(playerPortion);
}
if (newSetting.getProperty(PurgeSettings.REMOVE_LIMITED_CREATIVE_INVENTORIES)) {
plugin.dataManager.purgeLimitedCreative(namePortion);
}
if (newSetting.getProperty(PurgeSettings.REMOVE_ANTI_XRAY_FILE)) {
plugin.dataManager.purgeAntiXray(namePortion);
}
if (newSetting.getProperty(PurgeSettings.REMOVE_PERMISSIONS)) {
plugin.dataManager.purgePermissions(playerPortion);
}
}
private void finish() {
cancel();
// Show a status message
sendMessage(ChatColor.GREEN + "[AuthMe] Database has been purged correctly");
ConsoleLogger.info("AutoPurge Finished!");
if (autoPurging) {
plugin.notifyAutoPurgeEnd();
}
}
private void sendMessage(String message) {
if (sender == null) {
Bukkit.getConsoleSender().sendMessage(message);
} else {
Player player = Bukkit.getPlayer(sender);
if (player != null) {
player.sendMessage(message);
}
}
}
}