mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-09-06 20:28:06 +02:00
Improve AutoPurge feature, #627
This commit is contained in:
parent
cb995a99a6
commit
f7408c3e14
@ -2,18 +2,15 @@ package fr.xephi.authme;
|
||||
|
||||
import fr.xephi.authme.hooks.PluginHooks;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PurgeSettings;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
*/
|
||||
@ -25,7 +22,7 @@ public class DataManager {
|
||||
/**
|
||||
* Constructor for DataManager.
|
||||
*
|
||||
* @param plugin The plugin instance
|
||||
* @param plugin The plugin instance
|
||||
* @param pluginHooks Plugin hooks instance
|
||||
*/
|
||||
public DataManager(AuthMe plugin, PluginHooks pluginHooks) {
|
||||
@ -33,126 +30,83 @@ public class DataManager {
|
||||
this.pluginHooks = pluginHooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getOfflinePlayer.
|
||||
*
|
||||
* @param name String
|
||||
*
|
||||
* @return OfflinePlayer
|
||||
*/
|
||||
private synchronized OfflinePlayer getOfflinePlayer(final String name) {
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
Future<OfflinePlayer> result = executor.submit(new Callable<OfflinePlayer>() {
|
||||
|
||||
public synchronized OfflinePlayer call() throws Exception {
|
||||
OfflinePlayer result = null;
|
||||
try {
|
||||
for (OfflinePlayer op : Bukkit.getOfflinePlayers())
|
||||
if (op.getName().equalsIgnoreCase(name)) {
|
||||
result = op;
|
||||
break;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
private List<OfflinePlayer> getOfflinePlayers(List<String> names) {
|
||||
List<OfflinePlayer> result = new ArrayList<>();
|
||||
for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
|
||||
for (String name : names) {
|
||||
if (name.equalsIgnoreCase(op.getName())) {
|
||||
result.add(op);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
try {
|
||||
return result.get();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method purgeAntiXray.
|
||||
*
|
||||
* @param cleared List of String
|
||||
*/
|
||||
public synchronized void purgeAntiXray(List<String> cleared) {
|
||||
public void purgeAntiXray(List<String> cleared) {
|
||||
int i = 0;
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
|
||||
if (player == null)
|
||||
continue;
|
||||
String playerName = player.getName();
|
||||
File playerFile = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData" + File.separator + "PlayerData" + File.separator + playerName);
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData"
|
||||
+ File.separator + "PlayerData");
|
||||
if (!dataFolder.exists() || !dataFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
for (String file : dataFolder.list()) {
|
||||
if (cleared.contains(file.toLowerCase())) {
|
||||
File playerFile = new File(dataFolder, file);
|
||||
if (playerFile.exists() && playerFile.delete()) {
|
||||
i++;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase: Removed " + i + " AntiXRayData Files");
|
||||
ConsoleLogger.info("AutoPurge: Removed " + i + " AntiXRayData Files");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method purgeLimitedCreative.
|
||||
*
|
||||
* @param cleared List of String
|
||||
*/
|
||||
public synchronized void purgeLimitedCreative(List<String> cleared) {
|
||||
int i = 0;
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
|
||||
if (player == null)
|
||||
continue;
|
||||
String playerName = player.getName();
|
||||
File playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + ".yml");
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
i++;
|
||||
}
|
||||
playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_creative.yml");
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
i++;
|
||||
}
|
||||
playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_adventure.yml");
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
i++;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase: Removed " + i + " LimitedCreative Survival, Creative and Adventure files");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method purgeDat.
|
||||
*
|
||||
* @param cleared List of String
|
||||
*/
|
||||
public synchronized void purgeDat(List<String> cleared) {
|
||||
int i = 0;
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
|
||||
if (player == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
File playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + player.getUniqueId() + ".dat");
|
||||
playerFile.delete();
|
||||
i++;
|
||||
} catch (Exception ignore) {
|
||||
File playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + player.getName() + ".dat");
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
i++;
|
||||
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative"
|
||||
+ File.separator + "inventories");
|
||||
for (String file : dataFolder.list()) {
|
||||
String name = file;
|
||||
int idx;
|
||||
idx = file.lastIndexOf("_creative.yml");
|
||||
if (idx != -1) {
|
||||
name = name.substring(0, idx);
|
||||
} else {
|
||||
idx = file.lastIndexOf("_adventure.yml");
|
||||
if (idx != -1) {
|
||||
name = name.substring(0, idx);
|
||||
} else {
|
||||
idx = file.lastIndexOf(".yml");
|
||||
if (idx != -1) {
|
||||
name = name.substring(0, idx);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
if (name.equals(file)) {
|
||||
continue;
|
||||
}
|
||||
if (cleared.contains(name.toLowerCase())) {
|
||||
File dataFile = new File(dataFolder, file);
|
||||
if (dataFile.exists() && dataFile.delete()) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase: Removed " + i + " .dat Files");
|
||||
ConsoleLogger.info("AutoPurge: Removed " + i + " LimitedCreative Survival, Creative and Adventure files");
|
||||
}
|
||||
|
||||
public synchronized void purgeDat(List<String> cleared) {
|
||||
int i = 0;
|
||||
File dataFolder = new File(plugin.getServer().getWorldContainer()
|
||||
+ File.separator + plugin.getSettings().getProperty(PurgeSettings.DEFAULT_WORLD)
|
||||
+ File.separator + "players");
|
||||
List<OfflinePlayer> offlinePlayers = getOfflinePlayers(cleared);
|
||||
for (OfflinePlayer player : offlinePlayers) {
|
||||
File playerFile = new File(dataFolder, Utils.getUUIDorName(player) + ".dat");
|
||||
if (playerFile.delete()) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurge: Removed " + i + " .dat Files");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -169,66 +123,34 @@ public class DataManager {
|
||||
}
|
||||
|
||||
final File userDataFolder = new File(essentialsDataFolder, "userdata");
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
File playerFile = new File(userDataFolder, plugin.getServer().getOfflinePlayer(name).getUniqueId() + ".yml");
|
||||
playerFile.delete();
|
||||
List<OfflinePlayer> offlinePlayers = getOfflinePlayers(cleared);
|
||||
for (OfflinePlayer player : offlinePlayers) {
|
||||
File playerFile = new File(userDataFolder, Utils.getUUIDorName(player) + ".yml");
|
||||
if (playerFile.exists() && playerFile.delete()) {
|
||||
i++;
|
||||
} catch (Exception e) {
|
||||
File playerFile = new File(userDataFolder, name + ".yml");
|
||||
if (playerFile.exists()) {
|
||||
playerFile.delete();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase: Removed " + i + " EssentialsFiles");
|
||||
|
||||
ConsoleLogger.info("AutoPurge: Removed " + i + " EssentialsFiles");
|
||||
}
|
||||
|
||||
// TODO: What is this method for? Is it correct?
|
||||
|
||||
/**
|
||||
* @param cleared Cleared players.
|
||||
*/
|
||||
// TODO: Make it work with OfflinePlayers group data.
|
||||
public synchronized void purgePermissions(List<String> cleared) {
|
||||
// Get the permissions manager, and make sure it's valid
|
||||
PermissionsManager permsMan = this.plugin.getPermissionsManager();
|
||||
PermissionsManager permsMan = plugin.getPermissionsManager();
|
||||
if (permsMan == null) {
|
||||
ConsoleLogger.showError("Unable to access permissions manager instance!");
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
permsMan.removeAllGroups(this.getOnlinePlayerLower(name.toLowerCase()));
|
||||
i++;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
permsMan.removeAllGroups(getOnlinePlayerLower(name));
|
||||
i++;
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase: Removed " + i + "permissions");
|
||||
|
||||
/*int i = 0;
|
||||
for (String name : cleared) {
|
||||
try {
|
||||
OfflinePlayer p = this.getOfflinePlayer(name);
|
||||
for (String group : permission.getPlayerGroups((Player) p)) {
|
||||
permission.playerRemoveGroup(null, p, group);
|
||||
}
|
||||
i++;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " Permissions");*/
|
||||
ConsoleLogger.info("AutoPurge: Removed permissions from " + i + " player(s).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getOnlinePlayerLower.
|
||||
*
|
||||
* @param name String
|
||||
*
|
||||
* @return Player
|
||||
*/
|
||||
private Player getOnlinePlayerLower(String name) {
|
||||
name = name.toLowerCase();
|
||||
for (Player player : Utils.getOnlinePlayers()) {
|
||||
|
@ -9,10 +9,10 @@ import fr.xephi.authme.events.AuthMeTeleportEvent;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@ -278,8 +278,16 @@ public final class Utils {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static String getUUIDorName(OfflinePlayer player) {
|
||||
String uuidOrName;
|
||||
try {
|
||||
uuidOrName = player.getUniqueId().toString();
|
||||
} catch (Exception ignore) {
|
||||
uuidOrName = player.getName();
|
||||
}
|
||||
return uuidOrName;
|
||||
}
|
||||
|
||||
public enum GroupType {
|
||||
UNREGISTERED,
|
||||
REGISTERED,
|
||||
|
Loading…
Reference in New Issue
Block a user