mc-minder 0.6.0-alpha.13

A smart management suite for Minecraft servers(fabric) on Linux/Termux/Android
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use anyhow::{Context, Result};
use log::info;
use serde::Deserialize;
use std::path::Path;

/// Supported server core types.
#[derive(Debug, Clone, PartialEq)]
pub enum CoreType {
    Fabric,
    Paper,
    Vanilla,
}

impl CoreType {
    pub fn all() -> Vec<CoreType> {
        vec![CoreType::Fabric, CoreType::Vanilla, CoreType::Paper]
    }

    pub fn display_name(&self) -> &'static str {
        match self {
            CoreType::Fabric => "Fabric (Mod Loader)",
            CoreType::Vanilla => "Vanilla (Official Mojang)",
            CoreType::Paper => "Paper (Performance Fork)",
        }
    }

    pub fn display_name_cn(&self) -> &'static str {
        match self {
            CoreType::Fabric => "Fabric (模组加载器)",
            CoreType::Vanilla => "Vanilla (Mojang 官方)",
            CoreType::Paper => "Paper (性能优化版)",
        }
    }
}

// ============================================================
// FabricMC Meta API
// ============================================================

#[derive(Debug, Deserialize)]
struct FabricGameVersion {
    version: String,
    stable: bool,
}

#[derive(Debug, Deserialize)]
struct FabricLoaderVersion {
    version: String,
    stable: bool,
}

/// Fetch available Minecraft versions from FabricMC.
pub async fn fetch_fabric_game_versions() -> Result<Vec<String>> {
    let client = reqwest::Client::new();
    let resp: Vec<FabricGameVersion> = client
        .get("https://meta.fabricmc.net/v2/versions/game")
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch FabricMC game versions")?
        .json()
        .await
        .context("Failed to parse FabricMC game versions")?;

    // Return stable versions, newest first
    let mut versions: Vec<String> = resp
        .into_iter()
        .filter(|v| v.stable)
        .map(|v| v.version)
        .collect();
    versions.reverse(); // newest first
    Ok(versions)
}

/// Fetch stable Fabric loader version for a game version.
pub async fn fetch_fabric_loader(game_version: &str) -> Result<String> {
    let client = reqwest::Client::new();
    let resp: Vec<FabricLoaderVersion> = client
        .get(format!(
            "https://meta.fabricmc.net/v2/versions/loader/{}",
            game_version
        ))
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch Fabric loader versions")?
        .json()
        .await
        .context("Failed to parse Fabric loader versions")?;

    resp.into_iter()
        .find(|v| v.stable)
        .map(|v| v.version)
        .ok_or_else(|| anyhow::anyhow!("No stable Fabric loader found for {}", game_version))
}

/// Download Fabric server jar.
pub async fn download_fabric_server(
    game_version: &str,
    loader_version: &str,
    output_dir: &Path,
) -> Result<String> {
    let url = format!(
        "https://meta.fabricmc.net/v2/versions/loader/{}/{}/server/jar",
        game_version, loader_version
    );
    let filename = format!("fabric-server-{}.jar", game_version);
    download_to_file(&url, output_dir, &filename).await
}

// ============================================================
// Vanilla (Mojang) API
// ============================================================

#[derive(Debug, Deserialize)]
struct VersionManifest {
    versions: Vec<VersionEntry>,
}

#[derive(Debug, Deserialize)]
struct VersionEntry {
    id: String,
    #[serde(rename = "type")]
    release_type: String,
    url: String,
}

#[derive(Debug, Deserialize)]
struct VersionInfo {
    downloads: VersionDownloads,
}

#[derive(Debug, Deserialize)]
struct VersionDownloads {
    server: DownloadInfo,
}

#[derive(Debug, Deserialize)]
struct DownloadInfo {
    url: String,
}

/// Fetch available Vanilla Minecraft versions.
pub async fn fetch_vanilla_versions() -> Result<Vec<String>> {
    let client = reqwest::Client::new();
    let manifest: VersionManifest = client
        .get("https://launchermeta.mojang.com/mc/game/version_manifest.json")
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch Mojang version manifest")?
        .json()
        .await
        .context("Failed to parse Mojang version manifest")?;

    let mut versions: Vec<String> = manifest
        .versions
        .into_iter()
        .filter(|v| v.release_type == "release")
        .map(|v| v.id)
        .collect();
    versions.reverse(); // newest first
    Ok(versions)
}

/// Download Vanilla Minecraft server jar.
pub async fn download_vanilla_server(
    game_version: &str,
    output_dir: &Path,
) -> Result<String> {
    // First, get the manifest to find the version URL
    let client = reqwest::Client::new();
    let manifest: VersionManifest = client
        .get("https://launchermeta.mojang.com/mc/game/version_manifest.json")
        .header("User-Agent", "mc-minder")
        .send()
        .await?
        .json()
        .await?;

    let entry = manifest
        .versions
        .iter()
        .find(|v| v.id == game_version)
        .ok_or_else(|| anyhow::anyhow!("Version {} not found in Mojang manifest", game_version))?;

    // Get version info to find download URL
    let info: VersionInfo = client
        .get(&entry.url)
        .header("User-Agent", "mc-minder")
        .send()
        .await?
        .json()
        .await?;

    let filename = format!("minecraft-server-{}.jar", game_version);
    download_to_file(&info.downloads.server.url, output_dir, &filename).await
}

// ============================================================
// PaperMC API
// ============================================================

#[derive(Debug, Deserialize)]
struct PaperProject {
    versions: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct PaperVersionBuilds {
    builds: Vec<i64>,
}

#[derive(Debug, Deserialize)]
struct PaperBuildInfo {
    downloads: PaperDownloads,
}

#[derive(Debug, Deserialize)]
struct PaperDownloads {
    application: PaperDownloadFile,
}

#[derive(Debug, Deserialize)]
struct PaperDownloadFile {
    name: String,
}

/// Fetch available PaperMC versions.
pub async fn fetch_paper_versions() -> Result<Vec<String>> {
    let client = reqwest::Client::new();
    let project: PaperProject = client
        .get("https://api.papermc.io/v2/projects/paper")
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch PaperMC versions")?
        .json()
        .await
        .context("Failed to parse PaperMC versions")?;

    let mut versions = project.versions;
    versions.reverse(); // newest first
    Ok(versions)
}

/// Download PaperMC server jar (latest build for a version).
pub async fn download_paper_server(
    game_version: &str,
    output_dir: &Path,
) -> Result<String> {
    let client = reqwest::Client::new();

    // Get latest build
    let builds: PaperVersionBuilds = client
        .get(format!(
            "https://api.papermc.io/v2/projects/paper/versions/{}",
            game_version
        ))
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch PaperMC builds")?
        .json()
        .await
        .context("Failed to parse PaperMC builds")?;

    let latest_build = builds
        .builds
        .last()
        .ok_or_else(|| anyhow::anyhow!("No builds found for PaperMC {}", game_version))?;

    // Get download URL
    let build_info: PaperBuildInfo = client
        .get(format!(
            "https://api.papermc.io/v2/projects/paper/versions/{}/builds/{}",
            game_version, latest_build
        ))
        .header("User-Agent", "mc-minder")
        .send()
        .await?
        .json()
        .await?;

    let download_name = &build_info.downloads.application.name;
    let url = format!(
        "https://api.papermc.io/v2/projects/paper/versions/{}/builds/{}/downloads/{}",
        game_version, latest_build, download_name
    );

    let filename = format!("paper-{}.jar", game_version);
    download_to_file(&url, output_dir, &filename).await
}

// ============================================================
// Modrinth API (Mod platform integration)
// ============================================================

#[derive(Debug, Deserialize)]
pub struct ModrinthSearchResult {
    pub hits: Vec<ModrinthHit>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ModrinthHit {
    pub project_id: String,
    pub title: String,
    pub description: String,
    pub downloads: i64,
    pub project_type: String,
    pub slug: String,
}

#[derive(Debug, Deserialize)]
pub struct ModrinthVersion {
    pub files: Vec<ModrinthFile>,
    pub version_number: String,
}

#[derive(Debug, Deserialize)]
pub struct ModrinthFile {
    pub url: String,
    pub filename: String,
}

/// Search Modrinth for mods.
pub async fn search_modrinth(query: &str, limit: u32) -> Result<Vec<ModrinthHit>> {
    let client = reqwest::Client::new();
    let url = format!(
        "https://api.modrinth.com/v2/search?query={}&limit={}&facets=[[\"project_type:mod\"],[\"categories:fabric\"]]",
        urlencoding(query),
        limit
    );
    let resp: ModrinthSearchResult = client
        .get(&url)
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to search Modrinth")?
        .json()
        .await
        .context("Failed to parse Modrinth search")?;

    Ok(resp.hits)
}

/// Get the latest stable version file for a Modrinth project.
pub async fn get_modrinth_latest_version(project_id: &str, game_version: &str) -> Result<ModrinthFile> {
    let client = reqwest::Client::new();
    let url = format!(
        "https://api.modrinth.com/v2/project/{}/version?loaders=[\"fabric\"]&game_versions=[\"{}\"]",
        project_id, game_version
    );
    let versions: Vec<ModrinthVersion> = client
        .get(&url)
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .context("Failed to fetch Modrinth versions")?
        .json()
        .await
        .context("Failed to parse Modrinth versions")?;

    let version = versions.into_iter().next()
        .ok_or_else(|| anyhow::anyhow!("No compatible version found for {}", game_version))?;

    version.files.into_iter().next()
        .ok_or_else(|| anyhow::anyhow!("No download file for Modrinth project {}", project_id))
}

/// Download a mod from Modrinth URL to the mods folder.
pub async fn download_modrinth_mod(url: &str, filename: &str, output_dir: &Path) -> Result<String> {
    let mods_dir = output_dir.join("mods");
    std::fs::create_dir_all(&mods_dir).ok();
    download_to_file(url, &mods_dir, filename).await
}

/// Popular Fabric mods for quick install.
pub fn popular_mods() -> Vec<(&'static str, &'static str, &'static str)> {
    vec![
        ("fabric-api", "Fabric API", "P7dR8mSH"),
        ("sodium", "Sodium", "AANobbMI"),
        ("lithium", "Lithium", "gvQqBUqZ"),
        ("phosphor", "Phosphor", "fQEb0iXm"),
        ("iris", "Iris Shaders", "YL57xq9U"),
        ("modmenu", "Mod Menu", "mOgUt4GM"),
    ]
}

/// Simple URL encoding (just replaces space with %20).
fn urlencoding(s: &str) -> String {
    s.replace(' ', "%20")
        .replace('&', "%26")
        .replace('?', "%3F")
        .replace('#', "%23")
}


async fn download_to_file(url: &str, output_dir: &Path, filename: &str) -> Result<String> {
    let output_path = output_dir.join(filename);
    info!("Downloading {} from {}", filename, url);

    let client = reqwest::Client::new();
    let response = client
        .get(url)
        .header("User-Agent", "mc-minder")
        .send()
        .await
        .with_context(|| format!("Failed to download from {}", url))?;

    if !response.status().is_success() {
        return Err(anyhow::anyhow!(
            "Download failed: HTTP {} from {}",
            response.status(),
            url
        ));
    }

    let bytes = response
        .bytes()
        .await
        .context("Failed to read download response")?;

    std::fs::write(&output_path, &bytes)
        .with_context(|| format!("Failed to write to {:?}", output_path))?;

    info!("Downloaded {} ({} bytes)", filename, bytes.len());
    Ok(output_path.to_string_lossy().to_string())
}