More superadmin list updates.

This commit is contained in:
Steven Lawson 2012-11-19 21:50:33 -05:00
parent 6e266fcff2
commit 5845283bad
4 changed files with 133 additions and 43 deletions

View File

@ -1,7 +1,9 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_Superadmin;
import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -18,67 +20,111 @@ public class Command_saconfig extends TFM_Command
if (args[0].equals("list")) if (args[0].equals("list"))
{ {
sender.sendMessage(ChatColor.GOLD + "Superadmins: " + StringUtils.join(TFM_SuperadminList.getSuperadminNames(), ", ")); sender.sendMessage(ChatColor.GOLD + "Superadmins: " + StringUtils.join(TFM_SuperadminList.getSuperadminNames(), ", "));
}
else
{
if (!senderIsConsole)
{
sender.sendMessage(ChatColor.GRAY + "This command may only be used from the console.");
return true;
}
if (args[0].equals("clean"))
{
TFM_Util.adminAction(sender.getName(), "Cleaning superadmin list.", true);
TFM_SuperadminList.cleanSuperadminList(true);
sender.sendMessage(ChatColor.GOLD + "Superadmins: " + StringUtils.join(TFM_SuperadminList.getSuperadminNames(), ", "));
}
else
{
return false;
}
return true; return true;
} }
return false;
}
if (!senderIsConsole)
{
sender.sendMessage(ChatColor.GRAY + "This command may only be used from the console.");
return true; return true;
} }
else if (args.length == 2)
if (args.length < 2)
{ {
return false; if (!senderIsConsole)
}
if (args[0].equalsIgnoreCase("add"))
{
Player p;
try
{ {
p = getPlayer(args[1]); sender.sendMessage(ChatColor.GRAY + "This command may only be used from the console.");
}
catch (CantFindPlayerException ex)
{
sender.sendMessage(ex.getMessage());
return true; return true;
} }
TFM_Util.adminAction(sender.getName(), "Adding " + p.getName() + " to the superadmin list.", true); if (args[0].equalsIgnoreCase("add"))
TFM_SuperadminList.addSuperadmin(p);
}
else if (args[0].equalsIgnoreCase("delete") || args[0].equalsIgnoreCase("del") || args[0].equalsIgnoreCase("remove"))
{
String target_name = args[1];
try
{ {
target_name = getPlayer(target_name).getName(); Player p = null;
String admin_name = null;
try
{
p = getPlayer(args[1]);
}
catch (CantFindPlayerException ex)
{
TFM_Superadmin superadmin = TFM_SuperadminList.getAdminEntry(args[1].toLowerCase());
if (superadmin != null)
{
admin_name = superadmin.getName();
}
else
{
sender.sendMessage(ex.getMessage());
return true;
}
}
if (p != null)
{
TFM_Util.adminAction(sender.getName(), "Adding " + p.getName() + " to the superadmin list.", true);
TFM_SuperadminList.addSuperadmin(p);
}
else if (admin_name != null)
{
TFM_Util.adminAction(sender.getName(), "Adding " + admin_name + " to the superadmin list.", true);
TFM_SuperadminList.addSuperadmin(admin_name);
}
} }
catch (CantFindPlayerException ex) else if (args[0].equalsIgnoreCase("delete") || args[0].equalsIgnoreCase("del") || args[0].equalsIgnoreCase("remove"))
{ {
if (!TFM_SuperadminList.isSeniorAdmin(sender))
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true;
}
String target_name = args[1];
try
{
target_name = getPlayer(target_name).getName();
}
catch (CantFindPlayerException ex)
{
}
if (!TFM_SuperadminList.getSuperadminNames().contains(target_name.toLowerCase()))
{
sender.sendMessage("Superadmin not found: " + target_name);
return true;
}
TFM_Util.adminAction(sender.getName(), "Removing " + target_name + " from the superadmin list.", true);
TFM_SuperadminList.removeSuperadmin(target_name);
}
else
{
return false;
} }
if (!TFM_SuperadminList.getSuperadminNames().contains(target_name.toLowerCase())) return true;
{
sender.sendMessage("Superadmin not found: " + target_name);
return true;
}
TFM_Util.adminAction(sender.getName(), "Removing " + target_name + " from the superadmin list.", true);
TFM_SuperadminList.removeSuperadmin(target_name);
} }
else else
{ {
return false; return false;
} }
return true;
} }
} }

View File

@ -9,6 +9,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -23,6 +24,7 @@ public class TFM_SuperadminList
private static List<String> superadminNames = new ArrayList<String>(); private static List<String> superadminNames = new ArrayList<String>();
private static List<String> superadminIPs = new ArrayList<String>(); private static List<String> superadminIPs = new ArrayList<String>();
private static List<String> seniorAdminNames = new ArrayList<String>(); private static List<String> seniorAdminNames = new ArrayList<String>();
private static int clean_threshold_hours = 24 * 7; // 1 Week
private TFM_SuperadminList() private TFM_SuperadminList()
{ {
@ -48,6 +50,8 @@ public class TFM_SuperadminList
TFM_Util.createDefaultConfiguration(TotalFreedomMod.SUPERADMIN_FILE, TotalFreedomMod.plugin_file); TFM_Util.createDefaultConfiguration(TotalFreedomMod.SUPERADMIN_FILE, TotalFreedomMod.plugin_file);
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE)); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
clean_threshold_hours = config.getInt("clean_threshold_hours", clean_threshold_hours);
if (config.isConfigurationSection("superadmins")) if (config.isConfigurationSection("superadmins"))
{ {
ConfigurationSection section = config.getConfigurationSection("superadmins"); ConfigurationSection section = config.getConfigurationSection("superadmins");
@ -121,6 +125,8 @@ public class TFM_SuperadminList
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();
config.set("clean_threshold_hours", clean_threshold_hours);
Iterator<Entry<String, TFM_Superadmin>> it = superadminList.entrySet().iterator(); Iterator<Entry<String, TFM_Superadmin>> it = superadminList.entrySet().iterator();
while (it.hasNext()) while (it.hasNext())
{ {
@ -366,4 +372,38 @@ public class TFM_SuperadminList
{ {
removeSuperadmin(p.getName()); removeSuperadmin(p.getName());
} }
public static void cleanSuperadminList(boolean verbose)
{
try
{
Iterator<Entry<String, TFM_Superadmin>> it = superadminList.entrySet().iterator();
while (it.hasNext())
{
Entry<String, TFM_Superadmin> pair = it.next();
TFM_Superadmin superadmin = pair.getValue();
if (superadmin.isActivated() && !superadmin.isSeniorAdmin())
{
Date last_login = superadmin.getLastLogin();
long hours_since_login = TimeUnit.HOURS.convert(new Date().getTime() - last_login.getTime(), TimeUnit.MILLISECONDS);
if (hours_since_login > clean_threshold_hours)
{
if (verbose)
{
TFM_Util.adminAction("TotalFreedomSystem", "Deactivating superadmin \"" + superadmin.getName() + "\", inactive for " + hours_since_login + " hours.", true);
}
superadmin.setActivated(false);
}
}
}
saveSuperadminList();
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}
} }

View File

@ -163,7 +163,7 @@ commands:
usage: /<command> <carts> usage: /<command> <carts>
saconfig: saconfig:
description: Owner command - Manage superadmins. description: Owner command - Manage superadmins.
usage: /<command> <list | <add|delete> <username>> usage: /<command> <list | clean | <add|delete> <username>>
say: say:
description: Broadcasts the given message as the console, includes sender name. description: Broadcasts the given message as the console, includes sender name.
usage: /<command> <message> usage: /<command> <message>

View File

@ -1,3 +1,4 @@
clean_threshold_hours: 168
superadmins: superadmins:
darthsalamon: darthsalamon:
ips: ips:
@ -8,6 +9,7 @@ superadmins:
is_senior_admin: true is_senior_admin: true
console_aliases: console_aliases:
- darth - darth
is_activated: true
madgeek1450: madgeek1450:
ips: ips:
- 127.0.0.1 - 127.0.0.1
@ -17,6 +19,7 @@ superadmins:
is_senior_admin: true is_senior_admin: true
console_aliases: console_aliases:
- madgeek - madgeek
is_activated: false
markbyron: markbyron:
ips: ips:
- 74.125.224.72 - 74.125.224.72
@ -25,3 +28,4 @@ superadmins:
custom_login_message: the &downer&b. custom_login_message: the &downer&b.
is_senior_admin: true is_senior_admin: true
console_aliases: [] console_aliases: []
is_activated: true