Skip to content

Commit

Permalink
Fixed possible duplicate when loading heads
Browse files Browse the repository at this point in the history
  • Loading branch information
AerWyn81 committed Nov 17, 2024
1 parent b4aa35e commit 965da36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id("java")
}

version = "2.6.9"
version = "2.6.10"

allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StorageService {

private static boolean storageError;

private static ConcurrentHashMap<UUID, List<UUID>> _cacheHeads;
private static ConcurrentHashMap<UUID, Set<UUID>> _cacheHeads;
private static LinkedHashMap<PlayerProfileLight, Integer> _cacheTop;

private static String serverIdentifier = "";
Expand Down Expand Up @@ -228,7 +228,7 @@ public static void loadPlayers(Player... players) {

var playerProfile = new PlayerProfileLight(pUuid, playerName, playerDisplayName);

var playerHeads = new ArrayList<UUID>();
var playerHeads = new HashSet<UUID>();

if (isExist) {
boolean hasRenamed = hasPlayerRenamed(playerProfile);
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -336,26 +336,25 @@ public static Boolean containsPlayer(UUID playerUuid) throws InternalException {
return storage.containsPlayer(playerUuid) || database.containsPlayer(playerUuid);
}

public static BukkitFutureResult<List<UUID>> getHeadsPlayer(UUID playerUuid) {
public static BukkitFutureResult<Set<UUID>> 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<>();
}
});
}
Expand Down

0 comments on commit 965da36

Please # to comment.