TF-EssentialsX/Essentials/src/com/earth2me/essentials/UserMap.java

225 lines
5 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.StringUtil;
2012-01-16 01:12:20 +00:00
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.File;
2012-01-16 01:12:20 +00:00
import java.util.Collections;
import java.util.Set;
2014-04-13 05:53:11 +00:00
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListMap;
2012-01-16 01:12:20 +00:00
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
2013-10-11 02:44:41 +00:00
import net.ess3.api.IEssentials;
import org.bukkit.entity.Player;
2014-04-13 05:53:11 +00:00
public class UserMap extends CacheLoader<UUID, User> implements IConf
{
private final transient IEssentials ess;
2014-04-13 05:53:11 +00:00
private final transient Cache<UUID, User> users;
private final transient ConcurrentSkipListSet<UUID> keys = new ConcurrentSkipListSet<UUID>();
private final transient ConcurrentSkipListMap<String, UUID> names = new ConcurrentSkipListMap<String, UUID>();
private UUIDMap uuidMap;
public UserMap(final IEssentials ess)
{
2011-11-18 04:29:27 +00:00
super();
this.ess = ess;
uuidMap = new UUIDMap(ess);
2014-04-13 05:53:11 +00:00
users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this);
loadAllUsersAsync(ess);
}
private void loadAllUsersAsync(final IEssentials ess)
{
ess.runTaskAsynchronously(new Runnable()
{
@Override
public void run()
{
final File userdir = new File(ess.getDataFolder(), "userdata");
if (!userdir.exists())
{
return;
}
2012-01-16 01:12:20 +00:00
keys.clear();
2014-04-13 05:53:11 +00:00
names.clear();
2012-01-16 01:12:20 +00:00
users.invalidateAll();
for (String string : userdir.list())
{
2012-01-16 01:12:20 +00:00
if (!string.endsWith(".yml"))
{
2012-01-16 01:12:20 +00:00
continue;
}
2012-01-16 01:12:20 +00:00
final String name = string.substring(0, string.length() - 4);
2014-04-13 05:53:11 +00:00
try
{
keys.add(UUID.fromString(name));
}
catch (IllegalArgumentException ex)
{
//Ignore these users till they rejoin.
}
}
uuidMap.loadAllUsers(names);
2014-04-13 05:53:11 +00:00
}
});
}
2014-04-13 05:53:11 +00:00
public boolean userExists(final UUID uuid)
{
2014-04-13 05:53:11 +00:00
return keys.contains(uuid);
}
public User getUser(final String name)
2014-04-13 05:53:11 +00:00
{
try
{
2014-04-13 05:53:11 +00:00
final String sanitizedName = StringUtil.sanitizeFileName(name);
if (names.containsKey(sanitizedName))
{
final UUID uuid = names.get(sanitizedName);
return users.get(uuid);
}
for (Player player : ess.getServer().getOnlinePlayers())
{
String sanitizedPlayer = StringUtil.sanitizeFileName(player.getName());
if (sanitizedPlayer.equalsIgnoreCase(sanitizedName))
{
User user = new User(player, ess);
trackUUID(user.getBase().getUniqueId(), user.getName());
return new User(player, ess);
}
}
final File userFile = getUserFileFromString(sanitizedName);
if (userFile.exists())
{
User user = new User(new OfflinePlayer(sanitizedName, ess.getServer()), ess);
trackUUID(user.getBase().getUniqueId(), user.getName());
return user;
}
return null;
2014-04-13 05:53:11 +00:00
}
catch (ExecutionException ex)
{
return null;
}
catch (UncheckedExecutionException ex)
{
return null;
}
}
public User getUser(final UUID uuid)
{
2011-11-18 04:29:27 +00:00
try
{
2014-04-13 05:53:11 +00:00
return users.get(uuid);
2012-01-16 01:12:20 +00:00
}
catch (ExecutionException ex)
{
return null;
2011-11-28 23:30:06 +00:00
}
2012-01-16 01:12:20 +00:00
catch (UncheckedExecutionException ex)
2011-11-28 23:30:06 +00:00
{
return null;
2011-11-18 04:29:27 +00:00
}
}
2014-04-13 05:53:11 +00:00
public void trackUUID(final UUID uuid, final String name)
{
if (uuid != null)
{
keys.add(uuid);
if (name != null && name.length() > 0)
{
names.put(StringUtil.sanitizeFileName(name), uuid);
uuidMap.writeUUIDMap();
}
2014-04-13 05:53:11 +00:00
}
}
@Override
public User load(final UUID uuid) throws Exception
{
Player player = ess.getServer().getPlayer(uuid);
if (player != null)
{
final User user = new User(player, ess);
trackUUID(uuid, user.getName());
return user;
2014-04-13 05:53:11 +00:00
}
final File userFile = getUserFileFromID(uuid);
if (userFile.exists())
{
2014-04-17 05:09:08 +00:00
player = new OfflinePlayer(uuid, ess.getServer());
final User user = new User(player, ess);
((OfflinePlayer)player).setName(user.getLastAccountName());
trackUUID(uuid, user.getName());
return user;
}
2014-04-13 05:53:11 +00:00
2011-11-18 04:29:27 +00:00
throw new Exception("User not found!");
}
@Override
public void reloadConfig()
{
getUUIDMap().forceWriteUUIDMap();
2011-11-18 04:29:27 +00:00
loadAllUsersAsync(ess);
}
public void removeUser(final String name)
{
2014-04-13 05:53:11 +00:00
UUID uuid = names.get(name);
if (uuid != null)
{
keys.remove(uuid);
users.invalidate(uuid);
}
2014-04-13 05:53:11 +00:00
names.remove(name);
names.remove(StringUtil.sanitizeFileName(name));
}
2014-04-13 05:53:11 +00:00
public Set<UUID> getAllUniqueUsers()
{
2012-01-16 01:12:20 +00:00
return Collections.unmodifiableSet(keys);
}
2011-08-19 00:32:34 +00:00
public int getUniqueUsers()
{
2012-01-16 01:12:20 +00:00
return keys.size();
2011-08-19 00:32:34 +00:00
}
2012-02-14 19:29:45 +00:00
public ConcurrentSkipListMap<String, UUID> getNames()
{
return names;
}
public UUIDMap getUUIDMap()
{
return uuidMap;
}
2014-04-13 05:53:11 +00:00
private File getUserFileFromID(final UUID uuid)
2011-12-06 09:37:17 +00:00
{
final File userFolder = new File(ess.getDataFolder(), "userdata");
2014-04-13 05:53:11 +00:00
return new File(userFolder, uuid.toString() + ".yml");
2011-12-06 09:37:17 +00:00
}
public File getUserFileFromString(final String name)
{
final File userFolder = new File(ess.getDataFolder(), "userdata");
return new File(userFolder, StringUtil.sanitizeFileName(name) + ".yml");
}
}