leejunyoung5819 mc-paper-plugin-test-mcp-26.3
공개마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구
| 1 | // Loads mct.config.json (optional) and resolves every runtime path under .mct/. |
| 2 | import fs from 'node:fs'; |
| 3 | import path from 'node:path'; |
| 4 | import crypto from 'node:crypto'; |
| 5 | |
| 6 | export const ROOT = path.resolve(import.meta.dirname, '..'); |
| 7 | |
| 8 | export interface Config { |
| 9 | mcVersion: string; |
| 10 | fabricLoader: string; |
| 11 | fabricApi: string; |
| 12 | daemonPort: number; |
| 13 | server: { |
| 14 | paperBuild: 'latest' | number; |
| 15 | port: number; |
| 16 | rconPort: number; |
| 17 | memory: string; |
| 18 | /** false = offline mode on 127.0.0.1 (many test players). true = real auth (Microsoft account clients only). */ |
| 19 | onlineMode: boolean; |
| 20 | /** Plugin jars to copy into plugins/ on every start. Globs relative to the repo root (only * in the file name). */ |
| 21 | plugins: string[]; |
| 22 | /** Extra server.properties overrides. */ |
| 23 | properties: Record<string, string | number | boolean>; |
| 24 | /** Commands run over RCON right after every start (gamerules etc.). */ |
| 25 | onStart: string[]; |
| 26 | /** What a world snapshot captures, relative to the server dir. `dir/*` = every subdirectory. */ |
| 27 | snapshotInclude: string[]; |
| 28 | }; |
| 29 | client: { |
| 30 | memory: string; |
| 31 | /** "xvfb" = invisible virtual display per client, "host" = open a window on your desktop. */ |
| 32 | display: 'xvfb' | 'host'; |
| 33 | width: number; |
| 34 | height: number; |
| 35 | guiScale: number; |
| 36 | /** Auto-accept server resource packs (no confirmation screen blocking automated joins). */ |
| 37 | acceptServerResourcePacks: boolean; |
| 38 | /** Suppress toast popups (recipe unlocked, chat warnings) so they don't cover screenshots. */ |
| 39 | hideToasts: boolean; |
| 40 | /** Client UI language, e.g. ko_kr / en_us. */ |
| 41 | lang: string; |
| 42 | bridgeBasePort: number; |
| 43 | /** Extra mod jars (globs relative to repo root) to load in every client, e.g. a client-side resource helper. */ |
| 44 | extraMods: string[]; |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | const DEFAULTS: Config = { |
| 49 | mcVersion: '26.3', |
| 50 | fabricLoader: '0.20.1', |
| 51 | fabricApi: '0.166.2+26.3', |
| 52 | daemonPort: 7770, |
| 53 | server: { |
| 54 | paperBuild: 'latest', |
| 55 | port: 25565, |
| 56 | rconPort: 25575, |
| 57 | memory: '4G', |
| 58 | onlineMode: false, |
| 59 | plugins: ['fixtures/demo-shop/build/libs/demo-shop-*.jar'], |
| 60 | properties: {}, |
| 61 | onStart: [ |
| 62 | 'gamerule advance_time false', |
| 63 | 'gamerule advance_weather false', |
| 64 | 'gamerule spawn_mobs false', |
| 65 | 'time set day', |
| 66 | 'weather clear', |
| 67 | ], |
| 68 | snapshotInclude: ['world', 'world_nether', 'world_the_end', 'plugins/*'], |
| 69 | }, |
| 70 | client: { |
| 71 | memory: '3G', |
| 72 | display: 'xvfb', |
| 73 | width: 1280, |
| 74 | height: 720, |
| 75 | guiScale: 2, |
| 76 | lang: 'ko_kr', |
| 77 | hideToasts: true, |
| 78 | acceptServerResourcePacks: true, |
| 79 | bridgeBasePort: 7800, |
| 80 | extraMods: [], |
| 81 | }, |
| 82 | }; |
| 83 | |
| 84 | function merge<T>(base: T, over: unknown): T { |
| 85 | if (over === undefined || over === null) return base; |
| 86 | if (typeof base !== 'object' || Array.isArray(base) || base === null) return over as T; |
| 87 | const out: Record<string, unknown> = { ...(base as Record<string, unknown>) }; |
| 88 | for (const [k, v] of Object.entries(over as Record<string, unknown>)) { |
| 89 | out[k] = merge((base as Record<string, unknown>)[k], v); |
| 90 | } |
| 91 | return out as T; |
| 92 | } |
| 93 | |
| 94 | export function configPath(): string { |
| 95 | return process.env.MCT_CONFIG ?? path.join(ROOT, 'mct.config.json'); |
| 96 | } |
| 97 | |
| 98 | /** Read mct.config.json as the user wrote it (only their overrides, no defaults). */ |
| 99 | export function readUserConfig(): Record<string, any> { |
| 100 | const file = configPath(); |
| 101 | return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : {}; |
| 102 | } |
| 103 | |
| 104 | /** Apply a change to mct.config.json, keeping everything else the user wrote. */ |
| 105 | export function updateUserConfig(change: (c: Record<string, any>) => void): string { |
| 106 | const file = configPath(); |
| 107 | const c = readUserConfig(); |
| 108 | change(c); |
| 109 | const tmp = file + '.tmp'; |
| 110 | fs.writeFileSync(tmp, JSON.stringify(c, null, 2) + '\n'); |
| 111 | fs.renameSync(tmp, file); |
| 112 | return file; |
| 113 | } |
| 114 | |
| 115 | export function loadConfig(): Config { |
| 116 | const file = configPath(); |
| 117 | let user: unknown = {}; |
| 118 | if (fs.existsSync(file)) user = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 119 | const cfg = merge(DEFAULTS, user); |
| 120 | if (process.env.MCT_DISPLAY === 'host' || process.env.MCT_DISPLAY === 'xvfb') cfg.client.display = process.env.MCT_DISPLAY; |
| 121 | return cfg; |
| 122 | } |
| 123 | |
| 124 | export const DIRS = { |
| 125 | root: path.join(ROOT, '.mct'), |
| 126 | cache: path.join(ROOT, '.mct', 'cache'), |
| 127 | server: path.join(ROOT, '.mct', 'server'), |
| 128 | snapshots: path.join(ROOT, '.mct', 'snapshots'), |
| 129 | clients: path.join(ROOT, '.mct', 'clients'), |
| 130 | artifacts: path.join(ROOT, '.mct', 'artifacts'), |
| 131 | reports: path.join(ROOT, '.mct', 'reports'), |
| 132 | auth: path.join(ROOT, '.mct', 'auth'), |
| 133 | run: path.join(ROOT, '.mct', 'run'), |
| 134 | }; |
| 135 | |
| 136 | export function ensureDirs(): void { |
| 137 | for (const d of Object.values(DIRS)) fs.mkdirSync(d, { recursive: true }); |
| 138 | } |
| 139 | |
| 140 | /** Local secrets (RCON password). Never printed by the harness. */ |
| 141 | export function secrets(): { rconPassword: string } { |
| 142 | const file = path.join(DIRS.root, 'secrets.json'); |
| 143 | if (fs.existsSync(file)) return JSON.parse(fs.readFileSync(file, 'utf8')); |
| 144 | const s = { rconPassword: crypto.randomBytes(18).toString('base64url') }; |
| 145 | fs.mkdirSync(DIRS.root, { recursive: true }); |
| 146 | fs.writeFileSync(file, JSON.stringify(s, null, 2), { mode: 0o600 }); |
| 147 | return s; |
| 148 | } |
| 149 | |
| 150 | /** Expand `dir/name-*.jar` style globs (wildcards only in the last path segment). */ |
| 151 | export function expandGlob(pattern: string, base = ROOT): string[] { |
| 152 | const abs = path.isAbsolute(pattern) ? pattern : path.join(base, pattern); |
| 153 | const dir = path.dirname(abs); |
| 154 | const name = path.basename(abs); |
| 155 | if (!name.includes('*')) return fs.existsSync(abs) ? [abs] : []; |
| 156 | if (!fs.existsSync(dir)) return []; |
| 157 | const re = new RegExp('^' + name.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*') + '$'); |
| 158 | return fs |
| 159 | .readdirSync(dir) |
| 160 | .filter((f) => re.test(f) && !/-(sources|javadoc|dev)\.jar$/.test(f)) |
| 161 | .map((f) => path.join(dir, f)); |
| 162 | } |
| 163 | |
| 164 | export function stamp(): string { |
| 165 | return new Date().toISOString().replace(/[:.]/g, '-'); |
| 166 | } |