forked from MinecraftUtilities/Backend
cleanup username to uuid endpoint
This commit is contained in:
parent
f2e8360567
commit
a11a90f530
@ -43,7 +43,7 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WebMvcConfigurer corsConfigurer() {
|
public WebMvcConfigurer configureCors() {
|
||||||
return new WebMvcConfigurer() {
|
return new WebMvcConfigurer() {
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cc.fascinated.controller;
|
package cc.fascinated.controller;
|
||||||
|
|
||||||
import cc.fascinated.model.cache.CachedPlayer;
|
import cc.fascinated.model.cache.CachedPlayer;
|
||||||
|
import cc.fascinated.model.cache.CachedPlayerName;
|
||||||
import cc.fascinated.model.player.Skin;
|
import cc.fascinated.model.player.Skin;
|
||||||
import cc.fascinated.service.PlayerService;
|
import cc.fascinated.service.PlayerService;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
@ -41,10 +42,10 @@ public class PlayerController {
|
|||||||
@GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
@GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
public ResponseEntity<?> getPlayerUuid(
|
public ResponseEntity<?> getPlayerUuid(
|
||||||
@Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) {
|
@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(
|
return ResponseEntity.ok(Map.of(
|
||||||
"username", player.getUsername(),
|
"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;
|
private long cached;
|
||||||
|
|
||||||
public CachedPlayer(UUID uuid, String username, Skin skin, Cape cape, MojangProfile.ProfileProperty[] rawProperties, long cached) {
|
public CachedPlayer(UUID uniqueId, String username, Skin skin, Cape cape, MojangProfile.ProfileProperty[] rawProperties, long cached) {
|
||||||
super(uuid, username, skin, cape, rawProperties);
|
super(uniqueId, username, skin, cape, rawProperties);
|
||||||
this.cached = cached;
|
this.cached = cached;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package cc.fascinated.model.mojang;
|
package cc.fascinated.model.mojang;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@ -9,12 +10,14 @@ public class MojangUsernameToUuid {
|
|||||||
/**
|
/**
|
||||||
* The UUID of the player.
|
* The UUID of the player.
|
||||||
*/
|
*/
|
||||||
private String id;
|
@JsonProperty("id")
|
||||||
|
private String uuid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the player.
|
* The name of the player.
|
||||||
*/
|
*/
|
||||||
private String name;
|
@JsonProperty("name")
|
||||||
|
private String username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the profile is valid.
|
* Check if the profile is valid.
|
||||||
@ -22,6 +25,6 @@ public class MojangUsernameToUuid {
|
|||||||
* @return if the profile is valid
|
* @return if the profile is valid
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
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
|
* The UUID of the player
|
||||||
*/
|
*/
|
||||||
@Id private final UUID uuid;
|
@Id private final UUID uniqueId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The username of the player
|
* The username of the player
|
||||||
@ -40,7 +40,7 @@ public class Player {
|
|||||||
private MojangProfile.ProfileProperty[] rawProperties;
|
private MojangProfile.ProfileProperty[] rawProperties;
|
||||||
|
|
||||||
public Player(MojangProfile profile) {
|
public Player(MojangProfile profile) {
|
||||||
this.uuid = UUIDUtils.addDashes(profile.getId());
|
this.uniqueId = UUIDUtils.addDashes(profile.getId());
|
||||||
this.username = profile.getName();
|
this.username = profile.getName();
|
||||||
this.rawProperties = profile.getProperties();
|
this.rawProperties = profile.getProperties();
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public class PlayerService {
|
|||||||
log.info("Getting player: {}", originalId);
|
log.info("Getting player: {}", originalId);
|
||||||
UUID uuid = PlayerUtils.getUuidFromString(originalId);
|
UUID uuid = PlayerUtils.getUuidFromString(originalId);
|
||||||
if (uuid == null) { // If the id is not a valid uuid, get the uuid from the username
|
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);
|
Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid);
|
||||||
@ -91,11 +91,11 @@ public class PlayerService {
|
|||||||
* @param username the username of the player
|
* @param username the username of the player
|
||||||
* @return the uuid 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);
|
log.info("Getting UUID from username: {}", username);
|
||||||
Optional<CachedPlayerName> cachedPlayerName = playerNameCacheRepository.findById(username.toUpperCase());
|
Optional<CachedPlayerName> cachedPlayerName = playerNameCacheRepository.findById(username.toUpperCase());
|
||||||
if (cachedPlayerName.isPresent()) {
|
if (cachedPlayerName.isPresent()) {
|
||||||
return cachedPlayerName.get().getUniqueId();
|
return cachedPlayerName.get();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
MojangUsernameToUuid mojangUsernameToUuid = mojangAPIService.getUuidFromUsername(username);
|
MojangUsernameToUuid mojangUsernameToUuid = mojangAPIService.getUuidFromUsername(username);
|
||||||
@ -103,10 +103,11 @@ public class PlayerService {
|
|||||||
log.info("Player with username '{}' not found", username);
|
log.info("Player with username '{}' not found", username);
|
||||||
throw new ResourceNotFoundException("Player with username '%s' not found".formatted(username));
|
throw new ResourceNotFoundException("Player with username '%s' not found".formatted(username));
|
||||||
}
|
}
|
||||||
UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getId());
|
UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getUuid());
|
||||||
playerNameCacheRepository.save(new CachedPlayerName(username, uuid));
|
CachedPlayerName player = new CachedPlayerName(username, uuid);
|
||||||
|
playerNameCacheRepository.save(player);
|
||||||
log.info("Got UUID from username: {} -> {}", username, uuid);
|
log.info("Got UUID from username: {} -> {}", username, uuid);
|
||||||
return uuid;
|
return player;
|
||||||
} catch (RateLimitException exception) {
|
} catch (RateLimitException exception) {
|
||||||
throw new MojangAPIRateLimitException();
|
throw new MojangAPIRateLimitException();
|
||||||
}
|
}
|
||||||
@ -120,13 +121,13 @@ public class PlayerService {
|
|||||||
* @return the skin part
|
* @return the skin part
|
||||||
*/
|
*/
|
||||||
public CachedPlayerSkinPart getSkinPart(Player player, Skin.Parts part, int size) {
|
public CachedPlayerSkinPart getSkinPart(Player player, Skin.Parts part, int size) {
|
||||||
log.info("Getting skin part: {} for player: {}", part.getName(), player.getUuid());
|
log.info("Getting skin part: {} for player: {}", part.getName(), player.getUniqueId());
|
||||||
String key = "%s-%s-%s".formatted(player.getUuid(), part.getName(), size);
|
String key = "%s-%s-%s".formatted(player.getUniqueId(), part.getName(), size);
|
||||||
Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key);
|
Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key);
|
||||||
|
|
||||||
// The skin part is cached
|
// The skin part is cached
|
||||||
if (cache.isPresent()) {
|
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();
|
return cache.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +136,7 @@ public class PlayerService {
|
|||||||
key,
|
key,
|
||||||
skinPartBytes
|
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);
|
playerSkinPartCacheRepository.save(skinPart);
|
||||||
return skinPart;
|
return skinPart;
|
||||||
|
Loading…
Reference in New Issue
Block a user