PlayerProfileCache .java

net.minecraft.server.management.PlayerProfileCache (class): members: getGameProfileForUsername, usernameToProfileEntryMap, uuidToProfileEntryMap, save.

net.minecraft.server.managementclass402 linesserver14.2 KB

API Surface (15)

Type[] getActualTypeArguments()
Type getRawType()
Type getOwnerType()
void onProfileLookupSucceeded(GameProfile p_onProfileLookupSucceeded_1_)
void onProfileLookupFailed(GameProfile p_onProfileLookupFailed_1_, Exception p_onProfileLookupFailed_2_)
void addEntry(GameProfile gameProfile)
GameProfile getGameProfileForUsername(String username)
String[] getUsernames()
GameProfile getProfileByUUID(UUID uuid)
void load()
void save()
GameProfile getGameProfile()
Date getExpirationDate()
JsonElement serialize(PlayerProfileCache.ProfileEntry p_serialize_1_, Type p_serialize_2_, JsonSerializationContext p_serialize_3_)
PlayerProfileCache.ProfileEntry deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_)

Fields & Constants (8)

dateFormat
TYPE
usernameToProfileEntryMap
uuidToProfileEntryMap
gameProfiles
mcServer
gson
usercacheFile

String literals (3)

"yyyy-MM-dd HH:mm:ss Z"
"uuid"
"expiresOn"

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PlayerProfileCache.java402 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.server.management;
2
3
import com.google.common.base.Charsets;
4
import com.google.common.collect.Iterators;
5
import com.google.common.collect.Lists;
6
import com.google.common.collect.Maps;
7
import com.google.common.io.Files;
8
import com.google.gson.Gson;
9
import com.google.gson.GsonBuilder;
10
import com.google.gson.JsonDeserializationContext;
11
import com.google.gson.JsonDeserializer;
12
import com.google.gson.JsonElement;
13
import com.google.gson.JsonObject;
14
import com.google.gson.JsonParseException;
15
import com.google.gson.JsonSerializationContext;
16
import com.google.gson.JsonSerializer;
17
import com.mojang.authlib.Agent;
18
import com.mojang.authlib.GameProfile;
19
import com.mojang.authlib.ProfileLookupCallback;
20
import java.io.BufferedReader;
21
import java.io.BufferedWriter;
22
import java.io.File;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25
import java.io.Reader;
26
import java.io.Writer;
27
import java.lang.reflect.ParameterizedType;
28
import java.lang.reflect.Type;
29
import java.text.ParseException;
30
import java.text.SimpleDateFormat;
31
import java.util.ArrayList;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.LinkedList;
35
import java.util.List;
36
import java.util.Locale;
37
import java.util.Map;
38
import java.util.UUID;
39
import net.minecraft.entity.player.EntityPlayer;
40
import net.minecraft.server.MinecraftServer;
41
import org.apache.commons.io.IOUtils;
42
43
public class PlayerProfileCache
44
{
45
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
46
    private final Map<String, PlayerProfileCache.ProfileEntry> usernameToProfileEntryMap = Maps.<String, PlayerProfileCache.ProfileEntry>newHashMap();
47
    private final Map<UUID, PlayerProfileCache.ProfileEntry> uuidToProfileEntryMap = Maps.<UUID, PlayerProfileCache.ProfileEntry>newHashMap();
48
    private final LinkedList<GameProfile> gameProfiles = Lists.<GameProfile>newLinkedList();
49
    private final MinecraftServer mcServer;
50
    protected final Gson gson;
51
    private final File usercacheFile;
52
    private static final ParameterizedType TYPE = new ParameterizedType()
53
    {
54
        public Type[] getActualTypeArguments()
55
        {
56
            return new Type[] {PlayerProfileCache.ProfileEntry.class};
57
        }
58
        public Type getRawType()
59
        {
60
            return List.class;
61
        }
62
        public Type getOwnerType()
63
        {
64
            return null;
65
        }
66
    };
67
68
    public PlayerProfileCache(MinecraftServer server, File cacheFile)
69
    {
70
        this.mcServer = server;
71
        this.usercacheFile = cacheFile;
72
        GsonBuilder gsonbuilder = new GsonBuilder();
73
        gsonbuilder.registerTypeHierarchyAdapter(PlayerProfileCache.ProfileEntry.class, new PlayerProfileCache.Serializer());
74
        this.gson = gsonbuilder.create();
75
        this.load();
76
    }
77
78
    /**
79
     * Get a GameProfile given the MinecraftServer and the player's username.
80
81
     *  The UUID of the GameProfile will <b>not</b> be null. If the server is offline, a UUID based on the hash of the
82
     * username will be used.
83
     */
84
    private static GameProfile getGameProfile(MinecraftServer server, String username)
85
    {
86
        final GameProfile[] agameprofile = new GameProfile[1];
87
        ProfileLookupCallback profilelookupcallback = new ProfileLookupCallback()
88
        {
89
            public void onProfileLookupSucceeded(GameProfile p_onProfileLookupSucceeded_1_)
90
            {
91
                agameprofile[0] = p_onProfileLookupSucceeded_1_;
92
            }
93
            public void onProfileLookupFailed(GameProfile p_onProfileLookupFailed_1_, Exception p_onProfileLookupFailed_2_)
94
            {
95
                agameprofile[0] = null;
96
            }
97
        };
98
        server.getGameProfileRepository().findProfilesByNames(new String[] {username}, Agent.MINECRAFT, profilelookupcallback);
99
100
        if (!server.isServerInOnlineMode() && agameprofile[0] == null)
101
        {
102
            UUID uuid = EntityPlayer.getUUID(new GameProfile((UUID)null, username));
103
            GameProfile gameprofile = new GameProfile(uuid, username);
104
            profilelookupcallback.onProfileLookupSucceeded(gameprofile);
105
        }
106
107
        return agameprofile[0];
108
    }
109
110
    /**
111
     * Add an entry to this cache
112
     */
113
    public void addEntry(GameProfile gameProfile)
114
    {
115
        this.addEntry(gameProfile, (Date)null);
116
    }
117
118
    /**
119
     * Add an entry to this cache
120
     */
121
    private void addEntry(GameProfile gameProfile, Date expirationDate)
122
    {
123
        UUID uuid = gameProfile.getId();
124
125
        if (expirationDate == null)
126
        {
127
            Calendar calendar = Calendar.getInstance();
128
            calendar.setTime(new Date());
129
            calendar.add(2, 1);
130
            expirationDate = calendar.getTime();
131
        }
132
133
        String s = gameProfile.getName().toLowerCase(Locale.ROOT);
134
        PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = new PlayerProfileCache.ProfileEntry(gameProfile, expirationDate);
135
136
        if (this.uuidToProfileEntryMap.containsKey(uuid))
137
        {
138
            PlayerProfileCache.ProfileEntry playerprofilecache$profileentry1 = (PlayerProfileCache.ProfileEntry)this.uuidToProfileEntryMap.get(uuid);
139
            this.usernameToProfileEntryMap.remove(playerprofilecache$profileentry1.getGameProfile().getName().toLowerCase(Locale.ROOT));
140
            this.gameProfiles.remove(gameProfile);
141
        }
142
143
        this.usernameToProfileEntryMap.put(gameProfile.getName().toLowerCase(Locale.ROOT), playerprofilecache$profileentry);
144
        this.uuidToProfileEntryMap.put(uuid, playerprofilecache$profileentry);
145
        this.gameProfiles.addFirst(gameProfile);
146
        this.save();
147
    }
148
149
    /**
150
     * Get a player's GameProfile given their username. Mojang's server's will be contacted if the entry is not cached
151
     * locally.
152
     */
153
    public GameProfile getGameProfileForUsername(String username)
154
    {
155
        String s = username.toLowerCase(Locale.ROOT);
156
        PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = (PlayerProfileCache.ProfileEntry)this.usernameToProfileEntryMap.get(s);
157
158
        if (playerprofilecache$profileentry != null && (new Date()).getTime() >= playerprofilecache$profileentry.expirationDate.getTime())
159
        {
160
            this.uuidToProfileEntryMap.remove(playerprofilecache$profileentry.getGameProfile().getId());
161
            this.usernameToProfileEntryMap.remove(playerprofilecache$profileentry.getGameProfile().getName().toLowerCase(Locale.ROOT));
162
            this.gameProfiles.remove(playerprofilecache$profileentry.getGameProfile());
163
            playerprofilecache$profileentry = null;
164
        }
165
166
        if (playerprofilecache$profileentry != null)
167
        {
168
            GameProfile gameprofile = playerprofilecache$profileentry.getGameProfile();
169
            this.gameProfiles.remove(gameprofile);
170
            this.gameProfiles.addFirst(gameprofile);
171
        }
172
        else
173
        {
174
            GameProfile gameprofile1 = getGameProfile(this.mcServer, s);
175
176
            if (gameprofile1 != null)
177
            {
178
                this.addEntry(gameprofile1);
179
                playerprofilecache$profileentry = (PlayerProfileCache.ProfileEntry)this.usernameToProfileEntryMap.get(s);
180
            }
181
        }
182
183
        this.save();
184
        return playerprofilecache$profileentry == null ? null : playerprofilecache$profileentry.getGameProfile();
185
    }
186
187
    /**
188
     * Get an array of the usernames that are cached in this cache
189
     */
190
    public String[] getUsernames()
191
    {
192
        List<String> list = Lists.newArrayList(this.usernameToProfileEntryMap.keySet());
193
        return (String[])list.toArray(new String[list.size()]);
194
    }
195
196
    /**
197
     * Get a player's {@link GameProfile} given their UUID
198
     */
199
    public GameProfile getProfileByUUID(UUID uuid)
200
    {
201
        PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = (PlayerProfileCache.ProfileEntry)this.uuidToProfileEntryMap.get(uuid);
202
        return playerprofilecache$profileentry == null ? null : playerprofilecache$profileentry.getGameProfile();
203
    }
204
205
    /**
206
     * Get a {@link ProfileEntry} by UUID
207
     */
208
    private PlayerProfileCache.ProfileEntry getByUUID(UUID uuid)
209
    {
210
        PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = (PlayerProfileCache.ProfileEntry)this.uuidToProfileEntryMap.get(uuid);
211
212
        if (playerprofilecache$profileentry != null)
213
        {
214
            GameProfile gameprofile = playerprofilecache$profileentry.getGameProfile();
215
            this.gameProfiles.remove(gameprofile);
216
            this.gameProfiles.addFirst(gameprofile);
217
        }
218
219
        return playerprofilecache$profileentry;
220
    }
221
222
    /**
223
     * Load the cached profiles from disk
224
     */
225
    public void load()
226
    {
227
        BufferedReader bufferedreader = null;
228
229
        try
230
        {
231
            bufferedreader = Files.newReader(this.usercacheFile, Charsets.UTF_8);
232
            List<PlayerProfileCache.ProfileEntry> list = (List)this.gson.fromJson((Reader)bufferedreader, TYPE);
233
            this.usernameToProfileEntryMap.clear();
234
            this.uuidToProfileEntryMap.clear();
235
            this.gameProfiles.clear();
236
237
            for (PlayerProfileCache.ProfileEntry playerprofilecache$profileentry : Lists.reverse(list))
238
            {
239
                if (playerprofilecache$profileentry != null)
240
                {
241
                    this.addEntry(playerprofilecache$profileentry.getGameProfile(), playerprofilecache$profileentry.getExpirationDate());
242
                }
243
            }
244
        }
245
        catch (FileNotFoundException var9)
246
        {
247
            ;
248
        }
249
        catch (JsonParseException var10)
250
        {
251
            ;
252
        }
253
        finally
254
        {
255
            IOUtils.closeQuietly((Reader)bufferedreader);
256
        }
257
    }
258
259
    /**
260
     * Save the cached profiles to disk
261
     */
262
    public void save()
263
    {
264
        String s = this.gson.toJson((Object)this.getEntriesWithLimit(1000));
265
        BufferedWriter bufferedwriter = null;
266
267
        try
268
        {
269
            bufferedwriter = Files.newWriter(this.usercacheFile, Charsets.UTF_8);
270
            bufferedwriter.write(s);
271
            return;
272
        }
273
        catch (FileNotFoundException var8)
274
        {
275
            ;
276
        }
277
        catch (IOException var9)
278
        {
279
            return;
280
        }
281
        finally
282
        {
283
            IOUtils.closeQuietly((Writer)bufferedwriter);
284
        }
285
    }
286
287
    private List<PlayerProfileCache.ProfileEntry> getEntriesWithLimit(int limitSize)
288
    {
289
        ArrayList<PlayerProfileCache.ProfileEntry> arraylist = Lists.<PlayerProfileCache.ProfileEntry>newArrayList();
290
291
        for (GameProfile gameprofile : Lists.newArrayList(Iterators.limit(this.gameProfiles.iterator(), limitSize)))
292
        {
293
            PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = this.getByUUID(gameprofile.getId());
294
295
            if (playerprofilecache$profileentry != null)
296
            {
297
                arraylist.add(playerprofilecache$profileentry);
298
            }
299
        }
300
301
        return arraylist;
302
    }
303
304
    class ProfileEntry
305
    {
306
        private final GameProfile gameProfile;
307
        private final Date expirationDate;
308
309
        private ProfileEntry(GameProfile gameProfileIn, Date expirationDateIn)
310
        {
311
            this.gameProfile = gameProfileIn;
312
            this.expirationDate = expirationDateIn;
313
        }
314
315
        public GameProfile getGameProfile()
316
        {
317
            return this.gameProfile;
318
        }
319
320
        public Date getExpirationDate()
321
        {
322
            return this.expirationDate;
323
        }
324
    }
325
326
    class Serializer implements JsonDeserializer<PlayerProfileCache.ProfileEntry>, JsonSerializer<PlayerProfileCache.ProfileEntry>
327
    {
328
        private Serializer()
329
        {
330
        }
331
332
        public JsonElement serialize(PlayerProfileCache.ProfileEntry p_serialize_1_, Type p_serialize_2_, JsonSerializationContext p_serialize_3_)
333
        {
334
            JsonObject jsonobject = new JsonObject();
335
            jsonobject.addProperty("name", p_serialize_1_.getGameProfile().getName());
336
            UUID uuid = p_serialize_1_.getGameProfile().getId();
337
            jsonobject.addProperty("uuid", uuid == null ? "" : uuid.toString());
338
            jsonobject.addProperty("expiresOn", PlayerProfileCache.dateFormat.format(p_serialize_1_.getExpirationDate()));
339
            return jsonobject;
340
        }
341
342
        public PlayerProfileCache.ProfileEntry deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
343
        {
344
            if (p_deserialize_1_.isJsonObject())
345
            {
346
                JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
347
                JsonElement jsonelement = jsonobject.get("name");
348
                JsonElement jsonelement1 = jsonobject.get("uuid");
349
                JsonElement jsonelement2 = jsonobject.get("expiresOn");
350
351
                if (jsonelement != null && jsonelement1 != null)
352
                {
353
                    String s = jsonelement1.getAsString();
354
                    String s1 = jsonelement.getAsString();
355
                    Date date = null;
356
357
                    if (jsonelement2 != null)
358
                    {
359
                        try
360
                        {
361
                            date = PlayerProfileCache.dateFormat.parse(jsonelement2.getAsString());
362
                        }
363
                        catch (ParseException var14)
364
                        {
365
                            date = null;
366
                        }
367
                    }
368
369
                    if (s1 != null && s != null)
370
                    {
371
                        UUID uuid;
372
373
                        try
374
                        {
375
                            uuid = UUID.fromString(s);
376
                        }
377
                        catch (Throwable var13)
378
                        {
379
                            return null;
380
                        }
381
382
                        PlayerProfileCache.ProfileEntry playerprofilecache$profileentry = PlayerProfileCache.this.new ProfileEntry(new GameProfile(uuid, s1), date);
383
                        return playerprofilecache$profileentry;
384
                    }
385
                    else
386
                    {
387
                        return null;
388
                    }
389
                }
390
                else
391
                {
392
                    return null;
393
                }
394
            }
395
            else
396
            {
397
                return null;
398
            }
399
        }
400
    }
401
}
402

Deobfuscated MCP 1.8.9 source of PlayerProfileCache from Marcelektro/MavenMCP-1.8.9 (net/minecraft/server/management/PlayerProfileCache.java), stored verbatim.