Restructured /saconfig. Resolves #322. Resolves #303.

This commit is contained in:
StevenLawson 2014-09-19 19:59:29 -04:00
parent f4440fd262
commit ff9164714c
2 changed files with 226 additions and 153 deletions

View File

@ -14,56 +14,46 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@CommandPermissions(level = AdminLevel.OP, source = SourceType.BOTH) @CommandPermissions(level = AdminLevel.OP, source = SourceType.BOTH)
@CommandParameters(description = "Manage superadmins.", @CommandParameters(description = "Manage superadmins.", usage = "/<command> <list | clean | clearme [ip] | <add | delete | info> <username>>")
usage = "/<command> <list | clean | clearme [ip] | <add | delete | info> <username>>")
public class Command_saconfig extends TFM_Command public class Command_saconfig 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 == 0 || args.length > 2) final SAConfigMode mode;
try
{ {
mode = SAConfigMode.findMode(args, sender, senderIsConsole);
}
catch (final PermissionsException ex)
{
playerMsg(ex.getMessage());
return true;
}
catch (final FormatException ex)
{
playerMsg(ex.getMessage());
return false; return false;
} }
if (args.length == 1) switch (mode)
{ {
if (args[0].equals("list")) case LIST:
{ {
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperNames(), ", "), ChatColor.GOLD); playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperNames(), ", "), ChatColor.GOLD);
return true;
}
if (args[0].equals("clean")) break;
{
if (!TFM_AdminList.isSeniorAdmin(sender, true))
{
playerMsg(TFM_Command.MSG_NO_PERMS);
return true;
} }
case CLEAN:
{
TFM_Util.adminAction(sender.getName(), "Cleaning superadmin list", true); TFM_Util.adminAction(sender.getName(), "Cleaning superadmin list", true);
TFM_AdminList.cleanSuperadminList(true); TFM_AdminList.cleanSuperadminList(true);
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperNames(), ", "), ChatColor.YELLOW); playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperNames(), ", "), ChatColor.YELLOW);
return true;
}
}
// All commands below are superadmin+ commands. break;
if (!TFM_AdminList.isSuperAdmin(sender))
{
playerMsg(TFM_Command.MSG_NO_PERMS);
return true;
} }
case CLEARME:
if (args[0].equals("clearme"))
{ {
if (senderIsConsole)
{
playerMsg(TFM_Command.NOT_FROM_CONSOLE);
return true;
}
final TFM_Admin admin = TFM_AdminList.getEntry(sender_p); final TFM_Admin admin = TFM_AdminList.getEntry(sender_p);
final String ip = TFM_Util.getIp(sender_p); final String ip = TFM_Util.getIp(sender_p);
@ -72,58 +62,47 @@ public class Command_saconfig extends TFM_Command
{ {
TFM_Util.adminAction(sender.getName(), "Cleaning my supered IPs", true); TFM_Util.adminAction(sender.getName(), "Cleaning my supered IPs", true);
int counter = 0; int counter = admin.getIps().size() - 1;
for (int i = 0; i < admin.getIps().size(); i++) admin.clearIPs();
{ admin.addIp(ip);
if (admin.getIps().get(i).equals(ip))
{
continue;
}
admin.removeIp(admin.getIps().get(i));
counter++;
}
TFM_AdminList.saveAll(); TFM_AdminList.saveAll();
playerMsg(counter + " IPs removed."); playerMsg(counter + " IPs removed.");
playerMsg(admin.getIps().get(0) + " is now your only IP address"); playerMsg(admin.getIps().get(0) + " is now your only IP address");
return true;
} }
else
// args.length == 2 {
if (!admin.getIps().contains(args[1])) if (!admin.getIps().contains(args[1]))
{ {
playerMsg("That IP is not registered to you."); playerMsg("That IP is not registered to you.");
return true;
} }
else if (ip.equals(args[1]))
if (ip.equals(args[1]))
{ {
playerMsg("You cannot remove your current IP."); playerMsg("You cannot remove your current IP.");
return true;
} }
else
{
TFM_Util.adminAction(sender.getName(), "Removing a supered IP", true); TFM_Util.adminAction(sender.getName(), "Removing a supered IP", true);
admin.removeIp(args[1]); admin.removeIp(args[1]);
TFM_AdminList.saveAll(); TFM_AdminList.saveAll();
playerMsg("Removed IP " + args[1]); playerMsg("Removed IP " + args[1]);
playerMsg("Current IPs: " + StringUtils.join(admin.getIps(), ", ")); playerMsg("Current IPs: " + StringUtils.join(admin.getIps(), ", "));
return true; }
} }
if (args[0].equals("info")) break;
}
case INFO:
{ {
TFM_Admin superadmin = TFM_AdminList.getEntry(args[1].toLowerCase()); TFM_Admin superadmin = TFM_AdminList.getEntry(args[1].toLowerCase());
if (superadmin == null) if (superadmin == null)
{ {
final Player player = getPlayer(args[1]); final Player player = getPlayer(args[1]);
if (player != null) if (player != null)
{ {
superadmin = TFM_AdminList.getEntry(player.getName().toLowerCase()); superadmin = TFM_AdminList.getEntry(player.getName().toLowerCase());
@ -133,22 +112,17 @@ public class Command_saconfig extends TFM_Command
if (superadmin == null) if (superadmin == null)
{ {
playerMsg("Superadmin not found: " + args[1]); playerMsg("Superadmin not found: " + args[1]);
return true;
} }
else
{
playerMsg(superadmin.toString()); playerMsg(superadmin.toString());
return true;
} }
if (!senderIsConsole) break;
{
playerMsg("This command may only be used from the console.");
return true;
} }
case ADD:
if (args[0].equals("add"))
{ {
OfflinePlayer player = getPlayer(args[1]); OfflinePlayer player = getPlayer(args[1], true); // Exact case-insensitive match.
if (player == null) if (player == null)
{ {
@ -177,20 +151,13 @@ public class Command_saconfig extends TFM_Command
} }
} }
return true; break;
} }
case DELETE:
if ("delete".equals(args[0]))
{ {
if (!TFM_AdminList.isSeniorAdmin(sender))
{
playerMsg(TFM_Command.MSG_NO_PERMS);
return true;
}
String targetName = args[1]; String targetName = args[1];
final Player player = getPlayer(targetName); final Player player = getPlayer(targetName, true); // Exact case-insensitive match.
if (player != null) if (player != null)
{ {
@ -211,9 +178,110 @@ public class Command_saconfig extends TFM_Command
{ {
TFM_TwitterHandler.delTwitterVerbose(targetName, sender); TFM_TwitterHandler.delTwitterVerbose(targetName, sender);
} }
break;
}
}
return true; return true;
} }
return false;
private static enum SAConfigMode
{
LIST("list", AdminLevel.OP, SourceType.BOTH, 1, 1),
CLEAN("clean", AdminLevel.SENIOR, SourceType.BOTH, 1, 1),
CLEARME("clearme", AdminLevel.SUPER, SourceType.ONLY_IN_GAME, 1, 2),
INFO("info", AdminLevel.SUPER, SourceType.BOTH, 2, 2),
ADD("add", AdminLevel.SUPER, SourceType.ONLY_CONSOLE, 2, 2),
DELETE("delete", AdminLevel.SENIOR, SourceType.ONLY_CONSOLE, 2, 2);
private final String modeName;
private final AdminLevel adminLevel;
private final SourceType sourceType;
private final int minArgs;
private final int maxArgs;
private SAConfigMode(String modeName, AdminLevel adminLevel, SourceType sourceType, int minArgs, int maxArgs)
{
this.modeName = modeName;
this.adminLevel = adminLevel;
this.sourceType = sourceType;
this.minArgs = minArgs;
this.maxArgs = maxArgs;
}
private static SAConfigMode findMode(final String[] args, final CommandSender sender, final boolean senderIsConsole) throws PermissionsException, FormatException
{
if (args.length == 0)
{
throw new FormatException("Invalid number of arguments.");
}
boolean isSuperAdmin = TFM_AdminList.isSuperAdmin(sender);
boolean isSeniorAdmin = isSuperAdmin ? TFM_AdminList.isSeniorAdmin(sender, false) : false;
for (final SAConfigMode mode : values())
{
if (mode.modeName.equalsIgnoreCase(args[0]))
{
if (mode.adminLevel == AdminLevel.SUPER)
{
if (!isSuperAdmin)
{
throw new PermissionsException(TFM_Command.MSG_NO_PERMS);
}
}
else if (mode.adminLevel == AdminLevel.SENIOR)
{
if (!isSeniorAdmin)
{
throw new PermissionsException(TFM_Command.MSG_NO_PERMS);
}
}
if (mode.sourceType == SourceType.ONLY_IN_GAME)
{
if (senderIsConsole)
{
throw new PermissionsException("This command may only be used in-game.");
}
}
else if (mode.sourceType == SourceType.ONLY_CONSOLE)
{
if (!senderIsConsole)
{
throw new PermissionsException("This command may only be used from the console.");
}
}
if (args.length >= mode.minArgs && args.length <= mode.maxArgs)
{
return mode;
}
else
{
throw new FormatException("Invalid number of arguments for mode: " + mode.modeName);
}
}
}
throw new FormatException("Invalid mode.");
}
}
private static class PermissionsException extends Exception
{
public PermissionsException(final String message)
{
super(message);
}
}
private static class FormatException extends Exception
{
public FormatException(final String message)
{
super(message);
}
} }
} }

View File

@ -108,6 +108,11 @@ public class TFM_Admin
} }
} }
public void clearIPs()
{
ips.clear();
}
public Date getLastLogin() public Date getLastLogin()
{ {
return lastLogin; return lastLogin;