From a11a90f5308e2b83a2c3d1715baa765eced7468f Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 11 Apr 2024 00:49:16 +0100 Subject: [PATCH] cleanup username to uuid endpoint --- src/main/java/cc.fascinated/Main.java | 2 +- .../controller/PlayerController.java | 5 +++-- .../model/cache/CachedPlayer.java | 4 ++-- .../model/mojang/MojangUsernameToUuid.java | 9 +++++--- .../cc.fascinated/model/player/Player.java | 4 ++-- .../cc.fascinated/service/PlayerService.java | 21 ++++++++++--------- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/cc.fascinated/Main.java b/src/main/java/cc.fascinated/Main.java index 9058733..495a788 100644 --- a/src/main/java/cc.fascinated/Main.java +++ b/src/main/java/cc.fascinated/Main.java @@ -43,7 +43,7 @@ public class Main { } @Bean - public WebMvcConfigurer corsConfigurer() { + public WebMvcConfigurer configureCors() { return new WebMvcConfigurer() { @Override public void addCorsMappings(@NonNull CorsRegistry registry) { diff --git a/src/main/java/cc.fascinated/controller/PlayerController.java b/src/main/java/cc.fascinated/controller/PlayerController.java index 4638c58..f027319 100644 --- a/src/main/java/cc.fascinated/controller/PlayerController.java +++ b/src/main/java/cc.fascinated/controller/PlayerController.java @@ -1,6 +1,7 @@ package cc.fascinated.controller; import cc.fascinated.model.cache.CachedPlayer; +import cc.fascinated.model.cache.CachedPlayerName; import cc.fascinated.model.player.Skin; import cc.fascinated.service.PlayerService; import io.swagger.v3.oas.annotations.Parameter; @@ -41,10 +42,10 @@ public class PlayerController { @GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getPlayerUuid( @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { - CachedPlayer player = playerService.getPlayer(id); + CachedPlayerName player = playerService.usernameToUuid(id); return ResponseEntity.ok(Map.of( "username", player.getUsername(), - "uuid", player.getUuid().toString() + "uuid", player.getUniqueId() )); } diff --git a/src/main/java/cc.fascinated/model/cache/CachedPlayer.java b/src/main/java/cc.fascinated/model/cache/CachedPlayer.java index ae3cd3e..32ca42c 100644 --- a/src/main/java/cc.fascinated/model/cache/CachedPlayer.java +++ b/src/main/java/cc.fascinated/model/cache/CachedPlayer.java @@ -27,8 +27,8 @@ public final class CachedPlayer extends Player implements Serializable { */ private long cached; - public CachedPlayer(UUID uuid, String username, Skin skin, Cape cape, MojangProfile.ProfileProperty[] rawProperties, long cached) { - super(uuid, username, skin, cape, rawProperties); + public CachedPlayer(UUID uniqueId, String username, Skin skin, Cape cape, MojangProfile.ProfileProperty[] rawProperties, long cached) { + super(uniqueId, username, skin, cape, rawProperties); this.cached = cached; } } \ No newline at end of file diff --git a/src/main/java/cc.fascinated/model/mojang/MojangUsernameToUuid.java b/src/main/java/cc.fascinated/model/mojang/MojangUsernameToUuid.java index a8a2c0d..2be84bc 100644 --- a/src/main/java/cc.fascinated/model/mojang/MojangUsernameToUuid.java +++ b/src/main/java/cc.fascinated/model/mojang/MojangUsernameToUuid.java @@ -1,5 +1,6 @@ package cc.fascinated.model.mojang; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; import lombok.NoArgsConstructor; @@ -9,12 +10,14 @@ public class MojangUsernameToUuid { /** * The UUID of the player. */ - private String id; + @JsonProperty("id") + private String uuid; /** * The name of the player. */ - private String name; + @JsonProperty("name") + private String username; /** * Check if the profile is valid. @@ -22,6 +25,6 @@ public class MojangUsernameToUuid { * @return if the profile is valid */ public boolean isValid() { - return id != null && name != null; + return uuid != null && username != null; } } diff --git a/src/main/java/cc.fascinated/model/player/Player.java b/src/main/java/cc.fascinated/model/player/Player.java index 3405178..62883bc 100644 --- a/src/main/java/cc.fascinated/model/player/Player.java +++ b/src/main/java/cc.fascinated/model/player/Player.java @@ -15,7 +15,7 @@ public class Player { /** * The UUID of the player */ - @Id private final UUID uuid; + @Id private final UUID uniqueId; /** * The username of the player @@ -40,7 +40,7 @@ public class Player { private MojangProfile.ProfileProperty[] rawProperties; public Player(MojangProfile profile) { - this.uuid = UUIDUtils.addDashes(profile.getId()); + this.uniqueId = UUIDUtils.addDashes(profile.getId()); this.username = profile.getName(); this.rawProperties = profile.getProperties(); diff --git a/src/main/java/cc.fascinated/service/PlayerService.java b/src/main/java/cc.fascinated/service/PlayerService.java index 78c06ba..84cabf1 100644 --- a/src/main/java/cc.fascinated/service/PlayerService.java +++ b/src/main/java/cc.fascinated/service/PlayerService.java @@ -54,7 +54,7 @@ public class PlayerService { log.info("Getting player: {}", originalId); UUID uuid = PlayerUtils.getUuidFromString(originalId); if (uuid == null) { // If the id is not a valid uuid, get the uuid from the username - uuid = usernameToUuid(originalId); + uuid = usernameToUuid(originalId).getUniqueId(); } Optional cachedPlayer = playerCacheRepository.findById(uuid); @@ -91,11 +91,11 @@ public class PlayerService { * @param username the username of the player * @return the uuid of the player */ - public UUID usernameToUuid(String username) { + public CachedPlayerName usernameToUuid(String username) { log.info("Getting UUID from username: {}", username); Optional cachedPlayerName = playerNameCacheRepository.findById(username.toUpperCase()); if (cachedPlayerName.isPresent()) { - return cachedPlayerName.get().getUniqueId(); + return cachedPlayerName.get(); } try { MojangUsernameToUuid mojangUsernameToUuid = mojangAPIService.getUuidFromUsername(username); @@ -103,10 +103,11 @@ public class PlayerService { log.info("Player with username '{}' not found", username); throw new ResourceNotFoundException("Player with username '%s' not found".formatted(username)); } - UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getId()); - playerNameCacheRepository.save(new CachedPlayerName(username, uuid)); + UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getUuid()); + CachedPlayerName player = new CachedPlayerName(username, uuid); + playerNameCacheRepository.save(player); log.info("Got UUID from username: {} -> {}", username, uuid); - return uuid; + return player; } catch (RateLimitException exception) { throw new MojangAPIRateLimitException(); } @@ -120,13 +121,13 @@ public class PlayerService { * @return the skin part */ public CachedPlayerSkinPart getSkinPart(Player player, Skin.Parts part, int size) { - log.info("Getting skin part: {} for player: {}", part.getName(), player.getUuid()); - String key = "%s-%s-%s".formatted(player.getUuid(), part.getName(), size); + log.info("Getting skin part: {} for player: {}", part.getName(), player.getUniqueId()); + String key = "%s-%s-%s".formatted(player.getUniqueId(), part.getName(), size); Optional cache = playerSkinPartCacheRepository.findById(key); // The skin part is cached if (cache.isPresent()) { - log.info("Skin part {} for player {} is cached", part.getName(), player.getUuid()); + log.info("Skin part {} for player {} is cached", part.getName(), player.getUniqueId()); return cache.get(); } @@ -135,7 +136,7 @@ public class PlayerService { key, skinPartBytes ); - log.info("Fetched skin part: {} for player: {}", part.getName(), player.getUuid()); + log.info("Fetched skin part: {} for player: {}", part.getName(), player.getUniqueId()); playerSkinPartCacheRepository.save(skinPart); return skinPart;