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;
|
2014-04-13 05:53:11 +00:00
|
|
|
import com.google.common.io.Files;
|
2012-01-16 01:12:20 +00:00
|
|
|
import com.google.common.util.concurrent.UncheckedExecutionException;
|
2014-04-13 05:53:11 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
2011-08-08 12:40:30 +00:00
|
|
|
import java.io.File;
|
2014-04-13 05:53:11 +00:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
2012-01-16 01:12:20 +00:00
|
|
|
import java.util.Collections;
|
2014-04-13 05:53:11 +00:00
|
|
|
import java.util.Map;
|
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;
|
2014-04-13 05:53:11 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import java.util.regex.Pattern;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2014-04-13 05:53:11 +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>();
|
|
|
|
private final transient Pattern splitPattern = Pattern.compile(",");
|
|
|
|
private File userList;
|
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-13 05:53:11 +00:00
|
|
|
userList = new File(ess.getDataFolder(), "usermap.csv");
|
|
|
|
users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this);
|
2011-08-08 12:40:30 +00:00
|
|
|
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();
|
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())
|
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);
|
2014-04-13 05:53:11 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
keys.add(UUID.fromString(name));
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
|
|
|
//Ignore these users till they rejoin.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!userList.exists())
|
|
|
|
{
|
|
|
|
userList.createNewFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
final BufferedReader reader = new BufferedReader(new FileReader(userList));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
final String line = reader.readLine();
|
|
|
|
if (line == null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String[] values = splitPattern.split(line);
|
|
|
|
if (values.length == 2)
|
|
|
|
{
|
|
|
|
names.put(values[0], UUID.fromString(values[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (true);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
reader.close();
|
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
final String sanitizedName = StringUtil.sanitizeFileName(name);
|
|
|
|
if (names.containsKey(sanitizedName))
|
|
|
|
{
|
|
|
|
ess.getLogger().info("Trying to grab user via string, found matching key.");
|
|
|
|
final UUID uuid = names.get(sanitizedName);
|
|
|
|
return users.get(uuid);
|
|
|
|
}
|
|
|
|
ess.getLogger().info("Trying to grab user via string, failed, returning null.");
|
|
|
|
return null; // TODO: THIS IS PROBABLY NOT RIGHT
|
|
|
|
}
|
|
|
|
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-13 05:53:11 +00:00
|
|
|
names.put(StringUtil.sanitizeFileName(name), uuid);
|
|
|
|
writeUUIDMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeUUIDMap()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final File tempFile = File.createTempFile("usermap", ".tmp.yml", ess.getDataFolder());
|
|
|
|
final BufferedWriter bWriter = new BufferedWriter(new FileWriter(tempFile));
|
|
|
|
|
|
|
|
for (Map.Entry<String, UUID> entry : names.entrySet())
|
2011-08-08 12:40:30 +00:00
|
|
|
{
|
2014-04-13 05:53:11 +00:00
|
|
|
bWriter.write(entry.getKey() + "," + entry.getValue().toString());
|
|
|
|
bWriter.newLine();
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
|
|
|
|
bWriter.close();
|
|
|
|
Files.move(tempFile, userList);
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|
2014-04-13 05:53:11 +00:00
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
Logger.getLogger(UserMap.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public User load(final UUID uuid) throws Exception
|
|
|
|
{
|
|
|
|
Player player = ess.getServer().getPlayer(uuid);
|
|
|
|
if (player != null)
|
|
|
|
{
|
|
|
|
return new User(player, ess);
|
|
|
|
}
|
|
|
|
|
|
|
|
final File userFile = getUserFileFromID(uuid);
|
|
|
|
|
2011-08-08 12:40:30 +00:00
|
|
|
if (userFile.exists())
|
|
|
|
{
|
2014-04-13 05:53:11 +00:00
|
|
|
keys.add(uuid);
|
2014-04-13 23:03:07 +00:00
|
|
|
return new User(new OfflinePlayer(uuid, ess.getServer()), ess);
|
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()
|
|
|
|
{
|
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);
|
|
|
|
keys.remove(uuid);
|
|
|
|
users.invalidate(uuid);
|
|
|
|
names.remove(name);
|
|
|
|
names.remove(StringUtil.sanitizeFileName(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
|
|
|
{
|
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
|
|
|
|
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
|
|
|
}
|
2011-08-08 12:40:30 +00:00
|
|
|
}
|