Finished TFM_UuidResolver and /uuid. Resolves #214

This commit is contained in:
unknown 2014-06-29 12:09:03 +02:00
parent c4f1d0c06f
commit 8b4b2f97e0
11 changed files with 224 additions and 67 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Sun Jun 29 10:53:22 CEST 2014
build.number=893
#Sun Jun 29 12:03:50 CEST 2014
build.number=904

View File

@ -6,7 +6,7 @@ import me.StevenLawson.TotalFreedomMod.TFM_ServerInterface;
import me.StevenLawson.TotalFreedomMod.TFM_AdminList;
import me.StevenLawson.TotalFreedomMod.TFM_Ban;
import me.StevenLawson.TotalFreedomMod.TFM_BanManager;
import me.StevenLawson.TotalFreedomMod.TFM_PlayerEntry;
import me.StevenLawson.TotalFreedomMod.TFM_Player;
import me.StevenLawson.TotalFreedomMod.TFM_PlayerList;
import me.StevenLawson.TotalFreedomMod.TFM_Util;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
@ -58,7 +58,7 @@ public class Command_glist extends TFM_Command
if (player == null)
{
final TFM_PlayerEntry entry = TFM_PlayerList.getEntry(args[1]);
final TFM_Player entry = TFM_PlayerList.getEntry(args[1]);
if (entry == null)
{
@ -66,7 +66,7 @@ public class Command_glist extends TFM_Command
return true;
}
username = entry.getLastJoinName();
username = entry.getLastLoginName();
ips.addAll(entry.getIps());
}
else

View File

@ -94,7 +94,7 @@ public class Command_rollback extends TFM_Command
if (playerName == null)
{
playerName = TFM_PlayerList.getEntry(playerNameInput).getLastJoinName();
playerName = TFM_PlayerList.getEntry(playerNameInput).getLastLoginName();
}
return playerName;

View File

@ -86,7 +86,7 @@ public class Command_saconfig extends TFM_Command
counter++;
}
TFM_AdminList.save();
TFM_AdminList.saveAll();
playerMsg(counter + " IPs removed.");
playerMsg(admin.getIps().get(0) + " is now your only IP address");
@ -109,7 +109,7 @@ public class Command_saconfig extends TFM_Command
TFM_Util.adminAction(sender.getName(), "Removing a supered IP", true);
admin.removeIp(args[1]);
TFM_AdminList.save();
TFM_AdminList.saveAll();
playerMsg("Removed IP " + args[1]);
playerMsg("Current IPs: " + StringUtils.join(admin.getIps(), ", "));

View File

@ -1,14 +1,21 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import me.StevenLawson.TotalFreedomMod.TFM_Admin;
import me.StevenLawson.TotalFreedomMod.TFM_AdminList;
import me.StevenLawson.TotalFreedomMod.TFM_Player;
import me.StevenLawson.TotalFreedomMod.TFM_PlayerList;
import me.StevenLawson.TotalFreedomMod.TFM_UuidResolver;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = AdminLevel.SENIOR, source = SourceType.ONLY_CONSOLE)
@CommandParameters(description = "Provides uuid tools", usage = "/<command> recalculate admin")
@CommandParameters(description = "Provides uuid tools", usage = "/<command> recalculate <admin | player>")
public class Command_uuid extends TFM_Command
{
@Override
@ -24,18 +31,84 @@ public class Command_uuid extends TFM_Command
if ("admin".equals(args[1]))
{
playerMsg("Recalculating admin UUIDs...");
final Set<TFM_Admin> admins = TFM_AdminList.getAllAdmins();
final List<String> names = new ArrayList<String>();
for (TFM_Admin admin : TFM_AdminList.getAllAdmins())
for (TFM_Admin admin : admins)
{
final UUID original = admin.getUniqueId();
names.add(admin.getLastLoginName());
}
final Map<String, UUID> uuids = new TFM_UuidResolver(names).call();
int updated = 0;
for (String name : uuids.keySet())
{
for (TFM_Admin admin : admins)
{
if (!admin.getLastLoginName().equalsIgnoreCase(name))
{
continue;
}
if (admin.getUniqueId().equals(uuids.get(name)))
{
continue;
}
TFM_AdminList.setUuid(admin, admin.getUniqueId(), uuids.get(name));
updated++;
break;
}
}
playerMsg("Done, recalculated " + updated + " UUIDs");
return true;
}
if ("player".equals(args[1]))
{
playerMsg("Recalculating player UUIDs...");
final Set<TFM_Player> players = TFM_PlayerList.getAllPlayers();
final List<String> names = new ArrayList<String>();
for (TFM_Player player : players)
{
names.add(player.getLastLoginName());
}
final Map<String, UUID> uuids = new TFM_UuidResolver(names).call();
int updated = 0;
for (String name : uuids.keySet())
{
for (TFM_Player player : players)
{
if (!player.getLastLoginName().equalsIgnoreCase(name))
{
continue;
}
if (player.getUniqueId().equals(uuids.get(name)))
{
continue;
}
TFM_PlayerList.setUuid(player, player.getUniqueId(), uuids.get(name));
updated++;
break;
}
}
playerMsg("Done, recalculated " + updated + " UUIDs");
return true;
}
return false;
}

View File

@ -697,9 +697,9 @@ public class TFM_PlayerListener implements Listener
if (TFM_PlayerList.existsEntry(player))
{
final TFM_PlayerEntry entry = TFM_PlayerList.getEntry(player);
entry.setLastJoinUnix(TFM_Util.getUnixTime());
entry.setLastJoinName(player.getName());
final TFM_Player entry = TFM_PlayerList.getEntry(player);
entry.setLastLoginUnix(TFM_Util.getUnixTime());
entry.setLastLoginName(player.getName());
entry.save();
}
else

View File

@ -121,7 +121,13 @@ public class TFM_AdminList
return;
}
TFM_Admin newAdmin = new TFM_Admin(
if (oldUuid.equals(newUuid))
{
TFM_Log.warning("could not set new UUID for admin " + admin.getLastLoginName() + ", UUIDs match.");
return;
}
final TFM_Admin newAdmin = new TFM_Admin(
newUuid,
admin.getLastLoginName(),
admin.getLastLogin(),
@ -130,10 +136,8 @@ public class TFM_AdminList
admin.isSeniorAdmin(),
admin.isActivated());
for (String ip : admin.getIps())
{
newAdmin.addIps(admin.getIps());
}
newAdmin.addIps(admin.getIps());
adminList.remove(oldUuid);
adminList.put(newUuid, newAdmin);
@ -143,6 +147,8 @@ public class TFM_AdminList
config.load();
config.set("admins." + oldUuid.toString(), null);
config.save();
save(newAdmin);
}
public static void load()
@ -270,7 +276,7 @@ public class TFM_AdminList
TFM_Log.info("Done! " + counter + " admins parsed, " + errors + " errors");
}
public static void save()
public static void saveAll()
{
final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true);
config.load();
@ -298,6 +304,31 @@ public class TFM_AdminList
config.save();
}
public static void save(TFM_Admin admin)
{
if (!adminList.containsValue(admin))
{
TFM_Log.warning("Could not save admin " + admin.getLastLoginName() + ", admin is not loaded!");
return;
}
final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true);
config.load();
final UUID uuid = admin.getUniqueId();
config.set("admins." + uuid + ".last_login_name", admin.getLastLoginName());
config.set("admins." + uuid + ".is_activated", admin.isActivated());
config.set("admins." + uuid + ".is_telnet_admin", admin.isTelnetAdmin());
config.set("admins." + uuid + ".is_senior_admin", admin.isSeniorAdmin());
config.set("admins." + uuid + ".last_login", TFM_Util.dateToString(admin.getLastLogin()));
config.set("admins." + uuid + ".custom_login_message", admin.getCustomLoginMessage());
config.set("admins." + uuid + ".console_aliases", TFM_Util.removeDuplicates(admin.getConsoleAliases()));
config.set("admins." + uuid + ".ips", TFM_Util.removeDuplicates(admin.getIps()));
config.save();
}
public static TFM_Admin getEntry(Player player)
{
return getEntry(TFM_Util.getUuid(player));
@ -364,7 +395,7 @@ public class TFM_AdminList
}
admin.setLastLogin(new Date());
admin.setLastLoginName(player.getName());
save();
saveAll();
}
public static boolean isSeniorAdmin(CommandSender sender)
@ -503,7 +534,7 @@ public class TFM_AdminList
{
entry.addIp(ip);
}
save();
saveAll();
}
return true;
@ -542,7 +573,7 @@ public class TFM_AdminList
superadmin.setLastLogin(new Date());
superadmin.addIp(ip);
}
save();
saveAll();
updateIndexLists();
return;
}
@ -566,7 +597,7 @@ public class TFM_AdminList
adminList.put(uuid, superadmin);
save();
saveAll();
updateIndexLists();
}
@ -585,7 +616,7 @@ public class TFM_AdminList
superadmin.setActivated(false);
Command_logs.deactivateSuperadmin(superadmin);
save();
saveAll();
updateIndexLists();
}
@ -618,7 +649,7 @@ public class TFM_AdminList
}
}
save();
saveAll();
updateIndexLists();
}
}

View File

@ -8,29 +8,42 @@ import java.util.UUID;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import org.bukkit.configuration.ConfigurationSection;
public class TFM_PlayerEntry
public class TFM_Player
{
private final UUID uuid;
private String firstJoinName;
private String lastJoinName;
private long firstJoinUnix;
private long lastJoinUnix;
private long firstLoginUnix;
private long lastLoginUnix;
private final List<String> ips;
protected TFM_PlayerEntry(UUID uuid, ConfigurationSection section)
protected TFM_Player(UUID uuid, ConfigurationSection section)
{
this(uuid);
this.firstJoinName = section.getString("firstjoinname");
this.lastJoinName = section.getString("lastjoinname");
this.firstJoinUnix = section.getLong("firstjoinunix");
this.lastJoinUnix = section.getLong("lastjoinunix");
this.firstLoginUnix = section.getLong("firstjoinunix");
this.lastLoginUnix = section.getLong("lastjoinunix");
this.ips.addAll(section.getStringList("ips"));
}
protected TFM_PlayerEntry(UUID uuid)
protected TFM_Player(UUID uuid, String firstJoinName, String lastJoinName, long firstJoinUnix, long lastJoinUnix, List<String> ips)
{
this(uuid);
this.firstJoinName = firstJoinName;
this.lastJoinName = lastJoinName;
this.firstLoginUnix = firstJoinUnix;
this.lastLoginUnix = lastJoinUnix;
this.ips.addAll(ips);
}
protected TFM_Player(UUID uuid)
{
this.uuid = uuid;
this.ips = new ArrayList<String>();
@ -47,44 +60,44 @@ public class TFM_PlayerEntry
return Collections.unmodifiableList(ips);
}
public String getFirstJoinName()
public String getFirstLoginName()
{
return firstJoinName;
}
public void setFirstJoinName(String firstJoinName)
public void setFirstLoginName(String firstJoinName)
{
this.firstJoinName = firstJoinName;
}
public String getLastJoinName()
public String getLastLoginName()
{
return lastJoinName;
}
public void setLastJoinName(String lastJoinName)
public void setLastLoginName(String lastJoinName)
{
this.lastJoinName = lastJoinName;
}
public long getFirstJoinUnix()
public long getFirstLoginUnix()
{
return firstJoinUnix;
return firstLoginUnix;
}
public void setFirstJoinUnix(long firstJoinUnix)
public void setFirstLoginUnix(long firstJoinUnix)
{
this.firstJoinUnix = firstJoinUnix;
this.firstLoginUnix = firstJoinUnix;
}
public long getLastJoinUnix()
public long getLastLoginUnix()
{
return lastJoinUnix;
return lastLoginUnix;
}
public void setLastJoinUnix(long lastJoinUnix)
public void setLastLoginUnix(long lastJoinUnix)
{
this.lastJoinUnix = lastJoinUnix;
this.lastLoginUnix = lastJoinUnix;
}
public boolean addIp(String ip)
@ -101,8 +114,8 @@ public class TFM_PlayerEntry
{
return firstJoinName != null
&& lastJoinName != null
&& firstJoinUnix != 0
&& lastJoinUnix != 0
&& firstLoginUnix != 0
&& lastLoginUnix != 0
&& !ips.isEmpty();
}
@ -127,8 +140,8 @@ public class TFM_PlayerEntry
section.set("firstjoinname", firstJoinName);
section.set("lastjoinname", lastJoinName);
section.set("firstjoinunix", firstJoinUnix);
section.set("lastjoinunix", lastJoinUnix);
section.set("firstjoinunix", firstLoginUnix);
section.set("lastjoinunix", lastLoginUnix);
section.set("ips", ips);
config.save();

View File

@ -3,18 +3,21 @@ package me.StevenLawson.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.Config.TFM_Config;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static me.StevenLawson.TotalFreedomMod.TFM_AdminList.save;
import net.minecraft.util.com.google.common.collect.Sets;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class TFM_PlayerList
{
private static final Map<UUID, TFM_PlayerEntry> playerList;
private static final Map<UUID, TFM_Player> playerList;
private final static TFM_Config config;
static
{
playerList = new HashMap<UUID, TFM_PlayerEntry>();
playerList = new HashMap<UUID, TFM_Player>();
config = new TFM_Config(TotalFreedomMod.plugin, "playerlist.yml", false);
}
@ -23,6 +26,11 @@ public class TFM_PlayerList
throw new AssertionError();
}
public static Set<TFM_Player> getAllPlayers()
{
return Sets.newHashSet(playerList.values());
}
public static TFM_Config getConfig()
{
return config;
@ -44,7 +52,7 @@ public class TFM_PlayerList
final UUID uuid = UUID.fromString(uuidString);
final TFM_PlayerEntry entry = new TFM_PlayerEntry(uuid, config.getConfigurationSection(uuidString));
final TFM_Player entry = new TFM_Player(uuid, config.getConfigurationSection(uuidString));
if (!entry.isComplete())
{
@ -70,18 +78,18 @@ public class TFM_PlayerList
private static void saveAll()
{
// Put entries
for (TFM_PlayerEntry entry : playerList.values())
for (TFM_Player entry : playerList.values())
{
entry.save();
}
}
public static TFM_PlayerEntry getEntry(String player)
public static TFM_Player getEntry(String player)
{
for (TFM_PlayerEntry entry : playerList.values())
for (TFM_Player entry : playerList.values())
{
if (entry.getLastJoinName().equalsIgnoreCase(player))
if (entry.getLastLoginName().equalsIgnoreCase(player))
{
return entry;
}
@ -90,7 +98,7 @@ public class TFM_PlayerList
return null;
}
public static TFM_PlayerEntry getEntry(UUID uuid)
public static TFM_Player getEntry(UUID uuid)
{
return playerList.get(uuid);
}
@ -100,7 +108,7 @@ public class TFM_PlayerList
return playerList.containsKey(TFM_Util.getUuid(player));
}
public static TFM_PlayerEntry getEntry(Player player)
public static TFM_Player getEntry(Player player)
{
final UUID uuid = TFM_Util.getUuid(player);
@ -109,14 +117,14 @@ public class TFM_PlayerList
return playerList.get(uuid);
}
final TFM_PlayerEntry entry = new TFM_PlayerEntry(uuid);
final TFM_Player entry = new TFM_Player(uuid);
entry.setFirstJoinName(player.getName());
entry.setLastJoinName(player.getName());
entry.setFirstLoginName(player.getName());
entry.setLastLoginName(player.getName());
final long unix = TFM_Util.getUnixTime();
entry.setFirstJoinUnix(unix);
entry.setLastJoinUnix(unix);
entry.setFirstLoginUnix(unix);
entry.setLastLoginUnix(unix);
entry.addIp(TFM_Util.getIp(player));
@ -126,6 +134,38 @@ public class TFM_PlayerList
return entry;
}
public static void setUuid(TFM_Player player, UUID oldUuid, UUID newUuid)
{
if (!playerList.containsKey(oldUuid))
{
TFM_Log.warning("Could not set new UUID for player " + player.getLastLoginName() + ", player is not loaded!");
return;
}
if (oldUuid.equals(newUuid))
{
TFM_Log.warning("could not set new UUID for player " + player.getLastLoginName() + ", UUIDs match.");
return;
}
final TFM_Player newPlayer = new TFM_Player(
newUuid,
player.getFirstLoginName(),
player.getLastLoginName(),
player.getFirstLoginUnix(),
player.getLastLoginUnix(),
player.getIps());
playerList.remove(oldUuid);
playerList.put(newUuid, newPlayer);
final TFM_Config config = getConfig();
config.set(oldUuid.toString(), null);
config.save();
newPlayer.save();
}
public static void purgeAll()
{
// Clear the config entries

View File

@ -70,7 +70,7 @@ public class TFM_ServerInterface
final Player player = event.getPlayer();
final String username = player.getName();
final UUID uuid = TFM_Util.getUuid(player);
final UUID uuid = TFM_UuidResolver.getUUIDOf(username);
final String ip = event.getAddress().getHostAddress().trim();
if (username.length() < 3 || username.length() > 20)

View File

@ -163,7 +163,7 @@ public class TFM_Util
return ((Player) player).getAddress().getAddress().getHostAddress().trim();
}
final TFM_PlayerEntry entry = TFM_PlayerList.getEntry(TFM_Util.getUuid(player));
final TFM_Player entry = TFM_PlayerList.getEntry(TFM_Util.getUuid(player));
if (entry == null)
{