From a966977d82668119c784872e5865a84760a1601e Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 13 Apr 2024 17:29:32 +0100 Subject: [PATCH] fix username to uuid cache --- .../cc/fascinated/model/cache/CachedPlayerName.java | 10 ++++++++-- src/main/java/cc/fascinated/service/PlayerService.java | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/cc/fascinated/model/cache/CachedPlayerName.java b/src/main/java/cc/fascinated/model/cache/CachedPlayerName.java index da9c246..5cf1ba7 100644 --- a/src/main/java/cc/fascinated/model/cache/CachedPlayerName.java +++ b/src/main/java/cc/fascinated/model/cache/CachedPlayerName.java @@ -14,18 +14,24 @@ import java.util.UUID; @ToString @RedisHash(value = "playerName", timeToLive = 60L * 60L * 6) // 6 hours (in seconds) public final class CachedPlayerName extends CachedResponse { + /** + * The id of the player. + */ + @Id private final String id; + /** * The username of the player. */ - @Id private final String username; + private final String username; /** * The unique id of the player. */ private final UUID uniqueId; - public CachedPlayerName(String username, UUID uniqueId) { + public CachedPlayerName(String id, String username, UUID uniqueId) { super(CacheInformation.defaultCache()); + this.id = id; this.username = username; this.uniqueId = uniqueId; } diff --git a/src/main/java/cc/fascinated/service/PlayerService.java b/src/main/java/cc/fascinated/service/PlayerService.java index 4aad9d1..7f01fb8 100644 --- a/src/main/java/cc/fascinated/service/PlayerService.java +++ b/src/main/java/cc/fascinated/service/PlayerService.java @@ -100,7 +100,8 @@ public class PlayerService { */ public CachedPlayerName usernameToUuid(String username) { log.info("Getting UUID from username: {}", username); - Optional cachedPlayerName = playerNameCacheRepository.findById(username.toUpperCase()); + String id = username.toUpperCase(); + Optional cachedPlayerName = playerNameCacheRepository.findById(id); if (cachedPlayerName.isPresent() && Config.INSTANCE.isProduction()) { return cachedPlayerName.get(); } @@ -111,7 +112,7 @@ public class PlayerService { throw new ResourceNotFoundException("Player with username '%s' not found".formatted(username)); } UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getUuid()); - CachedPlayerName player = new CachedPlayerName(username, uuid); + CachedPlayerName player = new CachedPlayerName(id, username, uuid); playerNameCacheRepository.save(player); log.info("Got UUID from username: {} -> {}", username, uuid); player.getCache().setCached(false);