From 965da3655a1188c2c5ef6334734ad5af632ad154 Mon Sep 17 00:00:00 2001 From: Vincent Roquelaure Date: Sun, 17 Nov 2024 20:43:41 +0100 Subject: [PATCH] Fixed possible duplicate when loading heads --- build.gradle.kts | 2 +- .../headblocks/services/StorageService.java | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ec77a8a..93920ca 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("java") } -version = "2.6.9" +version = "2.6.10" allprojects { repositories { diff --git a/core/src/main/java/fr/aerwyn81/headblocks/services/StorageService.java b/core/src/main/java/fr/aerwyn81/headblocks/services/StorageService.java index 9d846fb..bb2057d 100644 --- a/core/src/main/java/fr/aerwyn81/headblocks/services/StorageService.java +++ b/core/src/main/java/fr/aerwyn81/headblocks/services/StorageService.java @@ -31,7 +31,7 @@ public class StorageService { private static boolean storageError; - private static ConcurrentHashMap> _cacheHeads; + private static ConcurrentHashMap> _cacheHeads; private static LinkedHashMap _cacheTop; private static String serverIdentifier = ""; @@ -228,7 +228,7 @@ public static void loadPlayers(Player... players) { var playerProfile = new PlayerProfileLight(pUuid, playerName, playerDisplayName); - var playerHeads = new ArrayList(); + var playerHeads = new HashSet(); if (isExist) { boolean hasRenamed = hasPlayerRenamed(playerProfile); @@ -278,14 +278,14 @@ public static void unloadPlayer(Player player) { UUID uuid = player.getUniqueId(); String playerName = player.getName(); + _cacheHeads.remove(uuid); + try { boolean isExist = containsPlayer(uuid); if (isExist) { storage.resetPlayer(uuid); } - - _cacheHeads.remove(uuid); } catch (InternalException ex) { storageError = true; HeadBlocks.log.sendMessage(MessageUtils.colorize("&cError while trying to unload player " + playerName + " from SQL database: " + ex.getMessage())); @@ -336,26 +336,25 @@ public static Boolean containsPlayer(UUID playerUuid) throws InternalException { return storage.containsPlayer(playerUuid) || database.containsPlayer(playerUuid); } - public static BukkitFutureResult> getHeadsPlayer(UUID playerUuid) { + public static BukkitFutureResult> getHeadsPlayer(UUID playerUuid) { if (_cacheHeads.containsKey(playerUuid)) return BukkitFutureResult.of(HeadBlocks.getInstance(), CompletableFuture.completedFuture(_cacheHeads.get(playerUuid))); return CompletableBukkitFuture.supplyAsync(HeadBlocks.getInstance(), () -> { try { var headsUuid = database.getHeadsPlayer(playerUuid); - _cacheHeads.compute(playerUuid, (key, playerHeads) -> { - if (playerHeads == null) { - return headsUuid; - } else { - playerHeads.addAll(headsUuid); - return playerHeads; - } - }); - return headsUuid; + if (!_cacheHeads.containsKey(playerUuid)) { + _cacheHeads.put(playerUuid, new HashSet<>()); + } + + var playerHeads = _cacheHeads.get(playerUuid); + playerHeads.addAll(headsUuid); + + return playerHeads; } catch (Exception ex) { HeadBlocks.log.sendMessage(MessageUtils.colorize("&cError while trying to get heads for " + playerUuid + ": " + ex.getMessage())); - return new ArrayList<>(); + return new HashSet<>(); } }); }