summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/UserMap.java
blob: dc6d310e6541be4c5bda946d3c94623a0252a321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.earth2me.essentials;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ConcurrentHashMultiset;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;


public class UserMap extends CacheLoader<String, User> implements IConf
{
	private final transient IEssentials ess;
	private final transient Cache<String, User> users = CacheBuilder.newBuilder().softValues().build(this);
	private final transient ConcurrentHashMultiset<String> keys = ConcurrentHashMultiset.create();

	public UserMap(final IEssentials ess)
	{
		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;
				}
				keys.clear();
				users.invalidateAll();
				for (String string : userdir.list())
				{
					if (!string.endsWith(".yml"))
					{
						continue;
					}
					final String name = string.substring(0, string.length() - 4);
					keys.add(name.toLowerCase());
				}
			}
		});
	}

	public boolean userExists(final String name)
	{
		return keys.contains(name.toLowerCase());
	}

	public User getUser(final String name) throws NullPointerException
	{
		try
		{
			return users.get(name.toLowerCase());
		}
		catch (ExecutionException ex)
		{
			throw new NullPointerException();
		}
	}

	@Override
	public User load(final String name) throws Exception
	{
		for (Player player : ess.getServer().getOnlinePlayers())
		{
			if (player.getName().equalsIgnoreCase(name))
			{
				keys.add(name.toLowerCase());
				return new User(player, ess);
			}
		}
		final File userFolder = new File(ess.getDataFolder(), "userdata");
		final File userFile = new File(userFolder, Util.sanitizeFileName(name) + ".yml");
		if (userFile.exists())
		{
			keys.add(name.toLowerCase());
			return new User(new OfflinePlayer(name, ess), ess);
		}
		throw new Exception("User not found!");
	}

	@Override
	public void reloadConfig()
	{
		loadAllUsersAsync(ess);
	}

	public void removeUser(final String name)
	{
		keys.remove(name.toLowerCase());
		users.invalidate(name.toLowerCase());
	}

	public Set<User> getAllUsers()
	{
		final Set<User> userSet = new HashSet<User>();
		for (String name : keys)
		{
			try
			{
				userSet.add(users.get(name));
			}
			catch (ExecutionException ex)
			{
				Bukkit.getLogger().log(Level.INFO, "Failed to load user " + name, ex);
			}
		}
		return userSet;
	}

	public int getUniqueUsers()
	{
		return keys.size();
	}
}