use of com.github.games647.changeskin.core.RateLimitException 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();
}
use of com.github.games647.changeskin.core.RateLimitException 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);
}
}
use of com.github.games647.changeskin.core.RateLimitException 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;
}
Aggregations