From ecd328aa485072a245a0635d0d4e85c0dadeb509 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 20 Apr 2024 21:12:35 +0100 Subject: [PATCH] add server previews --- pom.xml | 2 +- src/main/java/xyz/mcutils/McUtilsAPI.java | 15 +++++++- .../CachedPlayerSkinPart.java | 2 +- .../models/cache/CachedServerPreview.java | 13 +++++++ src/test/java/xyz/mcutils/PlayerTests.java | 2 +- src/test/java/xyz/mcutils/ServerTests.java | 36 +++++++++++++++++++ 6 files changed, 66 insertions(+), 4 deletions(-) rename src/main/java/xyz/mcutils/models/{player => cache}/CachedPlayerSkinPart.java (87%) create mode 100644 src/main/java/xyz/mcutils/models/cache/CachedServerPreview.java diff --git a/pom.xml b/pom.xml index cce5fd6..e0ef36e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ xyz.mcutils mcutils-java-library - 1.1.1 + 1.2.0 17 diff --git a/src/main/java/xyz/mcutils/McUtilsAPI.java b/src/main/java/xyz/mcutils/McUtilsAPI.java index 380d4ea..0ed6786 100644 --- a/src/main/java/xyz/mcutils/McUtilsAPI.java +++ b/src/main/java/xyz/mcutils/McUtilsAPI.java @@ -3,7 +3,7 @@ package xyz.mcutils; import xyz.mcutils.common.WebRequest; import xyz.mcutils.exception.ErrorResponse; 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.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 SERVER_ENDPOINT = API_ENDPOINT + "/server/%s/%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"; /** @@ -107,4 +108,16 @@ public class McUtilsAPI { public static CachedServerBlockedStatus getServerBlockedStatus(String id) throws ErrorResponse { 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); + } } \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/models/player/CachedPlayerSkinPart.java b/src/main/java/xyz/mcutils/models/cache/CachedPlayerSkinPart.java similarity index 87% rename from src/main/java/xyz/mcutils/models/player/CachedPlayerSkinPart.java rename to src/main/java/xyz/mcutils/models/cache/CachedPlayerSkinPart.java index 098812a..5e29e96 100644 --- a/src/main/java/xyz/mcutils/models/player/CachedPlayerSkinPart.java +++ b/src/main/java/xyz/mcutils/models/cache/CachedPlayerSkinPart.java @@ -1,4 +1,4 @@ -package xyz.mcutils.models.player; +package xyz.mcutils.models.cache; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/xyz/mcutils/models/cache/CachedServerPreview.java b/src/main/java/xyz/mcutils/models/cache/CachedServerPreview.java new file mode 100644 index 0000000..4c9b85f --- /dev/null +++ b/src/main/java/xyz/mcutils/models/cache/CachedServerPreview.java @@ -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; +} diff --git a/src/test/java/xyz/mcutils/PlayerTests.java b/src/test/java/xyz/mcutils/PlayerTests.java index 501e708..ac6043a 100644 --- a/src/test/java/xyz/mcutils/PlayerTests.java +++ b/src/test/java/xyz/mcutils/PlayerTests.java @@ -4,7 +4,7 @@ import lombok.SneakyThrows; import org.junit.jupiter.api.Test; import xyz.mcutils.exception.ErrorResponse; 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.player.Skin; diff --git a/src/test/java/xyz/mcutils/ServerTests.java b/src/test/java/xyz/mcutils/ServerTests.java index 973c1a2..a2529df 100644 --- a/src/test/java/xyz/mcutils/ServerTests.java +++ b/src/test/java/xyz/mcutils/ServerTests.java @@ -6,7 +6,9 @@ import xyz.mcutils.exception.ErrorResponse; import xyz.mcutils.models.cache.CachedBedrockMinecraftServer; import xyz.mcutils.models.cache.CachedJavaMinecraftServer; import xyz.mcutils.models.cache.CachedServerBlockedStatus; +import xyz.mcutils.models.cache.CachedServerPreview; import xyz.mcutils.models.server.CachedServerIcon; +import xyz.mcutils.models.server.ServerPlatform; public class ServerTests { @@ -71,4 +73,38 @@ public class ServerTests { 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; + } + } }