Esempio n. 1
0
  public UUID getUUID(String playerName) throws NotPremiumException, RateLimitException {
    logger.log(Level.FINE, "Making UUID->Name request for {0}", playerName);
    if (!playerName.matches(VALID_USERNAME)) {
      throw new NotPremiumException(playerName);
    }

    if (requests.size() >= rateLimit
        || System.currentTimeMillis() - lastRateLimit < 1_000 * 60 * 10) {
      //            logger.fine("STILL WAITING FOR RATE_LIMIT - TRYING SECOND API");
      return getUUIDFromAPI(playerName);
    }

    requests.put(new Object(), new Object());

    BufferedReader reader = null;
    try {

      HttpURLConnection httpConnection = ChangeSkinCore.getConnection(UUID_URL + playerName);
      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
        throw new NotPremiumException(playerName);
      } else if (httpConnection.getResponseCode() == RATE_LIMIT_ID) {
        logger.info("RATE_LIMIT REACHED - TRYING THIRD-PARTY API");
        lastRateLimit = System.currentTimeMillis();
        return getUUIDFromAPI(playerName);
      }

      InputStreamReader inputReader = new InputStreamReader(httpConnection.getInputStream());
      reader = new BufferedReader(inputReader);
      String line = reader.readLine();
      if (line != null && !line.equals("null")) {
        PlayerProfile playerProfile = gson.fromJson(line, PlayerProfile.class);
        String id = playerProfile.getId();
        return ChangeSkinCore.parseId(id);
      }
    } catch (IOException | JsonParseException ex) {
      logger.log(Level.SEVERE, "Tried converting player name to uuid", ex);
    } finally {
      ChangeSkinCore.closeQuietly(reader, logger);
    }

    return null;
  }
Esempio n. 2
0
  public UUID getUUIDFromAPI(String playerName) throws NotPremiumException {
    InputStreamReader inputReader = null;
    try {
      HttpURLConnection httpConnection = ChangeSkinCore.getConnection(MCAPI_UUID_URL + playerName);

      inputReader = new InputStreamReader(httpConnection.getInputStream());
      String line = CharStreams.toString(inputReader);
      if (line != null && !line.equals("null")) {
        PlayerProfile playerProfile = gson.fromJson(line, PlayerProfile[].class)[0];
        String id = playerProfile.getId();
        if (id == null || id.equalsIgnoreCase("null")) {
          throw new NotPremiumException(line);
        }

        return ChangeSkinCore.parseId(id);
      }
    } catch (IOException | JsonParseException ex) {
      logger.log(Level.SEVERE, "Tried converting player name to uuid from third-party api", ex);
    } finally {
      ChangeSkinCore.closeQuietly(inputReader, logger);
    }

    return null;
  }