add server previews

This commit is contained in:
Lee 2024-04-20 21:12:35 +01:00
parent 39cb7e3d81
commit ecd328aa48
6 changed files with 66 additions and 4 deletions

@ -6,7 +6,7 @@
<groupId>xyz.mcutils</groupId> <groupId>xyz.mcutils</groupId>
<artifactId>mcutils-java-library</artifactId> <artifactId>mcutils-java-library</artifactId>
<version>1.1.1</version> <version>1.2.0</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

@ -3,7 +3,7 @@ package xyz.mcutils;
import xyz.mcutils.common.WebRequest; import xyz.mcutils.common.WebRequest;
import xyz.mcutils.exception.ErrorResponse; import xyz.mcutils.exception.ErrorResponse;
import xyz.mcutils.models.cache.*; import xyz.mcutils.models.cache.*;
import xyz.mcutils.models.player.CachedPlayerSkinPart; import xyz.mcutils.models.cache.CachedPlayerSkinPart;
import xyz.mcutils.models.player.Skin; import xyz.mcutils.models.player.Skin;
import xyz.mcutils.models.server.*; import xyz.mcutils.models.server.*;
@ -15,6 +15,7 @@ public class McUtilsAPI {
private static final String PLAYER_SKIN_PART_ENDPOINT = API_ENDPOINT + "/player/%s/%s"; private static final String PLAYER_SKIN_PART_ENDPOINT = API_ENDPOINT + "/player/%s/%s";
private static final String SERVER_ENDPOINT = API_ENDPOINT + "/server/%s/%s"; private static final String SERVER_ENDPOINT = API_ENDPOINT + "/server/%s/%s";
private static final String SERVER_ICON_ENDPOINT = API_ENDPOINT + "/server/icon/%s"; private static final String SERVER_ICON_ENDPOINT = API_ENDPOINT + "/server/icon/%s";
private static final String SERVER_PREVIEW_ENDPOINT = API_ENDPOINT + "/server/%s/preview/%s";
private static final String SERVER_BLOCKED_STATUS_ENDPOINT = API_ENDPOINT + "/server/blocked/%s"; private static final String SERVER_BLOCKED_STATUS_ENDPOINT = API_ENDPOINT + "/server/blocked/%s";
/** /**
@ -107,4 +108,16 @@ public class McUtilsAPI {
public static CachedServerBlockedStatus getServerBlockedStatus(String id) throws ErrorResponse { public static CachedServerBlockedStatus getServerBlockedStatus(String id) throws ErrorResponse {
return WebRequest.get(SERVER_BLOCKED_STATUS_ENDPOINT.formatted(id), CachedServerBlockedStatus.class); return WebRequest.get(SERVER_BLOCKED_STATUS_ENDPOINT.formatted(id), CachedServerBlockedStatus.class);
} }
/**
* Gets the icon of a Java server from the API.
*
* @param id The id of the server to get the icon of.
* @return The server icon.
* @throws ErrorResponse If an error occurs.
*/
public static CachedServerPreview getServerPreview(ServerPlatform platform, String id) throws ErrorResponse {
byte[] bytes = WebRequest.get(SERVER_PREVIEW_ENDPOINT.formatted(platform.name(), id), byte[].class);
return new CachedServerPreview(bytes);
}
} }

@ -1,4 +1,4 @@
package xyz.mcutils.models.player; package xyz.mcutils.models.cache;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

@ -0,0 +1,13 @@
package xyz.mcutils.models.cache;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@AllArgsConstructor @Getter @ToString
public class CachedServerPreview {
/**
* The bytes for the skin part
*/
private byte[] bytes;
}

@ -4,7 +4,7 @@ import lombok.SneakyThrows;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.mcutils.exception.ErrorResponse; import xyz.mcutils.exception.ErrorResponse;
import xyz.mcutils.models.cache.CachedPlayer; import xyz.mcutils.models.cache.CachedPlayer;
import xyz.mcutils.models.player.CachedPlayerSkinPart; import xyz.mcutils.models.cache.CachedPlayerSkinPart;
import xyz.mcutils.models.cache.CachedUsernameToUuid; import xyz.mcutils.models.cache.CachedUsernameToUuid;
import xyz.mcutils.models.player.Skin; import xyz.mcutils.models.player.Skin;

@ -6,7 +6,9 @@ import xyz.mcutils.exception.ErrorResponse;
import xyz.mcutils.models.cache.CachedBedrockMinecraftServer; import xyz.mcutils.models.cache.CachedBedrockMinecraftServer;
import xyz.mcutils.models.cache.CachedJavaMinecraftServer; import xyz.mcutils.models.cache.CachedJavaMinecraftServer;
import xyz.mcutils.models.cache.CachedServerBlockedStatus; import xyz.mcutils.models.cache.CachedServerBlockedStatus;
import xyz.mcutils.models.cache.CachedServerPreview;
import xyz.mcutils.models.server.CachedServerIcon; import xyz.mcutils.models.server.CachedServerIcon;
import xyz.mcutils.models.server.ServerPlatform;
public class ServerTests { public class ServerTests {
@ -71,4 +73,38 @@ public class ServerTests {
assert ex.getCode() == 400; assert ex.getCode() == 400;
} }
} }
@Test
@SneakyThrows
public void ensureBedrockServerIconLookupSuccess() {
CachedServerIcon icon = McUtilsAPI.getServerIcon(testBedrockServer);
assert icon.getBytes() != null;
}
@Test
@SneakyThrows
public void ensureBedrockServerIconLookupFailure() {
try {
McUtilsAPI.getServerIcon(testInvalidServer);
} catch (ErrorResponse ex) {
assert ex.getCode() == 400;
}
}
@Test
@SneakyThrows
public void ensureServerPreviewLookupSuccess() {
CachedServerPreview icon = McUtilsAPI.getServerPreview(ServerPlatform.JAVA, testJavaServer);
assert icon.getBytes() != null;
}
@Test
@SneakyThrows
public void ensureServerPreviewLookupFailure() {
try {
McUtilsAPI.getServerPreview(ServerPlatform.JAVA, testInvalidServer);
} catch (ErrorResponse ex) {
assert ex.getCode() == 400;
}
}
} }