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

143 lines
3 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
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;
2012-01-16 01:12:20 +00:00
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import org.bukkit.entity.Player;
2012-01-16 01:12:20 +00:00
public class UserMap extends CacheLoader<String, User> implements IConf
{
private final transient IEssentials ess;
2012-01-16 01:12:20 +00:00
private final transient Cache<String, User> users = CacheBuilder.newBuilder().softValues().build(this);
private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>();
public UserMap(final IEssentials ess)
{
2011-11-18 04:29:27 +00:00
super();
this.ess = ess;
loadAllUsersAsync(ess);
}
private void loadAllUsersAsync(final IEssentials ess)
{
ess.scheduleAsyncDelayedTask(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();
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);
keys.add(Util.sanitizeFileName(name));
}
}
});
}
public boolean userExists(final String name)
{
2012-01-16 01:12:20 +00:00
return keys.contains(Util.sanitizeFileName(name));
}
public User getUser(final String name)
{
2011-11-18 04:29:27 +00:00
try
{
2012-02-13 20:32:05 +00:00
return users.get(name);
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
}
}
2012-01-16 01:12:20 +00:00
@Override
2011-11-18 04:29:27 +00:00
public User load(final String name) throws Exception
{
2012-02-13 20:32:05 +00:00
String sanitizedName = Util.sanitizeFileName(name);
2012-02-14 19:29:45 +00:00
if (!sanitizedName.equals(name))
{
User user = getUser(sanitizedName);
if (user == null)
{
throw new Exception("User not found!");
}
else
{
return user;
}
2012-02-13 20:32:05 +00:00
}
for (Player player : ess.getServer().getOnlinePlayers())
{
if (player.getName().equalsIgnoreCase(name))
{
2012-02-13 20:32:05 +00:00
keys.add(sanitizedName);
return new User(player, ess);
}
}
2012-02-13 20:32:05 +00:00
final File userFile = getUserFile2(sanitizedName);
if (userFile.exists())
{
2012-02-13 20:32:05 +00:00
keys.add(sanitizedName);
return new User(new OfflinePlayer(name, ess), ess);
}
2011-11-18 04:29:27 +00:00
throw new Exception("User not found!");
}
@Override
public void reloadConfig()
{
2011-11-18 04:29:27 +00:00
loadAllUsersAsync(ess);
}
public void removeUser(final String name)
{
2012-01-16 01:12:20 +00:00
keys.remove(Util.sanitizeFileName(name));
users.invalidate(Util.sanitizeFileName(name));
2012-02-13 20:32:05 +00:00
users.invalidate(name);
}
public Set<String> 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
2011-12-06 09:37:17 +00:00
public File getUserFile(final String name)
2012-02-13 20:32:05 +00:00
{
return getUserFile2(Util.sanitizeFileName(name));
}
2012-02-14 19:29:45 +00:00
2012-02-13 20:32:05 +00:00
private File getUserFile2(final String name)
2011-12-06 09:37:17 +00:00
{
final File userFolder = new File(ess.getDataFolder(), "userdata");
2012-02-13 20:32:05 +00:00
return new File(userFolder, name + ".yml");
2011-12-06 09:37:17 +00:00
}
}