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

147 lines
3.3 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;
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;
2012-01-16 01:12:20 +00:00
public class UserMap extends CacheLoader<String, User> implements IConf
{
private final transient IEssentials ess;
private final transient Cache<String, User> users;
2012-01-16 01:12:20 +00:00
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;
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();
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);
2013-06-08 21:31:19 +00:00
keys.add(StringUtil.sanitizeFileName(name));
}
}
});
}
public boolean userExists(final String name)
{
2013-06-08 21:31:19 +00:00
return keys.contains(StringUtil.sanitizeFileName(name));
}
public User getUser(final String name)
{
2011-11-18 04:29:27 +00:00
try
{
2014-02-27 01:55:04 +00:00
String sanitizedName = StringUtil.sanitizeFileName(name);
return users.get(sanitizedName);
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
{
2013-06-08 21:31:19 +00:00
String sanitizedName = StringUtil.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);
2014-02-27 01:55:04 +00:00
return new User(new OfflinePlayer(sanitizedName, 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)
{
2013-06-08 21:31:19 +00:00
keys.remove(StringUtil.sanitizeFileName(name));
users.invalidate(StringUtil.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
{
2013-06-08 21:31:19 +00:00
return getUserFile2(StringUtil.sanitizeFileName(name));
2012-02-13 20:32:05 +00:00
}
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
}
}