Search in sources :

Example 1 with GameProfile

use of com.github.games647.changeskin.core.model.GameProfile in project ChangeSkin by games647.

the class SharedUploader method run.

@Override
public void run() {
    GameProfile profile = owner.getProfile();
    String oldSkinUrl = core.getSkinApi().downloadSkin(profile.getId()).map(skinModel -> skinModel.getTextures().get(TextureType.SKIN).getUrl()).orElse("");
    UUID uuid = profile.getId();
    UUID accessToken = UUIDTypeAdapter.parseId(owner.getAccessToken());
    core.getAuthApi().changeSkin(uuid, accessToken, url, false);
    // this could properly cause issues for uuid resolving to this database entry
    core.getSkinApi().downloadSkin(uuid).ifPresent(skinData -> {
        core.getStorage().save(skinData);
        core.getAuthApi().changeSkin(uuid, accessToken, oldSkinUrl, false);
        String localeMessage = core.getMessage("skin-uploaded").replace("{0}", owner.getProfile().getName()).replace("{1}", "Skin-" + skinData.getRowId());
        sendMessageInvoker(localeMessage);
    });
}
Also used : ChangeSkinCore(com.github.games647.changeskin.core.ChangeSkinCore) UUIDTypeAdapter(com.github.games647.changeskin.core.model.UUIDTypeAdapter) GameProfile(com.github.games647.changeskin.core.model.GameProfile) Account(com.github.games647.changeskin.core.model.auth.Account) UUID(java.util.UUID) TextureType(com.github.games647.changeskin.core.model.skin.TextureType) GameProfile(com.github.games647.changeskin.core.model.GameProfile) UUID(java.util.UUID)

Example 2 with GameProfile

use of com.github.games647.changeskin.core.model.GameProfile in project ChangeSkin by games647.

the class LoginListener method onPlayerPreLogin.

@Listener
public void onPlayerPreLogin(ClientConnectionEvent.Auth preLoginEvent) {
    SkinStorage storage = core.getStorage();
    GameProfile profile = preLoginEvent.getProfile();
    UUID playerUUID = profile.getUniqueId();
    UserPreference preferences = storage.getPreferences(playerUUID);
    Optional<SkinModel> optSkin = preferences.getTargetSkin();
    if (optSkin.isPresent()) {
        SkinModel targetSkin = optSkin.get();
        if (!preferences.isKeepSkin()) {
            targetSkin = core.checkAutoUpdate(targetSkin);
        }
        plugin.getApi().applyProperties(profile, targetSkin);
        save(preferences);
    } else {
        String playerName = profile.getName().get();
        if (!core.getConfig().getBoolean("restoreSkins") || !refetchSkin(playerName, preferences)) {
            setDefaultSkin(preferences, profile);
        }
    }
}
Also used : GameProfile(org.spongepowered.api.profile.GameProfile) UserPreference(com.github.games647.changeskin.core.model.UserPreference) UUID(java.util.UUID) SkinStorage(com.github.games647.changeskin.core.SkinStorage) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel) Listener(org.spongepowered.api.event.Listener) SharedListener(com.github.games647.changeskin.core.shared.SharedListener)

Example 3 with GameProfile

use of com.github.games647.changeskin.core.model.GameProfile in project ChangeSkin by games647.

the class MojangSkinApi method getUUID.

public Optional<UUID> getUUID(String playerName) throws NotPremiumException, RateLimitException {
    logger.debug("Making UUID->Name request for {}", playerName);
    if (!validNamePattern.matcher(playerName).matches()) {
        throw new NotPremiumException(playerName);
    }
    Proxy proxy = null;
    try {
        HttpURLConnection connection;
        if (requests.size() >= rateLimit || Duration.between(lastRateLimit, Instant.now()).getSeconds() < 60 * 10) {
            synchronized (proxies) {
                if (proxies.hasNext()) {
                    proxy = proxies.next();
                    connection = getConnection(UUID_URL + playerName, proxy);
                } else {
                    return Optional.empty();
                }
            }
        } else {
            requests.put(new Object(), new Object());
            connection = getConnection(UUID_URL + playerName);
        }
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            throw new NotPremiumException(playerName);
        } else if (responseCode == RATE_LIMIT_ID) {
            logger.info("Mojang's rate-limit reached. The public IPv4 address of this server issued more than 600" + " Name -> UUID requests within 10 minutes. Once those 10 minutes ended we could make requests" + " again. In the meanwhile new skins can only be downloaded using the UUID directly." + " If you are using BungeeCord, consider adding a caching server in order to prevent multiple" + " spigot servers creating the same requests against Mojang's servers.");
            lastRateLimit = Instant.now();
            if (!connection.usingProxy()) {
                return getUUID(playerName);
            } else {
                throw new RateLimitException("Rate-Limit hit on request name->uuid of " + playerName);
            }
        }
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                GameProfile playerProfile = gson.fromJson(reader, GameProfile.class);
                return Optional.of(playerProfile.getId());
            }
        } else {
            logger.error("Received response code: {} for {} using proxy: {}", responseCode, playerName, proxy);
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                logger.error("Error stream: {}", CharStreams.toString(reader));
            }
        }
    } catch (IOException ioEx) {
        logger.error("Tried converting player name: {} to uuid", playerName, ioEx);
    }
    return Optional.empty();
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GameProfile(com.github.games647.changeskin.core.model.GameProfile) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 4 with GameProfile

use of com.github.games647.changeskin.core.model.GameProfile in project ChangeSkin by games647.

the class LoginListener method setDefaultSkin.

private void setDefaultSkin(UserPreference preferences, GameProfile profile) {
    Optional<SkinModel> randomSkin = getRandomSkin();
    if (randomSkin.isPresent()) {
        SkinModel targetSkin = randomSkin.get();
        preferences.setTargetSkin(targetSkin);
        plugin.getApi().applyProperties(profile, targetSkin);
    }
}
Also used : SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Aggregations

GameProfile (com.github.games647.changeskin.core.model.GameProfile)2 SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)2 UUID (java.util.UUID)2 ChangeSkinCore (com.github.games647.changeskin.core.ChangeSkinCore)1 SkinStorage (com.github.games647.changeskin.core.SkinStorage)1 UUIDTypeAdapter (com.github.games647.changeskin.core.model.UUIDTypeAdapter)1 UserPreference (com.github.games647.changeskin.core.model.UserPreference)1 Account (com.github.games647.changeskin.core.model.auth.Account)1 TextureType (com.github.games647.changeskin.core.model.skin.TextureType)1 SharedListener (com.github.games647.changeskin.core.shared.SharedListener)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 Proxy (java.net.Proxy)1 Listener (org.spongepowered.api.event.Listener)1 GameProfile (org.spongepowered.api.profile.GameProfile)1