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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
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.cache.RemovalCause;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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<String, User> implements IConf
{
private final transient IEssentials ess;
private final transient Cache<String, 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 ConcurrentSkipListMap<UUID, ArrayList<String>> history = new ConcurrentSkipListMap<UUID, ArrayList<String>>();
private UUIDMap uuidMap;
public UserMap(final IEssentials ess)
{
super();
this.ess = ess;
uuidMap = new UUIDMap(ess);
//RemovalListener<UUID, User> remListener = new UserMapRemovalListener();
//users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().removalListener(remListener).build(this);
users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this);
}
private void loadAllUsersAsync(final IEssentials ess)
{
ess.runTaskAsynchronously(new Runnable()
{
@Override
public void run()
{
synchronized (users)
{
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);
try
{
keys.add(UUID.fromString(name));
}
catch (IllegalArgumentException ex)
{
//Ignore these users till they rejoin.
}
}
uuidMap.loadAllUsers(names, history);
}
}
});
}
public boolean userExists(final UUID uuid)
{
return keys.contains(uuid);
}
public User getUser(final String name)
{
try
{
final String sanitizedName = StringUtil.safeString(name);
if (names.containsKey(sanitizedName))
{
final UUID uuid = names.get(sanitizedName);
return getUser(uuid);
}
final File userFile = getUserFileFromString(sanitizedName);
if (userFile.exists())
{
ess.getLogger().info("Importing user " + name + " to usermap.");
User user = new User(new OfflinePlayer(sanitizedName, ess.getServer()), ess);
trackUUID(user.getBase().getUniqueId(), user.getName());
return user;
}
return null;
}
catch (UncheckedExecutionException ex)
{
return null;
}
}
public User getUser(final UUID uuid)
{
try
{
return users.get(uuid.toString());
}
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)
{
final String keyName = StringUtil.safeString(name);
if (!names.containsKey(keyName) || !names.get(keyName).equals(uuid))
{
names.put(keyName, uuid);
uuidMap.writeUUIDMap();
}
}
}
}
@Override
public User load(final String stringUUID) throws Exception
{
UUID uuid = UUID.fromString(stringUUID);
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())
{
player = new OfflinePlayer(uuid, ess.getServer());
final User user = new User(player, ess);
((OfflinePlayer)player).setName(user.getLastAccountName());
trackUUID(uuid, user.getName());
return user;
}
throw new Exception("User not found!");
}
@Override
public void reloadConfig()
{
getUUIDMap().forceWriteUUIDMap();
loadAllUsersAsync(ess);
}
public void invalidateAll()
{
users.invalidateAll();
}
public void removeUser(final String name)
{
if (names == null)
{
ess.getLogger().warning("Name collection is null, cannot remove user.");
return;
}
UUID uuid = names.get(name);
if (uuid != null)
{
keys.remove(uuid);
users.invalidate(uuid);
}
names.remove(name);
names.remove(StringUtil.safeString(name));
}
public Set<UUID> getAllUniqueUsers()
{
return Collections.unmodifiableSet(keys.clone());
}
public int getUniqueUsers()
{
return keys.size();
}
protected ConcurrentSkipListMap<String, UUID> getNames()
{
return names;
}
protected ConcurrentSkipListMap<UUID, ArrayList<String>> getHistory()
{
return history;
}
public List<String> getUserHistory(final UUID uuid)
{
return history.get(uuid);
}
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");
}
// class UserMapRemovalListener implements RemovalListener
// {
// @Override
// public void onRemoval(final RemovalNotification notification)
// {
// Object value = notification.getValue();
// if (value != null)
// {
// ((User)value).cleanup();
// }
// }
// }
}
|