Skip to content

Commit

Permalink
fallback to minetools player identity lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 7, 2023
1 parent 8b3e4c7 commit 17c6849
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/main/java/vc/util/PlayerLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import vc.swagger.minetools_api.handler.UuidApi;
import vc.swagger.minetools_api.model.UUIDAndPlayerName;
import vc.swagger.mojang_api.handler.ProfileApi;
import vc.swagger.mojang_api.model.UUIDAndUser;

Expand All @@ -19,13 +21,15 @@
public class PlayerLookup {
private static final Logger logger = LoggerFactory.getLogger(PlayerLookup.class);
private final ProfileApi mojangApi;
private final UuidApi mineToolsApi;
private final Cache<String, PlayerIdentity> uuidCache = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofHours(12))
.maximumSize(250)
.build();

public PlayerLookup(final ProfileApi mojangApi) {
public PlayerLookup(final ProfileApi mojangApi, final UuidApi mineToolsApi) {
this.mojangApi = mojangApi;
this.mineToolsApi = mineToolsApi;
}

public record PlayerIdentity(UUID uuid, String playerName) { }
Expand All @@ -34,9 +38,34 @@ public Optional<PlayerIdentity> getPlayerIdentity(final String playerName) {
final PlayerIdentity identityFromCache = uuidCache.getIfPresent(playerName);
if (identityFromCache != null)
return Optional.of(identityFromCache);

Optional<PlayerIdentity> playerIdentity = lookupIdentityMojang(playerName).or(() -> lookupIdentityMinetools(playerName));
playerIdentity.ifPresent(identity -> uuidCache.put(playerName, identity));
return playerIdentity;
}

private Optional<PlayerIdentity> lookupIdentityMojang(final String playerName) {
try {
UUIDAndUser uuidAndUsername = mojangApi.getProfileFromUsername(playerName);
if (uuidAndUsername == null) return Optional.empty();
final PlayerIdentity playerIdentity = new PlayerIdentity(
UUID.fromString(uuidAndUsername
.getId()
.replaceFirst(
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)",
"$1-$2-$3-$4-$5")
), uuidAndUsername.getName());
return Optional.of(playerIdentity);
} catch (final Exception e) {
logger.error("Error while looking up player identity using Mojang API", e);
return Optional.empty();
}
}

private Optional<PlayerIdentity> lookupIdentityMinetools(final String playerName) {
try {
UUIDAndPlayerName uuidAndUsername = mineToolsApi.getUUIDAndPlayerName(playerName);
if (uuidAndUsername == null) return Optional.empty();
final PlayerIdentity playerIdentity = new PlayerIdentity(
UUID.fromString(uuidAndUsername
.getId()
Expand All @@ -47,7 +76,7 @@ public Optional<PlayerIdentity> getPlayerIdentity(final String playerName) {
uuidCache.put(playerName, playerIdentity);
return Optional.of(playerIdentity);
} catch (final Exception e) {
logger.error("Error while looking up player identity", e);
logger.error("Error while looking up player identity using Minetools API", e);
return Optional.empty();
}
}
Expand Down

0 comments on commit 17c6849

Please sign in to comment.