diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java index 2abc2c0bf..7c25d545a 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McpurgeCommand.java @@ -1,33 +1,16 @@ 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(); - - public McpurgeCommand(Plugin plugin) { - this.plugin = plugin; - } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -36,10 +19,10 @@ public class McpurgeCommand implements CommandExecutor{ } if (Config.getInstance().getUseMySQL()) { - purgePowerlessSQL(); + database.purgePowerlessSQL(); if (Config.getInstance().getOldUsersCutoff() != -1) { - purgeOldSQL(); + database.purgeOldSQL(); } } else { @@ -49,69 +32,4 @@ public class McpurgeCommand implements CommandExecutor{ sender.sendMessage(ChatColor.GREEN + "The database was successfully purged!"); //TODO: Locale) return true; } - - private void purgePowerlessSQL() { - plugin.getLogger().info("Purging powerless users..."); - HashMap> usernames = database.read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); - database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)"); - - int purgedUsers = 0; - 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(); - long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff(); - HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); - database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)"); - - int purgedUsers = 0; - for (int i = 1; i <= usernames.size(); i++) { - String playerName = usernames.get(i).get(0); - - if (playerName == null) { - continue; - } - - 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/commands/mc/McremoveCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java index 1b3911849..aa37f421c 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java @@ -9,19 +9,15 @@ 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 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.locale.LocaleLoader; -import com.gmail.nossr50.spout.SpoutStuff; import com.gmail.nossr50.util.Database; -import com.gmail.nossr50.util.Users; public class McremoveCommand implements CommandExecutor { + private Database database = mcMMO.getPlayerDatabase(); private final String location; private final mcMMO plugin; @@ -77,27 +73,7 @@ public class McremoveCommand implements CommandExecutor { } } - //Force PlayerProfile stuff to update - 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); - } - } - } + database.profileCleanup(playerName); return true; } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 5a020120a..16aeb6922 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -203,10 +203,10 @@ public class mcMMO extends JavaPlugin { //Old & Powerless User remover int purgeInterval = Config.getInstance().getPurgeInterval(); if (purgeInterval == 0) { - scheduler.scheduleSyncDelayedTask(this, new UserPurgeTask(this), 40); //Start 2 seconds after startup. + scheduler.scheduleSyncDelayedTask(this, new UserPurgeTask(), 40); //Start 2 seconds after startup. } else if (purgeInterval > 0) { - scheduler.scheduleSyncRepeatingTask(this, new UserPurgeTask(this), 0, purgeInterval * 60L * 60L * 20L); + scheduler.scheduleSyncRepeatingTask(this, new UserPurgeTask(), 0, purgeInterval * 60L * 60L * 20L); } registerCommands(); @@ -363,7 +363,7 @@ public class mcMMO extends JavaPlugin { //mc* commands if (configInstance.getCommandMCPurgeEnabled()) { - getCommand("mcpurge").setExecutor(new McpurgeCommand(this)); + getCommand("mcpurge").setExecutor(new McpurgeCommand()); } if (configInstance.getCommandMCRemoveEnabled()) { getCommand("mcremove").setExecutor(new McremoveCommand(this)); diff --git a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java index 9ae8f5b8d..960a07e5c 100644 --- a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java @@ -1,105 +1,27 @@ package com.gmail.nossr50.runnables; -import java.util.ArrayList; -import java.util.HashMap; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - import com.gmail.nossr50.mcMMO; 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 UserPurgeTask implements Runnable { - private Plugin plugin; private Database database = mcMMO.getPlayerDatabase(); - private String tablePrefix = Config.getInstance().getMySQLTablePrefix(); - public UserPurgeTask(Plugin plugin) { - this.plugin = plugin; + public UserPurgeTask() { + } @Override public void run() { if (Config.getInstance().getUseMySQL()) { - purgePowerlessSQL(); + database.purgePowerlessSQL(); if (Config.getInstance().getOldUsersCutoff() != -1) { - purgeOldSQL(); + database.purgeOldSQL(); } } else { //TODO: Make this work for Flatfile data. } } - - private void purgePowerlessSQL() { - plugin.getLogger().info("Purging powerless users..."); - HashMap> usernames = database.read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); - database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)"); - - int purgedUsers = 0; - 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(); - long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff(); - HashMap> usernames = database.read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); - database.write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)"); - - int purgedUsers = 0; - for (int i = 1; i <= usernames.size(); i++) { - String playerName = usernames.get(i).get(0); - - if (playerName == null) { - continue; - } - - 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/util/Database.java b/src/main/java/com/gmail/nossr50/util/Database.java index 985a73869..71d4f4b14 100644 --- a/src/main/java/com/gmail/nossr50/util/Database.java +++ b/src/main/java/com/gmail/nossr50/util/Database.java @@ -9,10 +9,16 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Properties; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.DatabaseUpdate; +import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.datatypes.SpoutHud; import com.gmail.nossr50.runnables.SQLReconnect; +import com.gmail.nossr50.spout.SpoutStuff; public class Database { @@ -411,9 +417,74 @@ public class Database { return rows; } + public void purgePowerlessSQL() { + plugin.getLogger().info("Purging powerless users..."); + HashMap> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0"); + write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT u.id FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0) AS p)"); + + int purgedUsers = 0; + 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."); + } + + public void purgeOldSQL() { + plugin.getLogger().info("Purging old users..."); + long currentTime = System.currentTimeMillis(); + long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff(); + HashMap> usernames = read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")"); + write("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.id IN (SELECT * FROM (SELECT id FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")) AS p)"); + + int purgedUsers = 0; + for (int i = 1; i <= usernames.size(); i++) { + String playerName = usernames.get(i).get(0); + + if (playerName == null) { + continue; + } + + profileCleanup(playerName); + purgedUsers++; + } + + plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); + } + private static void printErrors(SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } + + public 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); + } + } + } + } } \ No newline at end of file