accesscodes/src/lib/servertap.ts
anonymousratwastaken 02d686ccae fixed things
2025-12-20 07:58:05 +00:00

194 lines
5.1 KiB
TypeScript

import { cfg } from "./cfg";
type MinecraftPlayer = {
name: string;
};
type ResolvedPlayer = {
platform: "java" | "bedrock";
username: string;
uuid?: string;
xuid?: string;
};
type MinecraftResponse<T> = {
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<T>(
endpoint: string,
options: RequestInit = {}
): Promise<MinecraftResponse<T>> {
try {
const url = `${this.baseUrl}${endpoint}`;
const headers: Record<string, string> = {
"Content-Type": "application/json",
...(options.headers as Record<string, string> || {}),
};
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<MinecraftResponse<void>> {
// 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<MinecraftResponse<void>> {
// 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<MinecraftResponse<{ whitelist: string }>> {
return this.makeRequest("/api/whitelist/list", {
method: "GET",
});
}
async rewardPlayer(playerName: string): Promise<MinecraftResponse<void>> {
return this.makeRequest("/reward", {
method: "POST",
body: JSON.stringify({ player: playerName }),
});
}
async executeCommand(command: string): Promise<MinecraftResponse<{ response: string }>> {
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 };