summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/UserMap.java
blob: ee34491a275ba9ceb8dc569c980414af389d27c5 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.earth2me.essentials;

import com.earth2me.essentials.utils.StringUtil;
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;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import net.ess3.api.IEssentials;
import org.bukkit.entity.Player;


public class UserMap extends CacheLoader<UUID, User> implements IConf
{
	private final transient IEssentials ess;
	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 UUIDMap uuidMap;

	public UserMap(final IEssentials ess)
	{
		super();
		this.ess = ess;
		uuidMap = new UUIDMap(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;
				}
				keys.clear();
				names.clear();
				users.invalidateAll();
				for (String string : userdir.list())
				{
					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.
					}
				}

				uuidMap.loadAllUsers(names);

			}
		});
	}

	public boolean userExists(final UUID uuid)
	{
		return keys.contains(uuid);
	}

	public User getUser(final String name)
	{
		try
		{
			final String sanitizedName = StringUtil.sanitizeFileName(name);
			if (names.containsKey(sanitizedName))
			{
				final UUID uuid = names.get(sanitizedName);
				return users.get(uuid);
			}

			for (Player player : ess.getServer().getOnlinePlayers())
			{
				String sanitizedPlayer = StringUtil.sanitizeFileName(player.getName());
				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;
		}
		catch (ExecutionException ex)
		{
			return null;
		}
		catch (UncheckedExecutionException ex)
		{
			return null;
		}
	}

	public User getUser(final UUID uuid)
	{
		try
		{
			return users.get(uuid);
		}
		catch (ExecutionException ex)
		{
			return null;
		}
		catch (UncheckedExecutionException ex)
		{
			return null;
		}
	}

	public void trackUUID(final UUID uuid, final String name)
	{
		if (uuid != null)
		{
			keys.add(uuid);
			if (name != null && name.length() > 0)
			{
				names.put(StringUtil.sanitizeFileName(name), uuid);
				uuidMap.writeUUIDMap();
			}
		}
	}

	@Override
	public User load(final UUID uuid) throws Exception
	{
		Player player = ess.getServer().getPlayer(uuid);
		if (player != null)
		{
			final User user = new User(player, ess);
			trackUUID(uuid, user.getName());
			return user;
		}

		final File userFile = getUserFileFromID(uuid);

		if (userFile.exists())
		{
			final User user = new User(new OfflinePlayer(uuid, ess.getServer()), ess);
			trackUUID(uuid, user.getName());
			return user;
		}

		throw new Exception("User not found!");
	}

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

	public void removeUser(final String name)
	{
		UUID uuid = names.get(name);
		if (uuid != null)
		{
			keys.remove(uuid);
			users.invalidate(uuid);
		}
		names.remove(name);
		names.remove(StringUtil.sanitizeFileName(name));
	}

	public Set<UUID> getAllUniqueUsers()
	{
		return Collections.unmodifiableSet(keys);
	}

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

	public ConcurrentSkipListMap<String, UUID> getNames()
	{
		return names;
	}

	public UUIDMap getUUIDMap()
	{
		return uuidMap;
	}

	private File getUserFileFromID(final UUID uuid)
	{
		final File userFolder = new File(ess.getDataFolder(), "userdata");
		return new File(userFolder, uuid.toString() + ".yml");
	}

	public File getUserFileFromString(final String name)
	{
		final File userFolder = new File(ess.getDataFolder(), "userdata");
		return new File(userFolder, StringUtil.sanitizeFileName(name) + ".yml");
	}
}