From 321db2fb3d3e85187682fa10950ee9ebad26902e Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 20 Apr 2024 22:49:53 +0100 Subject: [PATCH] add server previews --- package.json | 2 +- src/lib/server.ts | 14 ++++++++++++++ test/player.ts | 1 + test/server.ts | 10 +++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5fc1783..22d07d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mcutils-library", - "version": "1.2.6", + "version": "1.3.0", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/lib/server.ts b/src/lib/server.ts index b9eadff..da060f0 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -5,6 +5,7 @@ import {ServerPlatform} from "../types/server/platform"; const serverEndpoint = API_ENDPOINT + "/server/{platform}/{hostname}"; const serverIconEndpoint = API_ENDPOINT + "/server/icon/{hostname}"; +const serverPreviewEndpoint = API_ENDPOINT + "/server/{platform}/preview/{hostname}"; const blockedServerEndpoint = API_ENDPOINT + "/server/blocked/{hostname}"; /** @@ -38,6 +39,19 @@ export async function getServerIcon(hostname: string, port?: 25565): Promise(serverIconEndpoint.replace("{hostname}", ip)); } +/** + * Gets the preview of a Minecraft server's MOTD. + * + * @param platform the platform of the server + * @param hostname the hostname of the server + * @param port the port of the server + * @returns the server icon, or null if the server does not have an icon + */ +export async function getServerPreview(platform: ServerPlatform, hostname: string, port?: 25565): Promise { + const ip = port ? `${hostname}:${port}` : hostname; + return WebRequest.get(serverPreviewEndpoint.replace("{platform}", platform).replace("{hostname}", ip)); +} + /** * Gets the Mojang blocked status of a Minecraft server. * diff --git a/test/player.ts b/test/player.ts index c57bea5..77e62ba 100644 --- a/test/player.ts +++ b/test/player.ts @@ -24,6 +24,7 @@ test("ensureGetPlayerSkinPartSuccess", async () => { // Test each skin part for (const part in skinParts) { const partBuffer = await getPlayerSkinPart(part, player.uniqueId); + expect(partBuffer).toBeDefined(); expect(partBuffer.byteLength).toBeGreaterThan(0); } diff --git a/test/server.ts b/test/server.ts index ffc741f..6ff0669 100644 --- a/test/server.ts +++ b/test/server.ts @@ -1,4 +1,4 @@ -import {getBlockedStatus, getServer, getServerIcon, ServerPlatform} from "../dist"; +import {getBlockedStatus, getServer, getServerIcon, getServerPreview, ServerPlatform} from "../dist"; test("ensureGetServerLookupSuccess", async () => { const server = await getServer(ServerPlatform.Java, "mc.hypixel.net"); @@ -9,6 +9,7 @@ test("ensureGetServerLookupSuccess", async () => { test("ensureGetServerIconSuccess", async () => { const icon = await getServerIcon("mc.hypixel.net"); + expect(icon).toBeDefined(); expect(icon.byteLength).toBeGreaterThan(0); // The server has an icon }); @@ -19,3 +20,10 @@ test("ensureGetServerBlockedStatusSuccess", async () => { expect(blocked).toBe(false); // The server is not blocked }); + +test("ensureGetServerPreviewSuccess", async () => { + const preview = await getServerPreview(ServerPlatform.Java, "mc.hypixel.net"); + + expect(preview).toBeDefined(); + expect(preview.byteLength).toBeGreaterThan(0); // The server has a preview +}); \ No newline at end of file