2024-04-10 07:43:38 +01:00
|
|
|
package cc.fascinated.service;
|
|
|
|
|
|
|
|
import cc.fascinated.common.PlayerUtils;
|
|
|
|
import cc.fascinated.common.Tuple;
|
|
|
|
import cc.fascinated.common.UUIDUtils;
|
2024-04-10 09:30:35 +01:00
|
|
|
import cc.fascinated.exception.impl.MojangAPIRateLimitException;
|
|
|
|
import cc.fascinated.exception.impl.RateLimitException;
|
2024-04-10 07:43:38 +01:00
|
|
|
import cc.fascinated.exception.impl.ResourceNotFoundException;
|
|
|
|
import cc.fascinated.model.cache.CachedPlayer;
|
|
|
|
import cc.fascinated.model.cache.CachedPlayerName;
|
|
|
|
import cc.fascinated.model.mojang.MojangProfile;
|
|
|
|
import cc.fascinated.model.mojang.MojangUsernameToUuid;
|
|
|
|
import cc.fascinated.model.player.Cape;
|
|
|
|
import cc.fascinated.model.player.Skin;
|
|
|
|
import cc.fascinated.repository.PlayerCacheRepository;
|
|
|
|
import cc.fascinated.repository.PlayerNameCacheRepository;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Service @Log4j2
|
|
|
|
public class PlayerService {
|
|
|
|
|
2024-04-10 11:39:17 +01:00
|
|
|
private final MojangService mojangAPIService;
|
2024-04-10 07:43:38 +01:00
|
|
|
private final PlayerCacheRepository playerCacheRepository;
|
|
|
|
private final PlayerNameCacheRepository playerNameCacheRepository;
|
|
|
|
|
|
|
|
@Autowired
|
2024-04-10 11:39:17 +01:00
|
|
|
public PlayerService(MojangService mojangAPIService, PlayerCacheRepository playerCacheRepository, PlayerNameCacheRepository playerNameCacheRepository) {
|
2024-04-10 07:43:38 +01:00
|
|
|
this.mojangAPIService = mojangAPIService;
|
|
|
|
this.playerCacheRepository = playerCacheRepository;
|
|
|
|
this.playerNameCacheRepository = playerNameCacheRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a player from the cache or
|
|
|
|
* from the Mojang API.
|
|
|
|
*
|
|
|
|
* @param id the id of the player
|
|
|
|
* @return the player
|
|
|
|
*/
|
|
|
|
public CachedPlayer getPlayer(String id) {
|
2024-04-10 10:36:31 +01:00
|
|
|
String originalId = id;
|
2024-04-10 09:30:35 +01:00
|
|
|
id = id.toUpperCase(); // Convert the id to uppercase to prevent case sensitivity
|
2024-04-10 10:36:31 +01:00
|
|
|
log.info("Getting player: {}", originalId);
|
|
|
|
UUID uuid = PlayerUtils.getUuidFromString(originalId);
|
2024-04-10 07:43:38 +01:00
|
|
|
if (uuid == null) { // If the id is not a valid uuid, get the uuid from the username
|
2024-04-10 12:16:24 +01:00
|
|
|
uuid = usernameToUuid(originalId);
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid);
|
|
|
|
if (cachedPlayer.isPresent()) { // Return the cached player if it exists
|
2024-04-10 10:36:31 +01:00
|
|
|
log.info("Player {} is cached", originalId);
|
2024-04-10 07:43:38 +01:00
|
|
|
return cachedPlayer.get();
|
|
|
|
}
|
|
|
|
|
2024-04-10 09:30:35 +01:00
|
|
|
try {
|
2024-04-10 10:36:31 +01:00
|
|
|
log.info("Getting player profile from Mojang: {}", originalId);
|
2024-04-10 09:30:35 +01:00
|
|
|
MojangProfile mojangProfile = mojangAPIService.getProfile(uuid.toString()); // Get the player profile from Mojang
|
2024-04-10 10:36:31 +01:00
|
|
|
log.info("Got player profile from Mojang: {}", originalId);
|
2024-04-10 09:30:35 +01:00
|
|
|
Tuple<Skin, Cape> skinAndCape = mojangProfile.getSkinAndCape();
|
|
|
|
CachedPlayer player = new CachedPlayer(
|
|
|
|
uuid, // Player UUID
|
|
|
|
mojangProfile.getName(), // Player Name
|
|
|
|
skinAndCape.getLeft(), // Skin
|
|
|
|
skinAndCape.getRight(), // Cape
|
|
|
|
System.currentTimeMillis() // Cache time
|
|
|
|
);
|
2024-04-10 07:43:38 +01:00
|
|
|
|
2024-04-10 09:30:35 +01:00
|
|
|
playerCacheRepository.save(player);
|
|
|
|
player.setCached(-1); // Indicate that the player is not cached
|
|
|
|
return player;
|
|
|
|
} catch (RateLimitException exception) {
|
|
|
|
throw new MojangAPIRateLimitException();
|
|
|
|
}
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the player's uuid from their username.
|
|
|
|
*
|
|
|
|
* @param username the username of the player
|
|
|
|
* @return the uuid of the player
|
|
|
|
*/
|
|
|
|
private UUID usernameToUuid(String username) {
|
2024-04-10 09:51:31 +01:00
|
|
|
log.info("Getting UUID from username: {}", username);
|
2024-04-10 12:17:27 +01:00
|
|
|
Optional<CachedPlayerName> cachedPlayerName = playerNameCacheRepository.findById(username.toUpperCase());
|
2024-04-10 07:43:38 +01:00
|
|
|
if (cachedPlayerName.isPresent()) {
|
|
|
|
return cachedPlayerName.get().getUniqueId();
|
|
|
|
}
|
2024-04-10 09:30:35 +01:00
|
|
|
try {
|
|
|
|
MojangUsernameToUuid mojangUsernameToUuid = mojangAPIService.getUuidFromUsername(username);
|
|
|
|
if (mojangUsernameToUuid == null) {
|
2024-04-10 09:51:31 +01:00
|
|
|
log.info("Player with username '{}' not found", username);
|
2024-04-10 09:30:35 +01:00
|
|
|
throw new ResourceNotFoundException("Player with username '%s' not found".formatted(username));
|
|
|
|
}
|
|
|
|
UUID uuid = UUIDUtils.addDashes(mojangUsernameToUuid.getId());
|
|
|
|
playerNameCacheRepository.save(new CachedPlayerName(username, uuid));
|
2024-04-10 09:51:31 +01:00
|
|
|
log.info("Got UUID from username: {} -> {}", username, uuid);
|
2024-04-10 09:30:35 +01:00
|
|
|
return uuid;
|
|
|
|
} catch (RateLimitException exception) {
|
|
|
|
throw new MojangAPIRateLimitException();
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|