use of org.spongepowered.api.profile.ProfileNotFoundException in project LanternServer by LanternPowered.
the class GameProfileQuery method queryProfileByUUID.
static GameProfile queryProfileByUUID(UUID uniqueId, boolean signed) throws IOException, ProfileNotFoundException {
final URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + UUIDHelper.toFlatString(uniqueId) + (signed ? "?unsigned=false" : ""));
int attempts = 0;
while (true) {
final URLConnection uc = url.openConnection();
final InputStream is = uc.getInputStream();
// Can be empty if the unique id invalid is
if (is.available() == 0) {
throw new ProfileNotFoundException("Failed to find a profile with the uuid: " + uniqueId);
}
// If it fails too many times, just leave it
if (++attempts > 6) {
throw new IOException("Failed to retrieve the profile after 6 attempts: " + uniqueId);
}
final JsonObject json = GSON.fromJson(new InputStreamReader(is), JsonObject.class);
if (json.has("error")) {
// Too many requests, lets wait for 10 seconds
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new IOException("Something interrupted the next attempt delay.");
}
continue;
}
final String name = json.get("name").getAsString();
final Multimap<String, ProfileProperty> properties;
if (json.has("properties")) {
properties = LanternProfileProperty.createPropertiesMapFromJson(json.get("properties").getAsJsonArray());
} else {
properties = LinkedHashMultimap.create();
}
return new LanternGameProfile(uniqueId, name, properties);
}
}
use of org.spongepowered.api.profile.ProfileNotFoundException in project SpongeCommon by SpongePowered.
the class Query method fillProfile.
protected GameProfile fillProfile(GameProfile profile, boolean signed) throws ProfileNotFoundException {
if (this.useCache) {
Optional<GameProfile> result = this.cache.getById(profile.getUniqueId());
if (result.isPresent() && result.get().isFilled() && !result.get().getPropertyMap().isEmpty()) {
return result.get();
}
}
Optional<GameProfile> result = this.cache.fillProfile(profile, signed);
if (result.isPresent() && result.get().isFilled()) {
GameProfile filled = result.get();
this.cache.add(filled, true, (Instant) null);
return filled;
}
throw new ProfileNotFoundException("Profile: " + profile);
}
use of org.spongepowered.api.profile.ProfileNotFoundException in project LanternServer by LanternPowered.
the class LanternGameProfileCache method lookupById.
@Override
public Optional<GameProfile> lookupById(UUID uniqueId) {
try {
final GameProfile gameProfile = GameProfileQuery.queryProfileByUUID(uniqueId, true);
add(gameProfile, true, (Instant) null);
return Optional.of(gameProfile);
} catch (IOException e) {
Lantern.getLogger().warn("An error occurred while retrieving game profile data.", e);
} catch (ProfileNotFoundException ignored) {
}
return Optional.empty();
}
use of org.spongepowered.api.profile.ProfileNotFoundException in project LanternServer by LanternPowered.
the class LanternGameProfileManager method fill.
@Override
public CompletableFuture<GameProfile> fill(GameProfile profile, boolean signed, boolean useCache) {
checkNotNull(profile, "profile");
return Lantern.getScheduler().submitAsyncTask(() -> {
if (useCache) {
// Load the profile into the cache
this.gameProfileCache.getOrLookupById(profile.getUniqueId());
final Optional<GameProfile> optProfile = this.gameProfileCache.fillProfile(profile, signed);
if (optProfile.isPresent()) {
return optProfile.get();
}
throw new ProfileNotFoundException("Failed to find a profile with the uuid: " + profile.getUniqueId());
}
final GameProfile gameProfile = getById(profile.getUniqueId(), false, signed);
((LanternGameProfile) profile).setName(gameProfile.getName().get());
profile.getPropertyMap().putAll(gameProfile.getPropertyMap());
return profile;
});
}
Aggregations