Search in sources :

Example 1 with NotPremiumException

use of com.github.games647.changeskin.core.NotPremiumException 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 2 with NotPremiumException

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

the class SharedNameResolver method run.

@Override
public void run() {
    UUID uuid = core.getUuidCache().get(targetName);
    if (uuid == null) {
        if (core.getCrackedNames().containsKey(targetName)) {
            sendMessageInvoker("not-premium");
            return;
        }
        try {
            Optional<UUID> optUUID = core.getSkinApi().getUUID(targetName);
            if (optUUID.isPresent()) {
                uuid = optUUID.get();
                core.getUuidCache().put(targetName, uuid);
            } else {
                sendMessageInvoker("no-resolve");
            }
        } catch (NotPremiumException notPremiumEx) {
            core.getCrackedNames().put(targetName, new Object());
            sendMessageInvoker("not-premium");
        } catch (RateLimitException rateLimitEx) {
            sendMessageInvoker("rate-limit");
        }
    }
    if (uuid != null) {
        sendMessageInvoker("uuid-resolved");
        if (!hasSkinPermission(uuid)) {
            return;
        }
        scheduleDownloader(uuid);
    }
}
Also used : RateLimitException(com.github.games647.changeskin.core.RateLimitException) NotPremiumException(com.github.games647.changeskin.core.NotPremiumException) UUID(java.util.UUID)

Example 3 with NotPremiumException

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

the class SharedListener method refetchSkin.

protected boolean refetchSkin(String playerName, UserPreference preferences) {
    UUID ownerUUID = core.getUuidCache().get(playerName);
    if (ownerUUID == null && !core.getCrackedNames().containsKey(playerName)) {
        try {
            Optional<UUID> optUUID = core.getSkinApi().getUUID(playerName);
            if (optUUID.isPresent()) {
                ownerUUID = optUUID.get();
            }
        } catch (NotPremiumException ex) {
            core.getCrackedNames().put(playerName, new Object());
        } catch (RateLimitException ex) {
        // ignore
        }
    }
    if (ownerUUID != null) {
        core.getUuidCache().put(playerName, ownerUUID);
        SkinModel storedSkin = core.checkAutoUpdate(core.getStorage().getSkin(ownerUUID));
        if (storedSkin == null) {
            storedSkin = core.getSkinApi().downloadSkin(ownerUUID).orElse(null);
        }
        preferences.setTargetSkin(storedSkin);
        save(preferences);
        return true;
    }
    return false;
}
Also used : RateLimitException(com.github.games647.changeskin.core.RateLimitException) NotPremiumException(com.github.games647.changeskin.core.NotPremiumException) UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Aggregations

NotPremiumException (com.github.games647.changeskin.core.NotPremiumException)2 RateLimitException (com.github.games647.changeskin.core.RateLimitException)2 UUID (java.util.UUID)2 GameProfile (com.github.games647.changeskin.core.model.GameProfile)1 SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)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