Fixed duplicate entries when adding/removing players

Some refractoring
This commit is contained in:
unknown 2014-04-21 17:45:32 +02:00
parent 9315227906
commit c82113dc22
9 changed files with 153 additions and 120 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Mon Apr 21 14:50:16 CEST 2014
build.number=778
#Mon Apr 21 17:44:46 CEST 2014
build.number=782

View File

@ -9,6 +9,7 @@ import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -24,11 +25,12 @@ public class Command_saconfig extends TFM_Command
{
return false;
}
if (args.length == 1)
{
if (args[0].equals("list"))
{
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperadminNames(), ", "), ChatColor.GOLD);
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperNames(), ", "), ChatColor.GOLD);
return true;
}
@ -43,7 +45,7 @@ public class Command_saconfig extends TFM_Command
TFM_Util.adminAction(sender.getName(), "Cleaning superadmin list", true);
TFM_AdminList.cleanSuperadminList(true);
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperadminUUIDs(), ", "), ChatColor.YELLOW);
playerMsg("Superadmins: " + StringUtils.join(TFM_AdminList.getSuperUUIDs(), ", "), ChatColor.YELLOW);
return true;
}
@ -91,8 +93,7 @@ public class Command_saconfig extends TFM_Command
if (args[0].equalsIgnoreCase("add"))
{
Player player = null;
String playername = null;
OfflinePlayer player;
try
{
@ -101,27 +102,19 @@ public class Command_saconfig extends TFM_Command
catch (PlayerNotFoundException ex)
{
final TFM_Admin superadmin = TFM_AdminList.getEntry(args[1]);
if (superadmin != null)
{
playername = superadmin.getLastLoginName();
}
else
if (superadmin == null)
{
playerMsg(ex.getMessage(), ChatColor.RED);
return true;
}
player = Bukkit.getOfflinePlayer(superadmin.getLastLoginName());
}
if (player != null)
{
TFM_Util.adminAction(sender.getName(), "Adding " + player.getName() + " to the superadmin list.", true);
TFM_AdminList.addSuperadmin(player);
}
else if (playername != null)
{
TFM_Util.adminAction(sender.getName(), "Adding " + playername + " to the superadmin list.", true);
TFM_AdminList.addSuperadmin(player);
}
TFM_Util.adminAction(sender.getName(), "Adding " + player.getName() + " to the superadmin list", true);
TFM_AdminList.addSuperadmin(player);
return true;
}
@ -143,7 +136,7 @@ public class Command_saconfig extends TFM_Command
{
}
if (!TFM_AdminList.getLowerSuperadminNames().contains(targetName.toLowerCase()))
if (!TFM_AdminList.getLowerSuperNames().contains(targetName.toLowerCase()))
{
playerMsg("Superadmin not found: " + targetName);
return true;

View File

@ -35,14 +35,14 @@ public class Module_players extends TFM_HTTPD_Module
}
// Super admins (non-telnet and non-senior)
for (UUID superadmin : TFM_AdminList.getSuperadminUUIDs())
for (UUID superadmin : TFM_AdminList.getSuperUUIDs())
{
if (TFM_AdminList.getSenioradminUUIDs().contains(superadmin))
if (TFM_AdminList.getSeniorUUIDs().contains(superadmin))
{
continue;
}
if (TFM_AdminList.getTelnetadminUUIDs().contains(superadmin))
if (TFM_AdminList.getTelnetUUIDs().contains(superadmin))
{
continue;
}
@ -51,9 +51,9 @@ public class Module_players extends TFM_HTTPD_Module
}
// Telnet admins (non-senior)
for (UUID telnetadmin : TFM_AdminList.getTelnetadminUUIDs())
for (UUID telnetadmin : TFM_AdminList.getTelnetUUIDs())
{
if (TFM_AdminList.getSenioradminUUIDs().contains(telnetadmin))
if (TFM_AdminList.getSeniorUUIDs().contains(telnetadmin))
{
continue;
}
@ -61,7 +61,7 @@ public class Module_players extends TFM_HTTPD_Module
}
// Senior admins
for (UUID senioradmin : TFM_AdminList.getSenioradminUUIDs())
for (UUID senioradmin : TFM_AdminList.getSeniorUUIDs())
{
senioradmins.add(getName(senioradmin));
}

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@ -19,16 +20,16 @@ public class TFM_Admin
private Date lastLogin;
private boolean isActivated;
public TFM_Admin(UUID uuid, String lastLoginName, List<String> ips, Date lastLogin, String loginMessage, boolean isSeniorAdmin, boolean isTelnetAdmin, List<String> consoleAliases, boolean isActivated)
public TFM_Admin(UUID uuid, String lastLoginName, Date lastLogin, String loginMessage, boolean isSeniorAdmin, boolean isTelnetAdmin, boolean isActivated)
{
this.uuid = uuid;
this.lastLoginName = lastLoginName;
this.ips = ips;
this.ips = new ArrayList<String>();
this.lastLogin = lastLogin;
this.loginMessage = loginMessage;
this.isSeniorAdmin = isSeniorAdmin;
this.isTelnetAdmin = isTelnetAdmin;
this.consoleAliases = consoleAliases;
this.consoleAliases = new ArrayList<String>();
this.isActivated = isActivated;
}

View File

@ -7,6 +7,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ -17,6 +18,7 @@ import me.StevenLawson.TotalFreedomMod.Config.TFM_Config;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import me.StevenLawson.TotalFreedomMod.Config.TFM_MainConfig;
import me.StevenLawson.TotalFreedomMod.World.TFM_AdminWorld;
import net.minecraft.util.com.google.common.collect.Sets;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
@ -28,72 +30,92 @@ import org.bukkit.util.FileUtil;
public class TFM_AdminList
{
private static final Map<UUID, TFM_Admin> adminList;
private static final Set<UUID> superadminUUIDs;
private static final Set<UUID> telnetadminUUIDs;
private static final Set<UUID> senioradminUUIDs;
private static final Set<String> consoleAliases;
private static final Set<String> superadminIps;
private static final Set<UUID> superUUIDs;
private static final Set<UUID> telnetUUIDs;
private static final Set<UUID> seniorUUIDs;
private static final Set<String> seniorConsoleAliases;
private static final Set<String> superIps;
private static int cleanThreshold = 24 * 7; // 1 Week in hours
static
{
adminList = new HashMap<UUID, TFM_Admin>();
superadminUUIDs = new HashSet<UUID>();
telnetadminUUIDs = new HashSet<UUID>();
senioradminUUIDs = new HashSet<UUID>();
consoleAliases = new HashSet<String>();
superadminIps = new HashSet<String>();
superUUIDs = new HashSet<UUID>();
telnetUUIDs = new HashSet<UUID>();
seniorUUIDs = new HashSet<UUID>();
seniorConsoleAliases = new HashSet<String>();
superIps = new HashSet<String>();
}
public static Set<UUID> getSuperadminUUIDs()
private TFM_AdminList()
{
return Collections.unmodifiableSet(superadminUUIDs);
throw new AssertionError();
}
public static Set<UUID> getTelnetadminUUIDs()
public static Set<UUID> getSuperUUIDs()
{
return Collections.unmodifiableSet(telnetadminUUIDs);
return Collections.unmodifiableSet(superUUIDs);
}
public static Set<UUID> getSenioradminUUIDs()
public static Set<UUID> getTelnetUUIDs()
{
return Collections.unmodifiableSet(senioradminUUIDs);
return Collections.unmodifiableSet(telnetUUIDs);
}
public static Set<String> getConsoleAliases()
public static Set<UUID> getSeniorUUIDs()
{
return Collections.unmodifiableSet(consoleAliases);
return Collections.unmodifiableSet(seniorUUIDs);
}
public static Set<String> getSeniorConsoleAliases()
{
return Collections.unmodifiableSet(seniorConsoleAliases);
}
public static Set<String> getSuperadminIps()
{
return Collections.unmodifiableSet(superadminIps);
return Collections.unmodifiableSet(superIps);
}
public static Set<String> getSuperadminNames()
public static Set<String> getSuperNames()
{
final Set<String> names = new HashSet<String>();
for (TFM_Admin admin : adminList.values())
{
if (!admin.isActivated())
{
continue;
}
names.add(admin.getLastLoginName());
}
return Collections.unmodifiableSet(names);
}
public static Set<String> getLowerSuperadminNames()
public static Set<String> getLowerSuperNames()
{
final Set<String> names = new HashSet<String>();
for (TFM_Admin admin : adminList.values())
{
if (!admin.isActivated())
{
continue;
}
names.add(admin.getLastLoginName().toLowerCase());
}
return Collections.unmodifiableSet(names);
}
public static Set<TFM_Admin> getAllAdmins()
{
return Sets.newHashSet(adminList.values());
}
public static void load()
{
try
@ -176,7 +198,7 @@ public class TFM_AdminList
}
}
public static void backupSavedList()
public static void createBackup()
{
final File oldYaml = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE);
final File newYaml = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE + ".bak");
@ -185,11 +207,11 @@ public class TFM_AdminList
public static void updateIndexLists()
{
superadminUUIDs.clear();
telnetadminUUIDs.clear();
senioradminUUIDs.clear();
consoleAliases.clear();
superadminIps.clear();
superUUIDs.clear();
telnetUUIDs.clear();
seniorUUIDs.clear();
seniorConsoleAliases.clear();
superIps.clear();
for (TFM_Admin admin : adminList.values())
{
@ -200,27 +222,27 @@ public class TFM_AdminList
final UUID uuid = admin.getUniqueId();
superadminUUIDs.add(uuid);
superUUIDs.add(uuid);
for (String ip : admin.getIps())
{
superadminIps.add(ip);
superIps.add(ip);
}
if (admin.isTelnetAdmin())
{
telnetadminUUIDs.add(uuid);
telnetUUIDs.add(uuid);
for (String alias : admin.getConsoleAliases())
{
consoleAliases.add(alias.toLowerCase());
seniorConsoleAliases.add(alias.toLowerCase());
}
}
if (admin.isSeniorAdmin())
{
senioradminUUIDs.add(uuid);
seniorUUIDs.add(uuid);
}
}
@ -229,8 +251,6 @@ public class TFM_AdminList
public static void save()
{
updateIndexLists();
final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true);
config.load();
@ -327,12 +347,13 @@ public class TFM_AdminList
public static void updateLastLogin(Player player)
{
final TFM_Admin admin = getEntry(player);
if (admin != null)
if (admin == null)
{
admin.setLastLogin(new Date());
admin.setLastLoginName(player.getName());
save();
return;
}
admin.setLastLogin(new Date());
admin.setLastLoginName(player.getName());
save();
}
public static boolean isSeniorAdmin(CommandSender sender)
@ -353,7 +374,7 @@ public class TFM_AdminList
if (!(sender instanceof Player))
{
return consoleAliases.contains(sender.getName())
return seniorConsoleAliases.contains(sender.getName())
|| (TFM_MainConfig.getInstance().getBoolean(TFM_ConfigEntry.CONSOLE_IS_SENIOR) && sender.getName().equals("CONSOLE"));
}
@ -373,13 +394,13 @@ public class TFM_AdminList
return true;
}
if (Bukkit.getOnlineMode() && superadminUUIDs.contains(((Player) sender).getUniqueId()))
if (Bukkit.getOnlineMode() && superUUIDs.contains(((Player) sender).getUniqueId()))
{
return true;
}
if (superadminIps.contains(TFM_Util.getIp((Player) sender)))
if (superIps.contains(TFM_Util.getIp((Player) sender)))
{
return true;
}
@ -413,7 +434,7 @@ public class TFM_AdminList
{
ip = ip.trim();
if (superadminIps.contains(ip))
if (superIps.contains(ip))
{
return true;
}
@ -421,7 +442,7 @@ public class TFM_AdminList
try
{
String matchIp = null;
for (String testIp : superadminIps)
for (String testIp : superIps)
{
if (TFM_Util.fuzzyIpMatch(ip, testIp, 3))
{
@ -461,7 +482,7 @@ public class TFM_AdminList
public static boolean isAdminImpostor(Player player)
{
if (superadminUUIDs.contains(player.getUniqueId()))
if (superUUIDs.contains(player.getUniqueId()))
{
return !isSuperAdmin(player);
}
@ -469,56 +490,65 @@ public class TFM_AdminList
return false;
}
public static void addSuperadmin(Player player)
public static void addSuperadmin(OfflinePlayer player)
{
try
final UUID uuid = player.getUniqueId();
final String ip = TFM_Util.getIp(player);
if (adminList.containsKey(uuid))
{
final UUID uuid = player.getUniqueId();
final String ip = TFM_Util.getIp(player);
final TFM_Admin superadmin = adminList.get(uuid);
superadmin.setActivated(true);
if (adminList.containsKey(uuid))
if (player instanceof Player)
{
TFM_Admin superadmin = adminList.get(uuid);
superadmin.setActivated(true);
superadmin.addIp(TFM_Util.getIp(player));
superadmin.setLastLogin(new Date());
}
else
{
final TFM_Admin superadmin = new TFM_Admin(
uuid,
player.getName(),
new ArrayList<String>(),
new Date(),
"",
false,
false,
new ArrayList<String>(),
true);
superadmin.addIp(ip);
adminList.put(uuid, superadmin);
}
save();
return;
}
catch (Exception ex)
if (ip == null)
{
TFM_Log.severe("Cannot add superadmin: " + TFM_Util.formatPlayer(player));
TFM_Log.severe(ex);
TFM_Log.severe("Could not retrieve IP!");
return;
}
final TFM_Admin superadmin = new TFM_Admin(
uuid,
player.getName(),
new Date(),
"",
false,
false,
true);
superadmin.addIp(ip);
adminList.put(uuid, superadmin);
save();
updateIndexLists();
}
public static void removeSuperadmin(OfflinePlayer player)
{
final UUID uuid = player.getUniqueId();
if (adminList.containsKey(uuid))
if (!adminList.containsKey(uuid))
{
TFM_Admin superadmin = adminList.get(uuid);
superadmin.setActivated(false);
Command_logs.deactivateSuperadmin(superadmin);
save();
TFM_Log.warning("Could not remove admin: " + TFM_Util.formatPlayer(player));
TFM_Log.warning("Player is not an admin!");
return;
}
final TFM_Admin superadmin = adminList.get(uuid);
superadmin.setActivated(false);
Command_logs.deactivateSuperadmin(superadmin);
save();
updateIndexLists();
}
public static void cleanSuperadminList(boolean verbose)
@ -549,16 +579,8 @@ public class TFM_AdminList
TFM_TwitterHandler.getInstance().delTwitter(superadmin.getLastLoginName());
}
}
save();
}
private TFM_AdminList()
{
throw new AssertionError();
}
public File getConfigFile()
{
return new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE);
updateIndexLists();
}
}

View File

@ -94,6 +94,11 @@ public class TFM_PlayerList
return null;
}
public TFM_PlayerEntry getEntry(UUID uuid)
{
return playerList.get(uuid);
}
public boolean existsEntry(Player player)
{
return playerList.containsKey(player.getUniqueId());

View File

@ -89,7 +89,7 @@ public class TFM_ServerInterface
boolean isAdmin;
if (server.getOnlineMode())
{
isAdmin = TFM_AdminList.getSuperadminUUIDs().contains(uuid);
isAdmin = TFM_AdminList.getSuperUUIDs().contains(uuid);
}
else
{

View File

@ -141,9 +141,21 @@ public class TFM_Util
TFM_Util.bcastMsg(adminName + " - " + action, (isRed ? ChatColor.RED : ChatColor.AQUA));
}
public static String getIp(Player player)
public static String getIp(OfflinePlayer player)
{
return player.getAddress().getAddress().getHostAddress().trim();
if (player instanceof Player)
{
return ((Player) player).getAddress().getAddress().getHostAddress().trim();
}
final TFM_PlayerEntry entry = TFM_PlayerList.getInstance().getEntry(player.getUniqueId());
if (entry == null)
{
return null;
}
return entry.getIps().get(0);
}
public static String formatLocation(Location location)
@ -155,7 +167,7 @@ public class TFM_Util
Math.round(location.getZ()));
}
public static String formatPlayer(Player player)
public static String formatPlayer(OfflinePlayer player)
{
return player.getName() + " (" + player.getUniqueId() + ")";
}

View File

@ -270,7 +270,7 @@ public class TotalFreedomMod extends JavaPlugin
{
try
{
TFM_AdminList.backupSavedList();
TFM_AdminList.createBackup();
TFM_AdminList.load();
}
catch (Exception ex)