2011-08-08 12:40:30 +00:00
|
|
|
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;
|
2011-08-08 12:40:30 +00:00
|
|
|
import java.io.File;
|
2012-01-16 01:12:20 +00:00
|
|
|
import java.util.Collections;
|
2011-08-08 12:40:30 +00:00
|
|
|
import java.util.Set;
|
2012-01-16 01:12:20 +00:00
|
|
|
import java.util.concurrent.ConcurrentSkipListSet;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2011-08-08 12:40:30 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
|
2012-01-16 01:12:20 +00:00
|
|
|
public class UserMap extends CacheLoader<String, User> implements IConf
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
|
|
|
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>();
|
2011-08-08 12:40:30 +00:00
|
|
|
|
|
|
|
public UserMap(final IEssentials ess)
|
|
|
|
{
|
2011-11-18 04:29:27 +00:00
|
|
|
super();
|
2011-08-08 12:40:30 +00:00
|
|
|
this.ess = ess;
|
|
|
|
loadAllUsersAsync(ess);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadAllUsersAsync(final IEssentials ess)
|
|
|
|
{
|
2013-01-31 19:16:09 +00:00
|
|
|
ess.runTaskAsynchronously(new Runnable()
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
|
|
|
@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())
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2012-01-16 01:12:20 +00:00
|
|
|
if (!string.endsWith(".yml"))
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2012-01-16 01:12:20 +00:00
|
|
|
continue;
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
2012-01-16 01:12:20 +00:00
|
|
|
final String name = string.substring(0, string.length() - 4);
|
|
|
|
keys.add(Util.sanitizeFileName(name));
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean userExists(final String name)
|
|
|
|
{
|
2012-01-16 01:12:20 +00:00
|
|
|
return keys.contains(Util.sanitizeFileName(name));
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 23:36:36 +00:00
|
|
|
public User getUser(final String name)
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
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
|
|
|
{
|
2011-11-28 23:36:36 +00:00
|
|
|
return null;
|
2011-11-18 04:29:27 +00:00
|
|
|
}
|
2011-08-08 12:40:30 +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
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
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
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
for (Player player : ess.getServer().getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (player.getName().equalsIgnoreCase(name))
|
|
|
|
{
|
2012-02-13 20:32:05 +00:00
|
|
|
keys.add(sanitizedName);
|
2011-08-08 12:40:30 +00:00
|
|
|
return new User(player, ess);
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 20:32:05 +00:00
|
|
|
final File userFile = getUserFile2(sanitizedName);
|
2011-08-08 12:40:30 +00:00
|
|
|
if (userFile.exists())
|
|
|
|
{
|
2012-02-13 20:32:05 +00:00
|
|
|
keys.add(sanitizedName);
|
2011-08-08 12:40:30 +00:00
|
|
|
return new User(new OfflinePlayer(name, ess), ess);
|
|
|
|
}
|
2011-11-18 04:29:27 +00:00
|
|
|
throw new Exception("User not found!");
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reloadConfig()
|
|
|
|
{
|
2011-11-18 04:29:27 +00:00
|
|
|
loadAllUsersAsync(ess);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 17:53:38 +00:00
|
|
|
public Set<String> getAllUniqueUsers()
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2012-01-16 01:12:20 +00:00
|
|
|
return Collections.unmodifiableSet(keys);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
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
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|