Search in sources :

Example 1 with ProfileNotFoundException

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);
    }
}
Also used : ProfileProperty(org.spongepowered.api.profile.property.ProfileProperty) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ProfileNotFoundException(org.spongepowered.api.profile.ProfileNotFoundException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 2 with ProfileNotFoundException

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);
}
Also used : GameProfile(org.spongepowered.api.profile.GameProfile) ProfileNotFoundException(org.spongepowered.api.profile.ProfileNotFoundException)

Example 3 with ProfileNotFoundException

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();
}
Also used : GameProfile(org.spongepowered.api.profile.GameProfile) IOException(java.io.IOException) ProfileNotFoundException(org.spongepowered.api.profile.ProfileNotFoundException)

Example 4 with ProfileNotFoundException

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;
    });
}
Also used : GameProfile(org.spongepowered.api.profile.GameProfile) ProfileNotFoundException(org.spongepowered.api.profile.ProfileNotFoundException)

Aggregations

ProfileNotFoundException (org.spongepowered.api.profile.ProfileNotFoundException)4 GameProfile (org.spongepowered.api.profile.GameProfile)3 IOException (java.io.IOException)2 JsonObject (com.google.gson.JsonObject)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ProfileProperty (org.spongepowered.api.profile.property.ProfileProperty)1