Updated /mcpurge for Bukkit CommandAPI, also made it work with Flatfile

This commit is contained in:
GJ
2013-02-05 15:29:57 -05:00
parent 419937f62e
commit 3f6c07ba6a
8 changed files with 193 additions and 64 deletions

View File

@ -2,15 +2,22 @@ package com.gmail.nossr50.database;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Bukkit;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Users;
public final class Leaderboard {
private static HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
@ -252,4 +259,143 @@ public final class Leaderboard {
return (o2.statVal - o1.statVal);
}
}
public static boolean removeFlatFileUser(String playerName) {
boolean worked = false;
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
try {
FileReader file = new FileReader(usersFilePath);
in = new BufferedReader(file);
StringBuilder writer = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
/* Write out the same file but when we get to the player we want to remove, we skip his line. */
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
writer.append(line).append("\r\n");
}
else {
System.out.println("User found, removing...");
worked = true;
continue; //Skip the player
}
}
out = new FileWriter(usersFilePath); //Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
return worked;
}
public static void purgePowerlessFlatfile() {
mcMMO.p.getLogger().info("Purging powerless users...");
int purgedUsers = 0;
for (McMMOPlayer player : Users.getPlayers().values()) {
String playerName = player.getPlayer().getName();
if (playerName == null || Bukkit.getOfflinePlayer(playerName).isOnline()) {
continue;
}
if (getPlayerRank(playerName)[1] == 0 && removeFlatFileUser(playerName)) {
purgedUsers++;
}
}
mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
}
public static void purgeOldFlatfile() {
mcMMO.p.getLogger().info("Purging old users...");
int purgedUsers = removeOldFlatfileUsers();
mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
}
private static int removeOldFlatfileUsers() {
int removedPlayers = 0;
long currentTime = System.currentTimeMillis();
long purgeTime = 2630000000L * Config.getInstance().getOldUsersCutoff();
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
try {
FileReader file = new FileReader(usersFilePath);
in = new BufferedReader(file);
StringBuilder writer = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
/* Write out the same file but when we get to the player we want to remove, we skip his line. */
if (currentTime - (Misc.getLong(line.split(":")[37]) * 1000) <= purgeTime) {
writer.append(line).append("\r\n");
}
else {
System.out.println("User found, removing...");
removedPlayers++;
continue; //Skip the player
}
}
out = new FileWriter(usersFilePath); //Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
return removedPlayers;
}
}

View File

@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
import com.gmail.nossr50.locale.LocaleLoader;
public class McpurgeCommand implements CommandExecutor{
@ -21,7 +22,11 @@ public class McpurgeCommand implements CommandExecutor{
}
}
else {
//TODO: Make this work for Flatfile data.
Leaderboard.purgePowerlessFlatfile();
if (Config.getInstance().getOldUsersCutoff() != -1) {
Leaderboard.purgeOldFlatfile();
}
}
sender.sendMessage(LocaleLoader.getString("Commands.mcpurge.Success"));

View File

@ -1,18 +1,13 @@
package com.gmail.nossr50.database.commands;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
import com.gmail.nossr50.locale.LocaleLoader;
public class McremoveCommand implements CommandExecutor {
@ -51,7 +46,7 @@ public class McremoveCommand implements CommandExecutor {
}
}
else {
if (removeFlatFileUser(playerName)) {
if (Leaderboard.removeFlatFileUser(playerName)) {
sender.sendMessage(success);
}
else {
@ -63,59 +58,4 @@ public class McremoveCommand implements CommandExecutor {
return true;
}
private boolean removeFlatFileUser(String playerName) {
boolean worked = false;
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
try {
FileReader file = new FileReader(usersFilePath);
in = new BufferedReader(file);
StringBuilder writer = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
/* Write out the same file but when we get to the player we want to remove, we skip his line. */
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
writer.append(line).append("\r\n");
}
else {
System.out.println("User found, removing...");
worked = true;
continue; //Skip the player
}
}
out = new FileWriter(usersFilePath); //Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
return worked;
}
}

View File

@ -2,6 +2,7 @@ package com.gmail.nossr50.database.runnables;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
public class UserPurgeTask implements Runnable {
@Override
@ -14,7 +15,11 @@ public class UserPurgeTask implements Runnable {
}
}
else {
//TODO: Make this work for Flatfile data.
Leaderboard.purgePowerlessFlatfile();
if (Config.getInstance().getOldUsersCutoff() != -1) {
Leaderboard.purgeOldFlatfile();
}
}
}
}