From c171d81072ea9d54b0c665ab4e26dbbc6dac5b6a Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sun, 7 Feb 2021 20:04:26 +0100 Subject: [PATCH] Added PlayerHead cache --- .../invui/item/itembuilder/ItemBuilder.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/studiocode/invui/item/itembuilder/ItemBuilder.java b/src/main/java/de/studiocode/invui/item/itembuilder/ItemBuilder.java index 62f84bf..bd448a8 100644 --- a/src/main/java/de/studiocode/invui/item/itembuilder/ItemBuilder.java +++ b/src/main/java/de/studiocode/invui/item/itembuilder/ItemBuilder.java @@ -192,6 +192,9 @@ public class ItemBuilder implements Cloneable { * @see ItemBuilder */ public static class PlayerHead { + + private static final HashMap uuidCache = new HashMap<>(); + private static final HashMap textureCache = new HashMap<>(); private final String textureValue; @@ -204,8 +207,13 @@ public class ItemBuilder implements Cloneable { } public static PlayerHead of(@NotNull String playerName) { + if (uuidCache.containsKey(playerName)) return of(uuidCache.get(playerName)); + try { - return of(MojangApiUtils.getCurrentUUID(playerName)); + UUID currentUUID = MojangApiUtils.getCurrentUUID(playerName); + uuidCache.put(playerName, currentUUID); + + return of(currentUUID); } catch (IOException e) { e.printStackTrace(); } @@ -214,8 +222,13 @@ public class ItemBuilder implements Cloneable { } public static PlayerHead of(@NotNull UUID uuid) { + if (textureCache.containsKey(uuid)) return new PlayerHead(textureCache.get(uuid)); + try { - return new PlayerHead(MojangApiUtils.getSkinData(uuid, false)[0]); + String textureValue = MojangApiUtils.getSkinData(uuid, false)[0]; + textureCache.put(uuid, textureValue); + + return new PlayerHead(textureValue); } catch (IOException e) { e.printStackTrace(); } @@ -227,6 +240,11 @@ public class ItemBuilder implements Cloneable { return new PlayerHead(textureValue); } + public static void clearCache() { + uuidCache.clear(); + textureCache.clear(); + } + public String getTextureValue() { return textureValue; }