2011-08-08 12:40:30 +00:00
|
|
|
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;
|
2011-08-08 12:40:30 +00:00
|
|
|
import java.io.File;
|
2014-04-18 03:48:34 +00:00
|
|
|
import java.util.ArrayList;
|
2012-01-16 01:12:20 +00:00
|
|
|
import java.util.Collections;
|
2014-04-18 03:48:34 +00:00
|
|
|
import java.util.List;
|
2011-08-08 12:40:30 +00:00
|
|
|
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;
|
2014-04-18 04:57:47 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2011-08-08 12:40:30 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
|
2014-04-13 05:53:11 +00:00
|
|
|
public class UserMap extends CacheLoader<UUID, User> implements IConf
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
|
|
|
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>();
|
2014-04-18 03:48:34 +00:00
|
|
|
private final transient ConcurrentSkipListMap<UUID, ArrayList<String>> history = new ConcurrentSkipListMap<UUID, ArrayList<String>>();
|
2014-04-17 03:53:02 +00:00
|
|
|
private UUIDMap uuidMap;
|
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;
|
2014-04-17 03:53:02 +00:00
|
|
|
uuidMap = new UUIDMap(ess);
|
2014-04-13 05:53:11 +00:00
|
|
|
users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2014-04-18 04:57:47 +00:00
|
|
|
synchronized (users)
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-04-18 04:57:47 +00:00
|
|
|
final File userdir = new File(ess.getDataFolder(), "userdata");
|
|
|
|
if (!userdir.exists())
|
2014-04-13 05:53:11 +00:00
|
|
|
{
|
2014-04-18 04:57:47 +00:00
|
|
|
return;
|
2014-04-13 05:53:11 +00:00
|
|
|
}
|
2014-04-18 04:57:47 +00:00
|
|
|
keys.clear();
|
|
|
|
users.invalidateAll();
|
|
|
|
for (String string : userdir.list())
|
2014-04-13 05:53:11 +00:00
|
|
|
{
|
2014-04-18 04:57:47 +00:00
|
|
|
if (!string.endsWith(".yml"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String name = string.substring(0, string.length() - 4);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
keys.add(UUID.fromString(name));
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
|
|
|
//Ignore these users till they rejoin.
|
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
}
|
2014-04-18 04:57:47 +00:00
|
|
|
uuidMap.loadAllUsers(names, history);
|
2014-04-13 05:53:11 +00:00
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-04-13 05:53:11 +00:00
|
|
|
public boolean userExists(final UUID uuid)
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-04-13 05:53:11 +00:00
|
|
|
return keys.contains(uuid);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 23:36:36 +00:00
|
|
|
public User getUser(final String name)
|
2014-04-13 05:53:11 +00:00
|
|
|
{
|
|
|
|
try
|
2014-04-14 23:07:35 +00:00
|
|
|
{
|
2014-05-05 21:36:16 +00:00
|
|
|
final String sanitizedName = StringUtil.safeString(name);
|
2014-04-13 05:53:11 +00:00
|
|
|
if (names.containsKey(sanitizedName))
|
|
|
|
{
|
|
|
|
final UUID uuid = names.get(sanitizedName);
|
|
|
|
return users.get(uuid);
|
|
|
|
}
|
2014-04-14 23:07:35 +00:00
|
|
|
|
|
|
|
for (Player player : ess.getServer().getOnlinePlayers())
|
|
|
|
{
|
2014-05-05 21:36:16 +00:00
|
|
|
String sanitizedPlayer = StringUtil.safeString(player.getName());
|
2014-04-14 23:07:35 +00:00
|
|
|
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-08-08 12:40:30 +00:00
|
|
|
{
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2014-04-13 05:53:11 +00:00
|
|
|
public void trackUUID(final UUID uuid, final String name)
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-04-14 23:07:35 +00:00
|
|
|
if (uuid != null)
|
|
|
|
{
|
|
|
|
keys.add(uuid);
|
2014-04-17 03:53:02 +00:00
|
|
|
if (name != null && name.length() > 0)
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-05-05 21:36:16 +00:00
|
|
|
final String keyName = StringUtil.safeString(name);
|
2014-04-27 00:35:52 +00:00
|
|
|
if (!names.containsKey(keyName) || !names.get(keyName).equals(uuid))
|
2014-04-18 05:46:46 +00:00
|
|
|
{
|
|
|
|
names.put(keyName, uuid);
|
|
|
|
uuidMap.writeUUIDMap();
|
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2014-04-17 03:53:02 +00:00
|
|
|
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);
|
|
|
|
|
2011-08-08 12:40:30 +00:00
|
|
|
if (userFile.exists())
|
|
|
|
{
|
2014-04-18 04:57:47 +00:00
|
|
|
player = new OfflinePlayer(uuid, ess.getServer());
|
2014-04-17 05:09:08 +00:00
|
|
|
final User user = new User(player, ess);
|
|
|
|
((OfflinePlayer)player).setName(user.getLastAccountName());
|
2014-04-17 03:53:02 +00:00
|
|
|
trackUUID(uuid, user.getName());
|
|
|
|
return user;
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
|
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()
|
|
|
|
{
|
2014-04-17 03:53:02 +00:00
|
|
|
getUUIDMap().forceWriteUUIDMap();
|
2011-11-18 04:29:27 +00:00
|
|
|
loadAllUsersAsync(ess);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeUser(final String name)
|
|
|
|
{
|
2014-04-13 05:53:11 +00:00
|
|
|
UUID uuid = names.get(name);
|
2014-04-14 23:07:35 +00:00
|
|
|
if (uuid != null)
|
|
|
|
{
|
|
|
|
keys.remove(uuid);
|
|
|
|
users.invalidate(uuid);
|
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
names.remove(name);
|
2014-05-05 21:36:16 +00:00
|
|
|
names.remove(StringUtil.safeString(name));
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 05:53:11 +00:00
|
|
|
public Set<UUID> getAllUniqueUsers()
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-05-17 00:50:41 +00:00
|
|
|
return Collections.unmodifiableSet(keys.clone());
|
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
|
|
|
|
2014-05-17 00:50:41 +00:00
|
|
|
protected ConcurrentSkipListMap<String, UUID> getNames()
|
2014-04-17 03:53:02 +00:00
|
|
|
{
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:50:41 +00:00
|
|
|
protected ConcurrentSkipListMap<UUID, ArrayList<String>> getHistory()
|
2014-04-18 03:48:34 +00:00
|
|
|
{
|
|
|
|
return history;
|
|
|
|
}
|
2014-04-18 04:57:47 +00:00
|
|
|
|
2014-04-18 03:48:34 +00:00
|
|
|
public List<String> getUserHistory(final UUID uuid)
|
|
|
|
{
|
|
|
|
return history.get(uuid);
|
|
|
|
}
|
|
|
|
|
2014-04-17 03:53:02 +00:00
|
|
|
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
|
|
|
}
|
2014-04-14 23:07:35 +00:00
|
|
|
|
|
|
|
public File getUserFileFromString(final String name)
|
|
|
|
{
|
|
|
|
final File userFolder = new File(ess.getDataFolder(), "userdata");
|
|
|
|
return new File(userFolder, StringUtil.sanitizeFileName(name) + ".yml");
|
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|