leejunyoung5819mc-paper-plugin-test-mcp-26.3

공개

마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구

76줄3.3 KB
1// Player identities for launched clients.
2// - offline: any name, UUID derived like the server does for offline players. Only works with online-mode=false.
3// - microsoft: your real Minecraft account via Microsoft device-code login (prismarine-auth).
4// Tokens are cached under .mct/auth (mode 700). The harness never prints them.
5import crypto from 'node:crypto';
6import fs from 'node:fs';
7import path from 'node:path';
8import { DIRS } from './config.ts';
9
10export interface Identity {
11 name: string;
12 uuid: string;
13 accessToken: string;
14 userType: 'msa' | 'legacy';
15 xuid: string;
16 online: boolean;
17}
18
19export function offlineIdentity(name: string): Identity {
20 if (!/^[A-Za-z0-9_]{3,16}$/.test(name)) throw new Error(`invalid player name '${name}' (3-16 chars, A-Z a-z 0-9 _)`);
21 // Same as Java UUID.nameUUIDFromBytes("OfflinePlayer:" + name): MD5, version 3, IETF variant.
22 const h = crypto.createHash('md5').update('OfflinePlayer:' + name, 'utf8').digest();
23 h[6] = (h[6] & 0x0f) | 0x30;
24 h[8] = (h[8] & 0x3f) | 0x80;
25 const hex = h.toString('hex');
26 const uuid = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
27 return { name, uuid, accessToken: '0', userType: 'legacy', xuid: '0', online: false };
28}
29
30export interface LoginPrompt {
31 verificationUri: string;
32 userCode: string;
33 message: string;
34}
35
36/**
37 * Microsoft login. On first use this needs a human: `onCode` receives the device-code prompt
38 * (open the URL, enter the code). Later calls reuse/refresh the cached tokens silently.
39 */
40export async function microsoftIdentity(profileKey = 'default', onCode?: (p: LoginPrompt) => void, interactive = true): Promise<Identity> {
41 const { Authflow, Titles } = await import('prismarine-auth');
42 const cache = path.join(DIRS.auth, profileKey);
43 fs.mkdirSync(cache, { recursive: true, mode: 0o700 });
44 let needLogin: (e: Error) => void = () => {};
45 const loginRequired = new Promise<never>((_, reject) => (needLogin = reject));
46 const flow = new Authflow(
47 profileKey,
48 cache,
49 { flow: 'live', authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo' },
50 (res: any) => {
51 const p: LoginPrompt = {
52 verificationUri: res.verification_uri ?? res.verificationUri,
53 userCode: res.user_code ?? res.userCode,
54 message: res.message,
55 };
56 if (!interactive) {
57 needLogin(new Error(`Microsoft login required for profile '${profileKey}'. A human must run: npm run mct -- login ${profileKey}`));
58 return;
59 }
60 if (onCode) onCode(p);
61 else console.error(p.message);
62 },
63 );
64 const tok: any = await Promise.race([flow.getMinecraftJavaToken({ fetchProfile: true }), loginRequired]);
65 if (!tok?.profile?.id) {
66 throw new Error('Microsoft account has no Minecraft: Java Edition profile (does this account own Java Edition?)');
67 }
68 const raw = String(tok.profile.id).replace(/-/g, '');
69 const uuid = `${raw.slice(0, 8)}-${raw.slice(8, 12)}-${raw.slice(12, 16)}-${raw.slice(16, 20)}-${raw.slice(20)}`;
70 return { name: tok.profile.name, uuid, accessToken: tok.token, userType: 'msa', xuid: '0', online: true };
71}
72
73export function hasMicrosoftCache(profileKey = 'default'): boolean {
74 const dir = path.join(DIRS.auth, profileKey);
75 return fs.existsSync(dir) && fs.readdirSync(dir).length > 0;
76}