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

@ -25,6 +25,7 @@ Version 1.4.00-dev
+ Added vanilla XP boost for Fishing - includes permissions, config options, etc + Added vanilla XP boost for Fishing - includes permissions, config options, etc
+ Added particle effect for bleeding + Added particle effect for bleeding
+ Added methods to check if a player is in party or admin chat to the ChatAPI + Added methods to check if a player is in party or admin chat to the ChatAPI
+ Added /mcpurge functionality for Flatfile users
= Fixed multiple commands not working properly on offline players = Fixed multiple commands not working properly on offline players
= Fixed /mmoedit not giving feedback when modifying another players stats = Fixed /mmoedit not giving feedback when modifying another players stats
= Fixed the guide usage string showing up every time /skillname was called = Fixed the guide usage string showing up every time /skillname was called

View File

@ -243,6 +243,7 @@ public final class CommandRegistrationHelper {
command.setPermission("mcmmo.commands.mcstats"); command.setPermission("mcmmo.commands.mcstats");
command.setPermissionMessage(permissionsMessage); command.setPermissionMessage(permissionsMessage);
command.setUsage(LocaleLoader.getString("Commands.Usage.0", "mcstats")); command.setUsage(LocaleLoader.getString("Commands.Usage.0", "mcstats"));
command.setAliases(aliasList);
command.setExecutor(new McstatsCommand()); command.setExecutor(new McstatsCommand());
} }

View File

@ -2,15 +2,22 @@ package com.gmail.nossr50.database;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import com.gmail.nossr50.mcMMO; 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.skills.utilities.SkillType;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Users;
public final class Leaderboard { public final class Leaderboard {
private static HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>(); 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); 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.config.Config;
import com.gmail.nossr50.database.Database; import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
public class McpurgeCommand implements CommandExecutor{ public class McpurgeCommand implements CommandExecutor{
@ -21,7 +22,11 @@ public class McpurgeCommand implements CommandExecutor{
} }
} }
else { else {
//TODO: Make this work for Flatfile data. Leaderboard.purgePowerlessFlatfile();
if (Config.getInstance().getOldUsersCutoff() != -1) {
Leaderboard.purgeOldFlatfile();
}
} }
sender.sendMessage(LocaleLoader.getString("Commands.mcpurge.Success")); sender.sendMessage(LocaleLoader.getString("Commands.mcpurge.Success"));

View File

@ -1,18 +1,13 @@
package com.gmail.nossr50.database.commands; 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.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
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.database.Database; import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
public class McremoveCommand implements CommandExecutor { public class McremoveCommand implements CommandExecutor {
@ -51,7 +46,7 @@ public class McremoveCommand implements CommandExecutor {
} }
} }
else { else {
if (removeFlatFileUser(playerName)) { if (Leaderboard.removeFlatFileUser(playerName)) {
sender.sendMessage(success); sender.sendMessage(success);
} }
else { else {
@ -63,59 +58,4 @@ public class McremoveCommand implements CommandExecutor {
return true; 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.config.Config;
import com.gmail.nossr50.database.Database; import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.Leaderboard;
public class UserPurgeTask implements Runnable { public class UserPurgeTask implements Runnable {
@Override @Override
@ -14,7 +15,11 @@ public class UserPurgeTask implements Runnable {
} }
} }
else { else {
//TODO: Make this work for Flatfile data. Leaderboard.purgePowerlessFlatfile();
if (Config.getInstance().getOldUsersCutoff() != -1) {
Leaderboard.purgeOldFlatfile();
}
} }
} }
} }

View File

@ -395,6 +395,7 @@ public class PlayerProfile {
writer.append(skills.get(SkillType.FISHING)).append(":"); writer.append(skills.get(SkillType.FISHING)).append(":");
writer.append(skillsXp.get(SkillType.FISHING)).append(":"); writer.append(skillsXp.get(SkillType.FISHING)).append(":");
writer.append(String.valueOf(skillsDATS.get(AbilityType.BLAST_MINING))).append(":"); writer.append(String.valueOf(skillsDATS.get(AbilityType.BLAST_MINING))).append(":");
writer.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":");
writer.append("\r\n"); writer.append("\r\n");
} }
} }
@ -455,6 +456,7 @@ public class PlayerProfile {
out.append("0:"); // Fishing out.append("0:"); // Fishing
out.append("0:"); // FishingXp out.append("0:"); // FishingXp
out.append("0:"); // Blast Mining out.append("0:"); // Blast Mining
out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); //LastLogin
// Add more in the same format as the line above // Add more in the same format as the line above

View File

@ -169,6 +169,19 @@ public final class Misc {
return 0; return 0;
} }
/**
* Gets the long represented by this string.
*
* @param string The string to parse
* @return the long represented by this string
*/
public static long getLong(String string) {
if (isLong(string)) {
return Long.parseLong(string);
}
return 0;
}
/** /**
* Checks to see if an entity is currently invincible. * Checks to see if an entity is currently invincible.
* *
@ -287,6 +300,22 @@ public final class Misc {
} }
} }
/**
* Determine if a string represents a Long
*
* @param string String to check
* @return true if the string is a Long, false otherwise
*/
public static boolean isLong(String string) {
try {
Long.parseLong(string);
return true;
}
catch (NumberFormatException nFE) {
return false;
}
}
/** /**
* Drop items at a given location. * Drop items at a given location.
* *