import { Card } from "@/app/components/card"; import { ErrorCard } from "@/app/components/error-card"; import { LookupServer } from "@/app/components/server/lookup-server"; import { generateEmbed } from "@/common/embed"; import { formatNumber } from "@/common/number-utils"; import { capitalizeFirstLetter } from "@/common/string-utils"; import { CachedBedrockMinecraftServer, CachedJavaMinecraftServer, McUtilsAPIError, ServerPlatform, getServer, } from "mcutils-library"; import { Metadata } from "next"; import Image from "next/image"; type Params = { params: { platform: ServerPlatform; hostname: string; }; }; /** * Gets the favicon for a server * * @param platform the platform of the server * @param server the server to get the favicon from * @returns the favicon url or null if there is no favicon */ function getFavicon( platform: ServerPlatform, server: CachedJavaMinecraftServer | CachedBedrockMinecraftServer ): string | undefined { if (platform === ServerPlatform.Bedrock) { return undefined; } server = server as CachedJavaMinecraftServer; return server.favicon && server.favicon.url; } /** * Checks if a platform is valid * * @param platform the platform to check * @returns true if the platform is valid, false otherwise */ function checkPlatform(platform: ServerPlatform): boolean { return platform === ServerPlatform.Java || platform === ServerPlatform.Bedrock; } export async function generateMetadata({ params: { platform, hostname } }: Params): Promise { try { if (checkPlatform(platform) === false) { return generateEmbed({ title: "Server Not Found", description: "Invalid platform", }); } const server = await getServer(platform, hostname); const { hostname: serverHostname, players } = server as CachedJavaMinecraftServer | CachedBedrockMinecraftServer; const favicon = server ? getFavicon(platform, server) : undefined; const description = ` ${capitalizeFirstLetter(platform)} Server Hostname: ${serverHostname} ${players.online}/${players.max} players online`; return generateEmbed({ title: `${serverHostname}`, description: description, image: favicon, }); } catch (err) { return generateEmbed({ title: "Server Not Found", description: (err as McUtilsAPIError).message, }); } } export default async function Page({ params: { platform, hostname } }: Params): Promise { let error: string | undefined = undefined; // The error to display let server: CachedJavaMinecraftServer | CachedBedrockMinecraftServer | undefined = undefined; // The server to display let invalidPlatform = checkPlatform(platform) === false; // Try and get the player to display try { console.log(platform); if (invalidPlatform) { error = "Invalid platform"; // Set the error message } else { server = platform && hostname ? await getServer(platform, hostname) : undefined; } } catch (err) { error = (err as McUtilsAPIError).message; // Set the error message } const favicon = server ? getFavicon(platform, server) : undefined; return (

Lookup a {invalidPlatform ? "" : capitalizeFirstLetter(platform)} Server

You can enter a server hostname to get information about the server.

{error && } {server != null && (
{favicon && (
The server's favicon
)}

{server.hostname}

Players online: {formatNumber(server.players.online)}/{formatNumber(server.players.max)}

{server.motd.html.map((line, index) => { return

; })}
)}
); }