add fallback skins

This commit is contained in:
Lee 2024-04-13 15:54:32 +01:00
parent 852f5a8bea
commit 3cf16bd2eb
4 changed files with 14 additions and 7 deletions

@ -32,19 +32,19 @@ public class WebRequest {
* @param <T> the type of the response * @param <T> the type of the response
*/ */
public static <T> T getAsEntity(String url, Class<T> clazz) throws RateLimitException { public static <T> T getAsEntity(String url, Class<T> clazz) throws RateLimitException {
ResponseEntity<T> profile = CLIENT.get() ResponseEntity<T> responseEntity = CLIENT.get()
.uri(url) .uri(url)
.retrieve() .retrieve()
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error .onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
.toEntity(clazz); .toEntity(clazz);
if (profile.getStatusCode().isError()) { if (responseEntity.getStatusCode().isError()) {
return null; return null;
} }
if (profile.getStatusCode().isSameCodeAs(HttpStatus.TOO_MANY_REQUESTS)) { if (responseEntity.getStatusCode().isSameCodeAs(HttpStatus.TOO_MANY_REQUESTS)) {
throw new RateLimitException("Rate limit reached"); throw new RateLimitException("Rate limit reached");
} }
return profile.getBody(); return responseEntity.getBody();
} }
/** /**

@ -21,10 +21,14 @@ import java.util.Map;
@Getter @Log4j2 @Getter @Log4j2
public class Skin { public class Skin {
/** /**
* The default skin, usually used when the skin is not found. * The default skins, usually used when the skin is not found.
*/ */
public static final Skin DEFAULT_SKIN = new Skin("http://textures.minecraft.net/texture/60a5bd016b3c9a1b9272e4929e30827a67be4ebb219017adbbc4a4d22ebd5b1", public static final Map<Model, Skin> DEFAULT_SKINS = new HashMap<>();
Model.DEFAULT);
static {
DEFAULT_SKINS.put(Model.DEFAULT, new Skin(Config.INSTANCE.getWebPublicUrl() + "/assets/steve.png", Model.DEFAULT));
DEFAULT_SKINS.put(Model.SLIM, new Skin(Config.INSTANCE.getWebPublicUrl() + "/assets/alex.png", Model.SLIM));
}
/** /**
* The URL for the skin * The URL for the skin
@ -58,6 +62,9 @@ public class Skin {
this.model = model; this.model = model;
this.skinImage = PlayerUtils.getSkinImage(url); this.skinImage = PlayerUtils.getSkinImage(url);
if (skinImage == null) { // Use the default skin if the skin is not found
this.skinImage = PlayerUtils.getSkinImage(DEFAULT_SKINS.get(model).getUrl());
}
if (this.skinImage != null) { if (this.skinImage != null) {
try { try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(this.skinImage)); BufferedImage image = ImageIO.read(new ByteArrayInputStream(this.skinImage));

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB