Skip to content

Commit

Permalink
api.ashcon.app v2 was temporary disabled, so do not use it until it w…
Browse files Browse the repository at this point in the history
…ill be work again
  • Loading branch information
Leymooo committed Jan 24, 2019
1 parent aa7a9e4 commit b38fa09
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 69 deletions.
12 changes: 6 additions & 6 deletions src/main/java/ru/leymooo/simpleskins/SimpleSkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ru.leymooo.simpleskins.utils.UuidFetchCache;
import ru.leymooo.simpleskins.utils.RoundIterator;
import ru.leymooo.simpleskins.utils.DataBaseUtils;
import ru.leymooo.simpleskins.utils.SkinFetcher;
import ru.leymooo.simpleskins.utils.skinfetch.SkinFetcher;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.GameProfileRequestEvent;
Expand Down Expand Up @@ -35,9 +35,9 @@
import net.md_5.bungee.config.YamlConfiguration;
import ru.leymooo.simpleskins.command.SkinCommand;
import ru.leymooo.simpleskins.utils.SkinApplier;
import ru.leymooo.simpleskins.utils.SkinFetcher.FetchResult;
import ru.leymooo.simpleskins.utils.skinfetch.FetchResult;

@Plugin(id = "simpleskins", name = "SimpleSkins", version = "1.2",
@Plugin(id = "simpleskins", name = "SimpleSkins", version = "1.3",
description = "Simple skins restorer plugin for velocity",
authors = "Leymooo")
public class SimpleSkins {
Expand All @@ -48,7 +48,7 @@ public class SimpleSkins {
private SkinFetcher skinFetcher;
private final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
private final DataBaseUtils dataBaseUtils;
private RoundIterator<SkinFetcher.FetchResult> defaultSkins;
private RoundIterator<FetchResult> defaultSkins;
private Configuration config;

@Inject
Expand Down Expand Up @@ -131,11 +131,11 @@ private boolean loadConfig() {

private void initDefaultSkins() {
logger.info("Loading default skins");
List<SkinFetcher.FetchResult> skins = new ArrayList<>();
List<FetchResult> skins = new ArrayList<>();
List<Future<?>> futures = new ArrayList<>();
for (String user : config.getStringList("default-skins")) {
futures.add(service.submit(() -> skinFetcher.fetchSkin(user, false)
.ifPresent(skin -> skins.add(new SkinFetcher.FetchResult(null, skin.getProperty())))));
.ifPresent(skin -> skins.add(skin))));
}
for (Future<?> future : futures) {
try {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ru/leymooo/simpleskins/command/SkinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.kyori.text.serializer.ComponentSerializers;
import ru.leymooo.simpleskins.SimpleSkins;
import ru.leymooo.simpleskins.utils.SkinApplier;
import ru.leymooo.simpleskins.utils.SkinFetcher;
import ru.leymooo.simpleskins.utils.skinfetch.FetchResult;

/**
*
Expand Down Expand Up @@ -47,7 +46,7 @@ public void execute(CommandSource cs, String[] args) {
Optional<UUID> uuid = plugin.getDataBaseUtils().getUuid(player.getUsername());
if (uuid.isPresent()) {
cs.sendMessage(plugin.deserialize("messages.fetching"));
Optional<SkinFetcher.FetchResult> newSkin = plugin.getSkinFetcher().fetchSkin(player, uuid.get());
Optional<FetchResult> newSkin = plugin.getSkinFetcher().fetchSkin(player, uuid.get());
newSkin.ifPresent(skin -> {
cs.sendMessage(plugin.deserialize("messages.skin-changed"));
SkinApplier.applySkin(player, skin.getProperty());
Expand All @@ -61,7 +60,7 @@ public void execute(CommandSource cs, String[] args) {
}
plugin.getExecutorService().execute(() -> {
cs.sendMessage(plugin.deserialize("messages.fetching"));
Optional<SkinFetcher.FetchResult> newSkin = plugin.getSkinFetcher().fetchSkin(player, args[0].equalsIgnoreCase("reset") ? player.getUsername() : args[0]);
Optional<FetchResult> newSkin = plugin.getSkinFetcher().fetchSkin(player, args[0].equalsIgnoreCase("reset") ? player.getUsername() : args[0]);
newSkin.ifPresent(skin -> {
plugin.getDataBaseUtils().saveUser(player.getUsername(), skin);
SkinApplier.applySkin(player, skin.getProperty());
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ru/leymooo/simpleskins/utils/DataBaseUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.leymooo.simpleskins.utils;

import ru.leymooo.simpleskins.utils.skinfetch.SkinFetcher;
import com.velocitypowered.api.util.GameProfile;
import java.io.File;
import java.sql.Connection;
Expand All @@ -12,6 +13,8 @@
import java.util.UUID;

import ru.leymooo.simpleskins.SimpleSkins;
import ru.leymooo.simpleskins.utils.skinfetch.FetchResult;
import ru.leymooo.simpleskins.utils.skinfetch.SkinFetchResult;

public class DataBaseUtils {

Expand Down Expand Up @@ -47,12 +50,12 @@ private void connect() {
}
}

public Optional<SkinFetcher.FetchResult> getProperty(String name) {
public Optional<FetchResult> getProperty(String name) {
try (PreparedStatement ps = connection.prepareStatement(SELECT_SKIN_SQL)) {
ps.setString(1, name.toLowerCase());
ResultSet set = ps.executeQuery();
if (set.next()) {
return Optional.of(new SkinFetcher.FetchResult(set.getObject(1, UUID.class),
return Optional.of(new SkinFetchResult(set.getObject(1, UUID.class),
new GameProfile.Property("textures", set.getString(2), set.getString(3))));
}
} catch (SQLException ex) {
Expand All @@ -74,7 +77,7 @@ public Optional<UUID> getUuid(String name) {
return Optional.empty();
}

public void saveUser(String name, SkinFetcher.FetchResult result) {
public void saveUser(String name, FetchResult result) {
try (PreparedStatement ps = connection.prepareStatement(SELECT_NAME_SQL)) {
ps.setString(1, name.toLowerCase());
ResultSet set = ps.executeQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import ru.leymooo.simpleskins.SimpleSkins;
import ru.leymooo.simpleskins.utils.SkinFetcher.FetchResult;
import ru.leymooo.simpleskins.utils.skinfetch.FetchResult;

public class UuidFetchCache {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.leymooo.simpleskins.utils.skinfetch;

import com.velocitypowered.api.util.GameProfile.Property;
import java.util.UUID;

public interface FetchResult {

public UUID getId();

public Property getProperty();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.leymooo.simpleskins.utils.skinfetch;

import com.velocitypowered.api.util.GameProfile;
import java.util.UUID;

public class RateLimitedFetchResult implements FetchResult {

private final UUID id;

public RateLimitedFetchResult(UUID id) {
this.id = id;
}

@Override
public UUID getId() {
return id;
}

@Override
public GameProfile.Property getProperty() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.leymooo.simpleskins.utils.skinfetch;

import com.velocitypowered.api.util.GameProfile;
import java.util.Objects;
import java.util.UUID;

public class SkinFetchResult implements FetchResult {

private final UUID id;
private final GameProfile.Property property;

public SkinFetchResult(UUID id, GameProfile.Property property) {
this.id = id;
this.property = property;
}

public UUID getId() {
return id;
}

public GameProfile.Property getProperty() {
return property;
}

@Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + Objects.hashCode(this.id);
hash = 17 * hash + Objects.hashCode(this.property);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final FetchResult other = (FetchResult) obj;
if (!Objects.equals(this.id, other.getId())) {
return false;
}
if (!Objects.equals(this.property, other.getProperty())) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ru.leymooo.simpleskins.utils;
package ru.leymooo.simpleskins.utils.skinfetch;

import com.google.common.io.CharStreams;
import com.google.common.net.HttpHeaders;
Expand All @@ -40,21 +40,22 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import org.slf4j.Logger;
import ru.leymooo.simpleskins.SimpleSkins;
import ru.leymooo.simpleskins.utils.DataBaseUtils;
import ru.leymooo.simpleskins.utils.UuidFetchCache;

/**
*
* @author mikim
*/
public class SkinFetcher {

private static final String UUID_URL = "https://api.ashcon.app/mojang/v2/uuid/";
private static final String UUID_URL = "https://api.ashcon.app/mojang/v1/uuid/";
private static final String SKIN_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
private static final String ALTERNATIVE_SKIN_URL = "https://api.ashcon.app/mojang/v2/user/";
//private static final String ALTERNATIVE_SKIN_URL = "https://api.ashcon.app/mojang/v2/user/";

private static final JsonParser JSON_PARSER = new JsonParser();
private final SimpleSkins plugin;
Expand Down Expand Up @@ -136,10 +137,12 @@ private Optional<FetchResult> getSkin(UUID uuid) throws UserNotFoundExeption, IO
try {
Optional<FetchResult> result = uuidFetchCache.getIfCached(uuid);
if (result.isPresent()) {
checkFetchResult(result.get());
return result;
}
result = Optional.of(fetchSkin(uuid));
uuidFetchCache.cache(result.get());
checkFetchResult(result.get());
return result;
} finally {
uuidFetchCache.removeWorking(uuid);
Expand All @@ -161,14 +164,20 @@ private UUID fetchUUID(String username) throws IOException, UserNotFoundExeption
throw new UserNotFoundExeption(username);
}

private void checkFetchResult(FetchResult result) throws UserNotFoundExeption {
if (result instanceof RateLimitedFetchResult) {
throw new UserNotFoundExeption("Can not fetch skin due to rate-limit for " + result.getId());
}
}

private FetchResult fetchSkin(UUID uuid) throws IOException, UserNotFoundExeption, JsonSyntaxException {
HttpURLConnection connection = getConnection(SKIN_URL + UuidUtils.toUndashed(uuid) + "?unsigned=false");
int responseCode = connection.getResponseCode();

if (validate(responseCode)) {
JsonObject paresed = getJson(connection).getAsJsonObject();
JsonObject skin = paresed.getAsJsonArray("properties").get(0).getAsJsonObject();
return new FetchResult(uuid,
return new SkinFetchResult(uuid,
new GameProfile.Property("textures", skin.get("value").getAsString(), skin.get("signature").getAsString()));
} else if (responseCode == 429) { //Rate limited
return fetchSkinAlternative(uuid);
Expand All @@ -179,7 +188,7 @@ private FetchResult fetchSkin(UUID uuid) throws IOException, UserNotFoundExeptio
}

private FetchResult fetchSkinAlternative(UUID uuid) throws IOException, UserNotFoundExeption, JsonSyntaxException {
HttpURLConnection connection = getConnection(ALTERNATIVE_SKIN_URL + uuid.toString());
/*HttpURLConnection connection = getConnection(ALTERNATIVE_SKIN_URL + uuid.toString());
int responseCode = connection.getResponseCode();
if (validate(responseCode)) {
Expand All @@ -191,6 +200,8 @@ private FetchResult fetchSkinAlternative(UUID uuid) throws IOException, UserNotF
printErrorStream(connection, responseCode);
}
throw new UserNotFoundExeption(uuid.toString());
*/
return new RateLimitedFetchResult(uuid);
}

private JsonElement getJson(HttpURLConnection connection) throws IOException {
Expand Down Expand Up @@ -243,53 +254,4 @@ public UserNotFoundExeption(String userName) {
super(userName);
}
}

public static class FetchResult {

private final UUID id;
private final GameProfile.Property property;

public FetchResult(UUID id, GameProfile.Property property) {
this.id = id;
this.property = property;
}

public UUID getId() {
return id;
}

public GameProfile.Property getProperty() {
return property;
}

@Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + Objects.hashCode(this.id);
hash = 17 * hash + Objects.hashCode(this.property);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final FetchResult other = (FetchResult) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
if (!Objects.equals(this.property, other.property)) {
return false;
}
return true;
}

}
}

0 comments on commit b38fa09

Please sign in to comment.