import { cfg } from "./cfg"; type MinecraftPlayer = { name: string; }; type ResolvedPlayer = { platform: "java" | "bedrock"; username: string; uuid?: string; xuid?: string; }; type MinecraftResponse = { success: boolean; data?: T; error?: string; message?: string; details?: string; }; class MinecraftClient { private baseUrl: string; private apiKey: string; constructor() { this.baseUrl = cfg.minecraftApi.url || "http://localhost:6767"; this.apiKey = cfg.minecraftApi.key || ""; } private async makeRequest( endpoint: string, options: RequestInit = {} ): Promise> { try { const url = `${this.baseUrl}${endpoint}`; const headers: Record = { "Content-Type": "application/json", ...(options.headers as Record || {}), }; if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } console.log(`Minecraft API Request: ${options.method || 'GET'} ${url}`); const response = await fetch(url, { headers, ...options, }); console.log(`Minecraft API Response: ${response.status} ${response.statusText}`); if (!response.ok) { const errorData = await response.json().catch(() => ({})); console.error(`Minecraft API Error Response:`, errorData); return { success: false, error: errorData.error || `HTTP ${response.status}: ${response.statusText}`, details: errorData.details, }; } const data = await response.json(); return { success: true, data, message: data.message, }; } catch (error) { console.error(`Minecraft API Error:`, error); console.error(`Base URL: ${this.baseUrl}`); console.error(`API Key configured: ${!!this.apiKey}`); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } } async addToWhitelist(player: string | ResolvedPlayer): Promise> { // Handle legacy string input for backward compatibility if (typeof player === 'string') { if (!player || player.trim() === '') { return { success: false, error: 'Player name is required' }; } return this.makeRequest("/api/whitelist/add", { method: "POST", body: JSON.stringify({ player: player.trim() }), }); } // Validate resolved player data if (!player || !player.username || !player.platform) { return { success: false, error: 'Player data is incomplete: username and platform are required' }; } // Validate platform-specific identifiers if (player.platform === 'bedrock' && !player.xuid) { return { success: false, error: 'Bedrock gamertag players require an XUID' }; } if (player.platform === 'java' && !player.uuid) { return { success: false, error: 'Java username players require a UUID' }; } // Send resolved player data with UUID/XUID const payload: any = { player: player.username.trim(), platform: player.platform, // Send 'java' and 'bedrock' as platform names }; if (player.uuid) { payload.uuid = player.uuid; } if (player.xuid) { payload.xuid = player.xuid; } return this.makeRequest("/api/whitelist/add", { method: "POST", body: JSON.stringify(payload), }); } async removeFromWhitelist(player: string | ResolvedPlayer): Promise> { // Handle legacy string input for backward compatibility if (typeof player === 'string') { return this.makeRequest("/api/whitelist/remove", { method: "POST", body: JSON.stringify({ player }), }); } // Send resolved player data with UUID/XUID const payload: any = { player: player.username.trim(), platform: player.platform, // Send 'java' and 'bedrock' as platform names }; if (player.uuid) { payload.uuid = player.uuid; } if (player.xuid) { payload.xuid = player.xuid; } return this.makeRequest("/api/whitelist/remove", { method: "POST", body: JSON.stringify(payload), }); } async getWhitelist(): Promise> { return this.makeRequest("/api/whitelist/list", { method: "GET", }); } async rewardPlayer(playerName: string): Promise> { return this.makeRequest("/reward", { method: "POST", body: JSON.stringify({ player: playerName }), }); } async executeCommand(command: string): Promise> { return this.makeRequest("/api/command", { method: "POST", body: JSON.stringify({ command }), }); } } export const minecraft = new MinecraftClient(); // Legacy exports for backward compatibility export const servertap = minecraft; export type { MinecraftPlayer as ServerTapPlayer, MinecraftResponse as ServerTapResponse, ResolvedPlayer };