Snippets

Summerfeeling NPCs - Fake players in the 1.8.3+

Created by Summerfeeling
  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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package de.summerfeeling.postcrafter.entity;

import com.google.gson.*;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.util.UUIDTypeAdapter;
import net.md_5.bungee.api.ChatColor;
import net.minecraft.server.v1_8_R2.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_8_R2.util.CraftChatMessage;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NPC {

    private DataWatcher watcher;
    private GameProfile profile;
    private Material chestplate;
    private Material leggings;
    private Location location;
    private Material inHand;
    private Material helmet;
    private Material boots;
    private String tablist;
    private int entityID;
    private String name;

   /*
    * NPC, a class for spawning fake players in the 1.8
   Copyright (C) [Summerfeeling]
   Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
   Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
   Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
    */

    public NPC(String name, String tablist, int entityID, Location location, Material inHand) {
        this.location = location;
        this.tablist = ChatColor.translateAlternateColorCodes('&', tablist);
        this.name = ChatColor.translateAlternateColorCodes('&', name);
        this.entityID = entityID;
        this.inHand = inHand;
        this.watcher = new DataWatcher(null);
        watcher.a(6, (float) 20);
    }

    public NPC(String name, Location location) {
        this(name, name, new Random().nextInt(10000), location, Material.AIR);
    }

    public NPC(String name, Location location, Material inHand) {
        this(name, name, new Random().nextInt(10000), location, inHand);
    }

    public NPC(String name, String tablist, Location location) {
        this(name, tablist, new Random().nextInt(10000), location, Material.AIR);
    }

    public NPC(String name, String tablist, Location location, Material inHand) {
        this(name, tablist, new Random().nextInt(10000), location, inHand);
    }

    @SuppressWarnings("deprecation")
    public void spawn() {
        try{
            PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
            addToTablist();

            setValue(packet, "a", entityID);
            setValue(packet, "b", this.profile.getId());
            setValue(packet, "c", toFixedPoint(location.getX()));
            setValue(packet, "d", toFixedPoint(location.getY()));
            setValue(packet, "e", toFixedPoint(location.getZ()));
            setValue(packet, "f", toPackedByte(location.getYaw()));
            setValue(packet, "g", toPackedByte(location.getPitch()));
            setValue(packet, "h", inHand == null ? 0 : inHand.getId());
            setValue(packet, "i", watcher);

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    public void despawn() {
        PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{this.entityID});
        this.removeFromTablist();
        for(Player online : Bukkit.getOnlinePlayers()) {
            ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
        }
    }

    @SuppressWarnings("deprecation")
    public void changePlayerlistName(String name) {
        try{
            PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);
            PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, 0, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(name)[0]);
            @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
            players.add(data);

            this.setValue(packet, "b", players);
            this.tablist = name;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    private void addToTablist() {
        try {
            PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
            GameProfile profile = this.profile = this.getProfile();
            PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(profile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(tablist)[0]);
            @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packet, "b");
            players.add(data);

            setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
            setValue(packet, "b", players);

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    private void removeFromTablist() {
        try{
            PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
            PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, -1, null, null);
            @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
            players.add(data);

            this.setValue(packet, "b", players);

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    public void teleport(Location location) {
        try{
            PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", this.toFixedPoint(location.getX()));
            this.setValue(packet, "c", this.toFixedPoint(location.getY()));
            this.setValue(packet, "d", this.toFixedPoint(location.getZ()));
            this.setValue(packet, "e", this.toPackedByte(location.getYaw()));
            this.setValue(packet, "f", this.toPackedByte(location.getPitch()));
            this.setValue(packet, "g", this.location.getBlock().getType() != Material.AIR);
            this.location = location;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    public void setItemInHand(Material material) {
        try{
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", 0);
            this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
            this.inHand = material;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Material getItemInHand() {
        return this.inHand;
    }

    @SuppressWarnings("deprecation")
    public void setHelmet(Material material) {
        try{
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", 4);
            this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
            this.helmet = material;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Material getHelmet() {
        return this.helmet;
    }

    @SuppressWarnings("deprecation")
    public void setChestplate(Material material) {
        try{
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", 3);
            this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
            this.chestplate = material;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Material getChestplate() {
        return this.chestplate;
    }

    @SuppressWarnings("deprecation")
    public void setLeggings(Material material) {
        try{
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", 2);
            this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
            this.leggings = material;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Material getLeggings() {
        return this.leggings;
    }

    @SuppressWarnings("deprecation")
    public void setBoots(Material material) {
        try{
            PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();

            this.setValue(packet, "a", this.entityID);
            this.setValue(packet, "b", 1);
            this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
            this.boots = material;

            for(Player online : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Material getBoots() {
        return this.boots;
    }

    public int getEntityID() {
        return this.entityID;
    }

    public UUID getUUID() {
        return this.profile.getId();
    }

    public Location getLocation() {
        return this.location;
    }

    public String getName() {
        return this.name;
    }

    public String getPlayerlistName() {
        return this.tablist;
    }

    private void setValue(Object instance, String field, Object value) throws Exception {
        Field f = instance.getClass().getDeclaredField(field);
        f.setAccessible(true);
        f.set(instance, value);
    }

    private Object getValue(Object instance, String field) throws Exception {
        Field f = instance.getClass().getDeclaredField(field);
        f.setAccessible(true);
        return f.get(instance);
    }

    private int toFixedPoint(double d) {
        return (int) (d * 32.0);
    }

    private byte toPackedByte(float f) {
        return (byte) ((int) (f * 256.0F / 360.0F));
    }

    private GameProfile getProfile() {
        try {
            GameProfile profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(ChatColor.stripColor(this.name)));
            Field name = profile.getClass().getDeclaredField("name");
            name.setAccessible(true);
            name.set(profile, this.name);
            return profile;
        } catch (Exception e) {
            return new GameProfile(UUID.randomUUID(), this.name);
        }
    }

    public static class GameProfileBuilder {

        private static final String SERVICE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
        private static final String JSON_SKIN = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}";
        private static final String JSON_CAPE = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}";

        private static Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();

        private static HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();

        private static long cacheTime = -1;

        /**
         * Don't run in main thread!
         *
         * Fetches the GameProfile from the Mojang servers
         *
         * @param uuid The player uuid
         * @return The GameProfile
         * @throws IOException If something wents wrong while fetching
         * @see GameProfile
         */
        public static GameProfile fetch(UUID uuid) throws IOException {
            return fetch(uuid, false);
        }

        /**
         * Don't run in main thread!
         *
         * Fetches the GameProfile from the Mojang servers
         * @param uuid The player uuid
         * @param forceNew If true the cache is ignored
         * @return The GameProfile
         * @throws IOException If something wents wrong while fetching
         * @see GameProfile
         */
        public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException {
            if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid()) {
                return cache.get(uuid).profile;
            } else {
                HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
                connection.setReadTimeout(5000);

                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();

                    GameProfile result = gson.fromJson(json, GameProfile.class);
                    cache.put(uuid, new CachedProfile(result));
                    return result;
                } else {
                    if (!forceNew && cache.containsKey(uuid)) {
                        return cache.get(uuid).profile;
                    }
                    JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
                    throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
                }
            }
        }

        /**
         * Builds a GameProfile for the specified args
         *
         * @param uuid The uuid
         * @param name The name
         * @param skin The url from the skin image
         * @return A GameProfile built from the arguments
         * @see GameProfile
         */
        public static GameProfile getProfile(UUID uuid, String name, String skin) {
            return getProfile(uuid, name, skin, null);
        }

        /**
         * Builds a GameProfile for the specified args
         *
         * @param uuid The uuid
         * @param name The name
         * @param skinUrl Url from the skin image
         * @param capeUrl Url from the cape image
         * @return A GameProfile built from the arguments
         * @see GameProfile
         */
        public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) {
            GameProfile profile = new GameProfile(uuid, name);
            boolean cape = capeUrl != null && !capeUrl.isEmpty();

            List<Object> args = new ArrayList<Object>();
            args.add(System.currentTimeMillis());
            args.add(UUIDTypeAdapter.fromUUID(uuid));
            args.add(name);
            args.add(skinUrl);
            if (cape) args.add(capeUrl);

            profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()])))));
            return profile;
        }

        /**
         * Sets the time as long as you want to keep the gameprofiles in cache (-1 = never remove it)
         * @param time cache time (default = -1)
         */
        public static void setCacheTime(long time) {
            cacheTime = time;
        }

        private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {

            public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
                JsonObject object = (JsonObject) json;
                UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
                String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
                GameProfile profile = new GameProfile(id, name);

                if (object.has("properties")) {
                    for (Map.Entry<String, Property> prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) {
                        profile.getProperties().put(prop.getKey(), prop.getValue());
                    }
                }
                return profile;
            }

            public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) {
                JsonObject result = new JsonObject();
                if (profile.getId() != null)
                    result.add("id", context.serialize(profile.getId()));
                if (profile.getName() != null)
                    result.addProperty("name", profile.getName());
                if (!profile.getProperties().isEmpty())
                    result.add("properties", context.serialize(profile.getProperties()));
                return result;
            }
        }

        private static class CachedProfile {

            private long timestamp = System.currentTimeMillis();
            private GameProfile profile;

            public CachedProfile(GameProfile profile) {
                this.profile = profile;
            }

            public boolean isValid() {
                return cacheTime < 0 || (System.currentTimeMillis() - timestamp) < cacheTime;
            }
        }
    }

    public static class UUIDFetcher {

        /**
         * Date when name changes were introduced
         * @see UUIDFetcher#getUUIDAt(String, long)
         */
        public static final long FEBRUARY_2015 = 1422748800000L;


        private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();

        private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%d";
        private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names";

        private static Map<String, UUID> uuidCache = new HashMap<String, UUID>();
        private static Map<UUID, String> nameCache = new HashMap<UUID, String>();

        private static ExecutorService pool = Executors.newCachedThreadPool();

        private String name;
        private UUID id;

        /**
         * Fetches the uuid asynchronously and passes it to the consumer
         *
         * @param name The name
         * @param action Do what you want to do with the uuid her
         */
        public static void getUUID(final String name, Consumer<UUID> action) {
            pool.execute(new Acceptor<UUID>(action) {

                @Override
                public UUID getValue() {
                    return getUUID(name);
                }
            });
        }

        /**
         * Fetches the uuid synchronously and returns it
         *
         * @param name The name
         * @return The uuid
         */
        public static UUID getUUID(String name) {
            return getUUIDAt(name, System.currentTimeMillis());
        }

        /**
         * Fetches the uuid synchronously for a specified name and time and passes the result to the consumer
         *
         * @param name The name
         * @param timestamp Time when the player had this name in milliseconds
         * @param action Do what you want to do with the uuid her
         */
        public static void getUUIDAt(final String name, final long timestamp, Consumer<UUID> action) {
            pool.execute(new Acceptor<UUID>(action) {

                @Override
                public UUID getValue() {
                    return getUUIDAt(name, timestamp);
                }

            });
        }

        /**
         * Fetches the uuid synchronously for a specified name and time
         *
         * @param name The name
         * @param timestamp Time when the player had this name in milliseconds
         * @see UUIDFetcher#FEBRUARY_2015
         */
        public static UUID getUUIDAt(String name, long timestamp) {
            name = name.toLowerCase();
            if (uuidCache.containsKey(name)) {
                return uuidCache.get(name);
            }
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(String.format(UUID_URL, name, timestamp/1000)).openConnection();
                connection.setReadTimeout(5000);
                UUIDFetcher data = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher.class);

                uuidCache.put(name, data.id);
                nameCache.put(data.id, data.name);

                return data.id;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * Fetches the name asynchronously and passes it to the consumer
         *
         * @param uuid The uuid
         * @param action Do what you want to do with the name her
         */
        public static void getName(final UUID uuid, Consumer<String> action) {
            pool.execute(new Acceptor<String>(action) {

                @Override
                public String getValue() {
                    return getName(uuid);
                }

            });
        }

        /**
         * Fetches the name synchronously and returns it
         *
         * @param uuid The uuid
         * @return The name
         */
        public static String getName(UUID uuid) {
            if (nameCache.containsKey(uuid)) {
                return nameCache.get(uuid);
            }
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
                connection.setReadTimeout(5000);
                UUIDFetcher[] nameHistory = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher[].class);
                UUIDFetcher currentNameData = nameHistory[nameHistory.length - 1];

                uuidCache.put(currentNameData.name.toLowerCase(), uuid);
                nameCache.put(uuid, currentNameData.name);

                return currentNameData.name;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        public static interface Consumer<T> {
            void accept(T t);
        }

        public static abstract class Acceptor<T> implements Runnable {

            private Consumer<T> consumer;

            public Acceptor(Consumer<T> consumer) {
                this.consumer = consumer;
            }

            public abstract T getValue();

            @Override
            public void run() {
                consumer.accept(getValue());
            }

        }
    }

}

Comments (0)