From c2bad4419b242c4daefe1f66073eb53d3892c75a Mon Sep 17 00:00:00 2001 From: gmcferrin Date: Fri, 11 Jan 2013 16:33:49 -0500 Subject: [PATCH] Make the purge task available as a command. --- .../nossr50/commands/mc/McpurgeCommand.java | 136 ++++++++++++++++++ .../java/com/gmail/nossr50/config/Config.java | 1 + src/main/java/com/gmail/nossr50/mcMMO.java | 4 + src/main/resources/config.yml | 2 + src/main/resources/plugin.yml | 5 +- 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java new file mode 100644 index 000000000..211986c75 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java @@ -0,0 +1,136 @@ +package com.gmail.nossr50.commands.mc; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.commands.CommandHelper; +import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.datatypes.SpoutHud; +import com.gmail.nossr50.spout.SpoutStuff; +import com.gmail.nossr50.util.Database; +import com.gmail.nossr50.util.Users; + +public class McpurgeCommand implements CommandExecutor{ + private Plugin plugin; + private Database database = mcMMO.getPlayerDatabase(); + private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); + private String databaseName = Config.getInstance().getMySQLDatabaseName(); + + public McpurgeCommand(Plugin plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mcremove")) { + return true; + } + + if (Config.getInstance().getUseMySQL()) { + purgePowerlessSQLFaster(); + purgeOldSQL(); + } + else { + //TODO: Make this work for Flatfile data. + } + + sender.sendMessage(ChatColor.GREEN + "The database was successfully purged!"); //TODO: Locale) + return true; + } + + private void purgePowerlessSQLFaster() { + plugin.getLogger().info("Purging powerless users..."); + String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; + HashMap> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " = 0 ORDER BY " + query + " DESC "); + + int purgedUsers = 0; + String userIdString = "("; + + for (int i = 1; i <= userslist.size(); i++) { + int userId = Integer.valueOf(userslist.get(i).get(1)); + + if (i == userslist.size()) { + userIdString = userIdString + userId + ")"; + } + else { + userIdString = userIdString + userId + ","; + } + } + + HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE id IN " + userIdString); + database.write("DELETE FROM " + databaseName + "." + tablePrefix + "users WHERE " + tablePrefix + "users.id IN " + userIdString); + + for (int i = 1; i <= usernames.size(); i++) { + String playerName = usernames.get(i).get(0); + + if (playerName == null || Bukkit.getOfflinePlayer(playerName).isOnline()) { + continue; + } + + profileCleanup(playerName); + purgedUsers++; + } + + plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); + } + + private void purgeOldSQL() { + plugin.getLogger().info("Purging old users..."); + long currentTime = System.currentTimeMillis(); + String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing"; + HashMap> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " > 0 ORDER BY " + query + " DESC "); + + int purgedUsers = 0; + + for (int i = 1; i <= userslist.size(); i++) { + int userId = Integer.valueOf(userslist.get(i).get(1)); + HashMap> username = database.read("SELECT user FROM " + tablePrefix + "users WHERE id = '" + userId + "'"); + String playerName = username.get(1).get(0); + + long lastLoginTime = database.getInt("SELECT lastlogin FROM " + tablePrefix + "users WHERE id = '" + userId + "'") * 1000L; + long loginDifference = currentTime - lastLoginTime; + + if (loginDifference > 2630000000L) { + database.write("DELETE FROM " + databaseName + "." + tablePrefix + "users WHERE " + tablePrefix + "users.id IN " + userId); + profileCleanup(playerName); + + purgedUsers++; + } + } + + plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); + } + + private void profileCleanup(String playerName) { + McMMOPlayer mcmmoPlayer = Users.getPlayer(playerName); + + if (mcmmoPlayer != null) { + Player player = mcmmoPlayer.getPlayer(); + SpoutHud spoutHud = mcmmoPlayer.getProfile().getSpoutHud(); + + if (spoutHud != null) { + spoutHud.removeWidgets(); + } + + Users.remove(playerName); + + if (player.isOnline()) { + Users.addUser(player); + + if (mcMMO.spoutEnabled) { + SpoutStuff.reloadSpoutPlayer(player); + } + } + } + } +} diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index cf36bef18..33d247460 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -88,6 +88,7 @@ public class Config extends ConfigLoader { public boolean getCommandSkillResetEnabled() { return config.getBoolean("Commands.skillreset.Enabled", true); } public boolean getCommandMmoeditEnabled() { return config.getBoolean("Commands.mmoedit.Enabled", true); } public boolean getCommandMCRemoveEnabled() { return config.getBoolean("Commands.mcremove.Enable", true); } + public boolean getCommandMCPurgeEnabled() { return config.getBoolean("Commands.mcpurge.Enable", true); } public boolean getCommandPTPEnabled() { return config.getBoolean("Commands.ptp.Enabled", true); } public boolean getCommandPartyEnabled() { return config.getBoolean("Commands.party.Enabled", true); } public boolean getCommandInspectEnabled() { return config.getBoolean("Commands.inspect.Enabled", true); } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 4527f2569..61869709f 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -26,6 +26,7 @@ import com.gmail.nossr50.commands.mc.McabilityCommand; import com.gmail.nossr50.commands.mc.MccCommand; import com.gmail.nossr50.commands.mc.McgodCommand; import com.gmail.nossr50.commands.mc.McmmoCommand; +import com.gmail.nossr50.commands.mc.McpurgeCommand; import com.gmail.nossr50.commands.mc.McrefreshCommand; import com.gmail.nossr50.commands.mc.McremoveCommand; import com.gmail.nossr50.commands.mc.MctopCommand; @@ -354,6 +355,9 @@ public class mcMMO extends JavaPlugin { Config configInstance = Config.getInstance(); //mc* commands + if (configInstance.getCommandMCPurgeEnabled()) { + getCommand("mcpurge").setExecutor(new McpurgeCommand(this)); + } if (configInstance.getCommandMCRemoveEnabled()) { getCommand("mcremove").setExecutor(new McremoveCommand(this)); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5a6a27445..9b17cd839 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -302,6 +302,8 @@ Commands: Enabled: true mcc: Enabled: true + mcpurge: + Enabled: true mcremove: Enabled: true mmoedit: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 34074cd7c..863ac3278 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -124,6 +124,9 @@ commands: a: aliases: [ac] description: Toggle Admin chat or send admin chat messages + mcpurge: + aliases: [] + description: Purge users with 0 powerlevel and/or who haven't connected in over 30 days from the server DB. permissions: mcmmo.*: default: false @@ -341,7 +344,7 @@ permissions: mcmmo.tools.mcgod: description: Allows access to mcgod command mcmmo.tools.mcremove: - decription: Allows access to mcremove command + decription: Allows access to mcremove and mcpurge command mcmmo.ability.*: description: Implies all mcmmo.ability permissions. children: