cleanup username to uuid endpoint
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m32s

This commit is contained in:
Lee 2024-04-11 00:49:16 +01:00
parent f2e8360567
commit a11a90f530
6 changed files with 25 additions and 20 deletions

@ -43,7 +43,7 @@ public class Main {
}
@Bean
public WebMvcConfigurer corsConfigurer() {
public WebMvcConfigurer configureCors() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(@NonNull CorsRegistry registry) {

@ -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()
));
}

@ -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;
}
}

@ -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;
}
}

@ -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();

@ -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> 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> 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<CachedPlayerSkinPart> 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;