From d25a314d3a9a4e449ccf47f2860014a8ac217090 Mon Sep 17 00:00:00 2001 From: gmcferrin Date: Thu, 10 Jan 2013 21:41:35 -0500 Subject: [PATCH] Fix bug with UserPurgeTask... --- .../nossr50/runnables/UserPurgeTask.java | 48 ++++++++++++++++--- .../java/com/gmail/nossr50/util/Database.java | 2 +- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java index 46ca3918f..b2b360305 100644 --- a/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/UserPurgeTask.java @@ -4,11 +4,16 @@ 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; @@ -34,7 +39,7 @@ public class UserPurgeTask implements Runnable { private void purgePowerlessSQL() { System.out.println("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 "); + HashMap> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " = 0 ORDER BY " + query + " DESC "); int purgedUsers = 0; @@ -42,16 +47,17 @@ public class UserPurgeTask implements Runnable { System.out.println("Checking user " + i + "/" + userslist.size()); 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); - if (username != null && Bukkit.getOfflinePlayer(username.get(1).get(0)).isOnline()) { + if (Bukkit.getOfflinePlayer(playerName).isOnline()) { continue; } - deleteFromSQL(userId); + deleteFromSQL(userId, playerName); purgedUsers++; } - plugin.getLogger().info("Purged " + purgedUsers + "users from the database."); + plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); } private void purgeOldSQL() { @@ -65,19 +71,22 @@ public class UserPurgeTask implements Runnable { for (int i = 1; i <= userslist.size(); i++) { System.out.println("Checking user " + i + "/" + userslist.size()); 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) { - deleteFromSQL(userId); + deleteFromSQL(userId, playerName); purgedUsers++; } } - plugin.getLogger().info("Purged " + purgedUsers + "users from the database."); + plugin.getLogger().info("Purged " + purgedUsers + " users from the database."); } - private void deleteFromSQL(int userId) { + private void deleteFromSQL(int userId, String playerName) { System.out.println("Deleting user " + userId); database.write("DELETE FROM " + databaseName + "." @@ -104,6 +113,31 @@ public class UserPurgeTask implements Runnable { + tablePrefix + "experience WHERE " + tablePrefix + "experience.user_id=" + userId); + profileCleanup(playerName); + System.out.println("User " + userId + " was successfully removed!"); } + + 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 c918fd27b..7ebc2e799 100644 --- a/src/main/java/com/gmail/nossr50/util/Database.java +++ b/src/main/java/com/gmail/nossr50/util/Database.java @@ -24,7 +24,7 @@ public class Database { private static mcMMO plugin = null; // Scale waiting time by this much per failed attempt - private static final double SCALING_FACTOR = 10; + private static final double SCALING_FACTOR = 40; // Minimum wait in nanoseconds (default 500ms) private static final long MIN_WAIT = 500L*1000000L;