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);
});
}
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);
}
}
}
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();
}
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);
}
}
Aggregations