Duplicate code is stupid.

This commit is contained in:
GJ 2013-01-14 00:47:47 -05:00
parent 268db10361
commit 8e2707d33e
5 changed files with 82 additions and 195 deletions

View File

@ -1,33 +1,16 @@
package com.gmail.nossr50.commands.mc; 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.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.config.Config; 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.Database;
import com.gmail.nossr50.util.Users;
public class McpurgeCommand implements CommandExecutor{ public class McpurgeCommand implements CommandExecutor{
private Plugin plugin;
private Database database = mcMMO.getPlayerDatabase(); private Database database = mcMMO.getPlayerDatabase();
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
public McpurgeCommand(Plugin plugin) {
this.plugin = plugin;
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -36,10 +19,10 @@ public class McpurgeCommand implements CommandExecutor{
} }
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
purgePowerlessSQL(); database.purgePowerlessSQL();
if (Config.getInstance().getOldUsersCutoff() != -1) { if (Config.getInstance().getOldUsersCutoff() != -1) {
purgeOldSQL(); database.purgeOldSQL();
} }
} }
else { else {
@ -49,69 +32,4 @@ public class McpurgeCommand implements CommandExecutor{
sender.sendMessage(ChatColor.GREEN + "The database was successfully purged!"); //TODO: Locale) sender.sendMessage(ChatColor.GREEN + "The database was successfully purged!"); //TODO: Locale)
return true; return true;
} }
private void purgePowerlessSQL() {
plugin.getLogger().info("Purging powerless users...");
HashMap<Integer, ArrayList<String>> 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<Integer, ArrayList<String>> 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);
}
}
}
}
} }

View File

@ -9,19 +9,15 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.config.Config; 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.locale.LocaleLoader;
import com.gmail.nossr50.spout.SpoutStuff;
import com.gmail.nossr50.util.Database; import com.gmail.nossr50.util.Database;
import com.gmail.nossr50.util.Users;
public class McremoveCommand implements CommandExecutor { public class McremoveCommand implements CommandExecutor {
private Database database = mcMMO.getPlayerDatabase();
private final String location; private final String location;
private final mcMMO plugin; private final mcMMO plugin;
@ -77,27 +73,7 @@ public class McremoveCommand implements CommandExecutor {
} }
} }
//Force PlayerProfile stuff to update database.profileCleanup(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);
}
}
}
return true; return true;
} }

View File

@ -203,10 +203,10 @@ public class mcMMO extends JavaPlugin {
//Old & Powerless User remover //Old & Powerless User remover
int purgeInterval = Config.getInstance().getPurgeInterval(); int purgeInterval = Config.getInstance().getPurgeInterval();
if (purgeInterval == 0) { 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) { 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(); registerCommands();
@ -363,7 +363,7 @@ public class mcMMO extends JavaPlugin {
//mc* commands //mc* commands
if (configInstance.getCommandMCPurgeEnabled()) { if (configInstance.getCommandMCPurgeEnabled()) {
getCommand("mcpurge").setExecutor(new McpurgeCommand(this)); getCommand("mcpurge").setExecutor(new McpurgeCommand());
} }
if (configInstance.getCommandMCRemoveEnabled()) { if (configInstance.getCommandMCRemoveEnabled()) {
getCommand("mcremove").setExecutor(new McremoveCommand(this)); getCommand("mcremove").setExecutor(new McremoveCommand(this));

View File

@ -1,105 +1,27 @@
package com.gmail.nossr50.runnables; 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.mcMMO;
import com.gmail.nossr50.config.Config; 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.Database;
import com.gmail.nossr50.util.Users;
public class UserPurgeTask implements Runnable { public class UserPurgeTask implements Runnable {
private Plugin plugin;
private Database database = mcMMO.getPlayerDatabase(); private Database database = mcMMO.getPlayerDatabase();
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
public UserPurgeTask(Plugin plugin) { public UserPurgeTask() {
this.plugin = plugin;
} }
@Override @Override
public void run() { public void run() {
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
purgePowerlessSQL(); database.purgePowerlessSQL();
if (Config.getInstance().getOldUsersCutoff() != -1) { if (Config.getInstance().getOldUsersCutoff() != -1) {
purgeOldSQL(); database.purgeOldSQL();
} }
} }
else { else {
//TODO: Make this work for Flatfile data. //TODO: Make this work for Flatfile data.
} }
} }
private void purgePowerlessSQL() {
plugin.getLogger().info("Purging powerless users...");
HashMap<Integer, ArrayList<String>> 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<Integer, ArrayList<String>> 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);
}
}
}
}
} }

View File

@ -9,10 +9,16 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Properties; import java.util.Properties;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.DatabaseUpdate; 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.runnables.SQLReconnect;
import com.gmail.nossr50.spout.SpoutStuff;
public class Database { public class Database {
@ -411,9 +417,74 @@ public class Database {
return rows; return rows;
} }
public void purgePowerlessSQL() {
plugin.getLogger().info("Purging powerless users...");
HashMap<Integer, ArrayList<String>> 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<Integer, ArrayList<String>> 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) { private static void printErrors(SQLException ex) {
System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState()); System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode()); 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);
}
}
}
}
} }