feat: fetch skin in separate thread
This commit is contained in:
parent
d784ed448f
commit
59addc6ee3
2 changed files with 41 additions and 34 deletions
|
@ -128,6 +128,8 @@ public class AppearanceManager {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
reset();
|
reset();
|
||||||
return ActionResult.error(I18NDict.Error.MOJANG_NAME);
|
return ActionResult.error(I18NDict.Error.MOJANG_NAME);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return ActionResult.error(I18NDict.Error.GENERIC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ActionResult.ok();
|
return ActionResult.ok();
|
||||||
|
|
|
@ -17,6 +17,8 @@ import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -26,6 +28,7 @@ public class MojangAPI {
|
||||||
|
|
||||||
private final Logger logger = Logger.getLogger("MojangAPI");
|
private final Logger logger = Logger.getLogger("MojangAPI");
|
||||||
private final HashMap<String, String> uuidToName = new HashMap<>();
|
private final HashMap<String, String> uuidToName = new HashMap<>();
|
||||||
|
private final ExecutorService worker = Executors.newFixedThreadPool(6);
|
||||||
|
|
||||||
private final CacheLoader<String, Optional<MojangSkin>> skinLoader = new CacheLoader<>() {
|
private final CacheLoader<String, Optional<MojangSkin>> skinLoader = new CacheLoader<>() {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -56,7 +59,7 @@ public class MojangAPI {
|
||||||
return skinCache.get(uuid);
|
return skinCache.get(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<MojangSkin> getSkinWithoutCaching(String uuid) throws IOException {
|
public Optional<MojangSkin> getSkinWithoutCaching(String uuid) throws IOException, ExecutionException, InterruptedException {
|
||||||
return getSkinFromMojang(uuid);
|
return getSkinFromMojang(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +67,7 @@ public class MojangAPI {
|
||||||
return uuidCache.get(name);
|
return uuidCache.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<String> getUUIDFromMojang(String name) throws IOException {
|
private Optional<String> getUUIDFromMojang(String name) throws ExecutionException, InterruptedException {
|
||||||
final String parametrizedUrl = URL_NAME.replace("{name}", name);
|
final String parametrizedUrl = URL_NAME.replace("{name}", name);
|
||||||
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
||||||
if (hasNoError(object)) {
|
if (hasNoError(object)) {
|
||||||
|
@ -84,7 +87,7 @@ public class MojangAPI {
|
||||||
uuidCache.invalidate(uuid);
|
uuidCache.invalidate(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<MojangSkin> getSkinFromMojang(String uuid) throws IOException {
|
private Optional<MojangSkin> getSkinFromMojang(String uuid) throws ExecutionException, InterruptedException {
|
||||||
final String parametrizedUrl = URL_SKIN.replace("{uuid}", uuid);
|
final String parametrizedUrl = URL_SKIN.replace("{uuid}", uuid);
|
||||||
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
||||||
if (hasNoError(object)) {
|
if (hasNoError(object)) {
|
||||||
|
@ -98,7 +101,8 @@ public class MojangAPI {
|
||||||
return uuidToName.get(uuid);
|
return uuidToName.get(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonObject getRequestToUrl(String parametrizedUrl) throws IOException {
|
private JsonObject getRequestToUrl(String parametrizedUrl) throws ExecutionException, InterruptedException {
|
||||||
|
return worker.submit(() -> {
|
||||||
final URL url = new URL(parametrizedUrl);
|
final URL url = new URL(parametrizedUrl);
|
||||||
final HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
final HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||||
con.setDoInput(true);
|
con.setDoInput(true);
|
||||||
|
@ -131,6 +135,7 @@ public class MojangAPI {
|
||||||
logger.warning("Unhandled response code from Mojang: " + con.getResponseCode());
|
logger.warning("Unhandled response code from Mojang: " + con.getResponseCode());
|
||||||
return getErrorObject();
|
return getErrorObject();
|
||||||
}
|
}
|
||||||
|
}).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonObject getErrorObject() {
|
private JsonObject getErrorObject() {
|
||||||
|
|
Loading…
Reference in a new issue