HeadlessMC Dart

A Dart package that simplifies downloading and running HeadlessMC, allowing you to launch and manage Minecraft servers and clients headlessly.

Features

  • Download and manage HeadlessMC launcher
  • Launch Minecraft with various configurations
  • Support for different Minecraft versions and mod loaders
  • Process management and interaction
  • Headless operation mode (without GUI)

Installation

Add this package to your pubspec.yaml:

dependencies:
  headlessmc_dart: ^0.0.2

Then run:

flutter pub get

Usage

Basic Launch Flow

The basic flow involves setting up a launcher with the proper configuration and then launching Minecraft:

import 'package:headlessmc_dart/headlessmc_dart.dart';
import 'dart:io';
import 'package:path/path.dart' as p;

// Initialize the launcher with required parameters
final launcher = HeadlessMCLauncher(
  javaExecutablePath: Platform.isWindows ? 'java.exe' : 'java',
  headlessmcJarPath: '/path/to/headlessmc-launcher-wrapper-3.0.0.jar',
  gameDir: '/path/to/minecraft/directory',
  mcDir: '/path/to/minecraft/directory',
  workingDirectory: Directory('/path/to/minecraft/directory'),
  stay: true, // Keep HeadlessMC running after Minecraft exits
);

// Start HeadlessMC
await launcher.start();

// Send commands to HeadlessMC
await launcher.send('loglevel ALL'); // Set log level

// Launch Minecraft with Fabric mod loader
await launcher.send('launch fabric:1.21.2');

// Wait for Minecraft to exit
await launcher.isRunningStream.firstWhere((running) => !running);

Downloading Libraries

You can use the library downloader to fetch HeadlessMC or other required files:

import 'package:headlessmc_dart/headlessmc_dart.dart';
import 'dart:io';
import 'package:path/path.dart' as p;

// Create a downloader for HeadlessMC JAR
final downloader = LibraryDownloader(
  url: 'https://github.com/3arthqu4ke/headlessmc/releases/download/2.6.1/headlessmc-launcher-wrapper-2.6.1.jar',
  destinationDirectory: '/path/to/download/directory',
  onProgress: (progress) {
    debugPrint('Downloaded: ${progress.downloadedBytes} / ${progress.totalBytes} bytes');
    debugPrint('Progress: ${progress.percentage}%');
  },
  onComplete: (file) {
    debugPrint('Download complete: ${file.path}');
  },
);

// Start the download
final file = await downloader.download();

// Check download status
final latestProgress = downloader.progress;
if (latestProgress != null && latestProgress.percentage == 100) {
  debugPrint('Download completed successfully');
}

Using Custom Filename for Downloads

You can specify a custom filename when downloading:

import 'package:headlessmc_dart/headlessmc_dart.dart';
import 'dart:io';

// Download with a custom filename
final downloader = LibraryDownloader(
  url: 'https://github.com/3arthqu4ke/headlessmc/releases/download/2.6.1/headlessmc-launcher-wrapper-2.6.1.jar',
  destinationDirectory: '/path/to/download/directory',
  filename: 'custom-launcher.jar',
);

final file = await downloader.download();

Understanding HeadlessMC Launcher Properties

The HeadlessMCLauncher class has several important properties:

// Check if Minecraft is running
final isMinecraftRunning = launcher.isMinecraftRunning;

// Access the Minecraft process if it's running
final minecraftProcess = launcher.minecraftProcess;

// Check if HeadlessMC is running
final isHeadlessMCRunning = launcher.isHeadlessMCRunning;

// Access the HeadlessMC process if it's running
final headlessmcProcess = launcher.headlessmcProcess;

// Listen to the isRunning stream to know when Minecraft starts or stops
launcher.isRunningStream.listen((running) {
  if (running) {
    debugPrint('Minecraft started');
  } else {
    debugPrint('Minecraft stopped');
  }
});

License

This package is available under the MIT License.