Rework command interface for rollback.

This commit is contained in:
Steven Lawson 2013-08-05 22:05:01 -04:00
parent 3bcc4fd7a7
commit b2eec1ef01
3 changed files with 58 additions and 53 deletions

View File

@ -1,9 +1,8 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager; import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager;
import me.StevenLawson.TotalFreedomMod.TFM_UserList;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,74 +14,61 @@ public class Command_rollback extends TFM_Command
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
if (args.length > 2) if (args.length == 1)
{ {
return false; if (args[0].equalsIgnoreCase("purgeall"))
}
if (args.length == 1 && args[0].equalsIgnoreCase("purgeall"))
{ {
TFM_Util.adminAction(sender.getName(), "Puring all rollback data", false); TFM_Util.adminAction(sender.getName(), "Purging all rollback history.", false);
playerMsg("Purged entries for " + TFM_RollbackManager.purgeEntries() + " players."); playerMsg("Purged all rollback history for " + TFM_RollbackManager.purgeEntries() + " players.");
return true;
}
if (args.length == 2 && args[0].equalsIgnoreCase("purge"))
{
OfflinePlayer p;
try
{
p = getPlayer(args[1]);
}
catch (CantFindPlayerException ex)
{
p = server.getOfflinePlayer(args[1]);
if (!p.hasPlayedBefore())
{
playerMsg("Player is not online, or never joined the server.", ChatColor.RED);
return true;
}
}
if (!TFM_RollbackManager.canRollback(p.getName()))
{
playerMsg("No rollback data found for that player", ChatColor.RED);
} }
else else
{ {
playerMsg("Purged " + TFM_RollbackManager.purgeEntries(p.getName()) + " entries."); String playerName = getPlayerName(args[0]);
return true; TFM_Util.adminAction(sender.getName(), "Rolling back player: " + playerName, false);
playerMsg("Rolled back " + TFM_RollbackManager.purgeEntries(playerName) + " edits for " + playerName + ".");
} }
} }
else if (args.length == 2)
if (args.length != 1) {
if (args[0].equalsIgnoreCase("purge"))
{
String playerName = getPlayerName(args[1]);
playerMsg("Purged " + TFM_RollbackManager.purgeEntries(playerName) + " rollback history entries for " + playerName + ".");
}
else
{
return false;
}
}
else
{ {
return false; return false;
} }
OfflinePlayer p; return true;
}
private String getPlayerName(String playerNameInput)
{
String playerName = null;
try try
{ {
p = getPlayer(args[0]); Player player = getPlayer(playerNameInput);
if (player != null)
{
playerName = player.getName();
}
} }
catch (CantFindPlayerException ex) catch (CantFindPlayerException ex)
{ {
p = server.getOfflinePlayer(args[0]);
if (!p.hasPlayedBefore())
{
playerMsg("Player is not online, or never joined the server.", ChatColor.RED);
return true;
}
} }
if (!TFM_RollbackManager.canRollback(p.getName())) if (playerName == null)
{ {
playerMsg("Player has no rollback data set.", ChatColor.RED); playerName = TFM_UserList.getInstance(plugin).searchByPartialName(playerNameInput);
return true;
} }
TFM_Util.adminAction(sender.getName(), "Rolling back player: " + p.getName(), false); return playerName;
playerMsg("Rolled back " + TFM_RollbackManager.rollback(p.getName()) + " blocks");
return true;
} }
} }

View File

@ -40,18 +40,16 @@ public class TFM_RollbackManager
public static int purgeEntries() public static int purgeEntries()
{ {
int count = 0;
Iterator<List<TFM_RollbackManager_Entry>> it = PLAYER_HISTORY_MAP.values().iterator(); Iterator<List<TFM_RollbackManager_Entry>> it = PLAYER_HISTORY_MAP.values().iterator();
while (it.hasNext()) while (it.hasNext())
{ {
List<TFM_RollbackManager_Entry> playerEntryList = it.next(); List<TFM_RollbackManager_Entry> playerEntryList = it.next();
if (playerEntryList != null) if (playerEntryList != null)
{ {
count += playerEntryList.size();
playerEntryList.clear(); playerEntryList.clear();
} }
} }
return count; return PLAYER_HISTORY_MAP.size();
} }
public static int purgeEntries(String playerName) public static int purgeEntries(String playerName)

View File

@ -4,8 +4,10 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -125,6 +127,25 @@ public class TFM_UserList
exportList(); exportList();
} }
public String searchByPartialName(String needle)
{
needle = needle.toLowerCase().trim();
Integer minEditDistance = null;
String minEditMatch = null;
Iterator<String> it = _userlist.keySet().iterator();
while (it.hasNext())
{
String haystack = it.next();
int editDistance = StringUtils.getLevenshteinDistance(needle, haystack.toLowerCase());
if (minEditDistance == null || minEditDistance.intValue() > editDistance)
{
minEditDistance = editDistance;
minEditMatch = haystack;
}
}
return minEditMatch;
}
public class TFM_UserListEntry public class TFM_UserListEntry
{ {
private String _username; private String _username;