# 20.3 我的世界（Minecraft）

本节将介绍 Minecraft 在 FreeBSD 上的客户端与服务器端部署方案。Minecraft 是一款使用 Java 开发的沙盒游戏，因此部署前需先配置 Java 运行环境，并涉及启动器选择等多个技术层面。

## 桌面端

### 安装 OpenJDK

Minecraft 对 JDK 版本有特定要求，JDK 8 及更早版本不受支持。本文测试使用 JDK 21 可正常运行，该版本在 FreeBSD 上提供了良好的兼容性和稳定性。

可使用 pkg 或 Ports 两种方式安装 OpenJDK。pkg 提供预编译二进制包，安装速度快；Ports 允许从源代码编译，可进行自定义配置。

使用 pkg 安装 OpenJDK 21：

```sh
# pkg install openjdk21
```

或者使用 Ports 安装 OpenJDK 21：

```sh
# cd /usr/ports/java/openjdk21/
# make install clean
```

### Minecraft 客户端

FreeBSD 中常见有两种启动器供选择，分别是 HMCL 和 Prism Launcher。其中 Prism Launcher 可在 FreeBSD Ports 中获得，而 HMCL 只能通过手动下载 `.jar` 文件运行。

#### HMCL 启动器

HMCL（Hello Minecraft! Launcher）是一款使用 Java 语言开发的 Minecraft 启动器，支持多版本管理和模组加载。

**配置 HMCL 启动器**

本小节介绍 HMCL 启动器的配置方法。配置启动器前需先获取安装文件。

从 [releases](https://github.com/HMCL-dev/HMCL/releases) 页面下载最新的发行版本。

打开终端执行命令，使用 Java 运行 HMCL 启动器的 JAR 文件：

```sh
$ java -jar HMCL*.jar
```

并非所有 Minecraft 版本都受支持，适配情况请参考 [平台支持状态](https://github.com/HMCL-dev/HMCL/blob/main/docs/PLATFORM_zh.md) 文档。其他设置与其他平台相同，关键配置包括 Java 路径设置、游戏目录选择和内存分配。

**使用 HMCL 启动游戏**

使用 HMCL 启动游戏的具体操作可参考下图。

![FreeBSD 玩 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-59b1ce8d5fbe19d604d8470d00fb60b333243384%2Fmc1.png?alt=media)

![FreeBSD 玩 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-9e1cbaebdfe9e6e75ab5a46092c73e23d69ec969%2Fmc2.png?alt=media)

#### Prism Launcher 启动器

Prism Launcher 是一款使用 C++ 和 Qt 框架开发的开源启动器。Prism Launcher 作为开源项目，其设计理念强调正版验证，因此默认禁止离线登录，并对[绕过方法](https://github.com/antunnitraj/Prism-Launcher-PolyMC-Offline-Bypass)进行限制。因此仅推荐正版用户使用。

**安装 Prism Launcher 启动器**

可使用 pkg 或 Ports 两种方式安装 Prism Launcher。

使用 pkg 安装 Prism Launcher：

```sh
# pkg install prismlauncher
```

还可使用 Ports 安装 Prism Launcher：

```sh
# cd /usr/ports/games/prismlauncher/
# make install clean
```

**配置 Prism Launcher**

本小节介绍 Prism Launcher 的配置步骤。配置时需注意桌面环境的兼容性。

若使用 KDE 桌面环境，还需要更新第三方软件包到最新版本。KDE 桌面环境对 Qt 版本有特定要求，Prism Launcher 依赖的 Qt 库需要与系统库保持一致。

```sh
# pkg upgrade
```

否则可能无法启动，终端会提示 Qt 库版本不兼容或缺少符号等错误信息。

点击启动程序：

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-01d0eac89a7113d3e7d2f791588a6b3bbd02e421%2Fmcp01.png?alt=media)

此处为语言设置，默认支持中文。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-593f23f43e72f2ec134abc83f4ac33406d46b820%2Fmcp02.png?alt=media)

此处设置 Java 版本，选择默认设置即可。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-a50e9e0f209fe2e7540e1046bc5624420d22faa8%2Fmcp03.png?alt=media)

外观设置，选择默认即可。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-4149d968d52dfa967b606a8630d49c8f8339399c%2Fmcp04.png?alt=media)

**使用 Prism Launcher 启动《Minecraft》**

启动程序后，可以看到 Prism Launcher 支持中文，但需要正版登录。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-699f8c306fddccaa6a1c3470830963dd87431c45%2Fmcpl1.png?alt=media)

登录后下载最新版本游戏。

> **注意**
>
> 需要 Minecraft 正版账号才能登录游戏，可 [购买](https://www.minecraft.net/zh-hans/store/minecraft-java-bedrock-edition-pc) 该游戏。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-d6bf209bf6d31689923d06b3b4f391da4568904e%2Fmcpl2.png?alt=media)

进入我的世界游戏。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-95ff5ad1172265e4892afac55d3f0ea61243af81%2Fmcpl3.png?alt=media)

进入游戏画面。

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-b83a83101112ae0c57c48d5a15df048a5efb71fd%2Fmcpl4.png?alt=media)

## 服务器端

本小节介绍如何在 FreeBSD 系统上搭建 Minecraft 服务器。搭建服务器需先安装 Java 运行环境，服务器端与客户端对 Java 版本的要求通常保持一致。

### 相关文件结构

```sh
/
├── etc/
│   └── rc.conf # 系统配置文件，包含 minecraft 服务配置
├── usr/
│   └── local/
│       ├── bin/
│       │   └── minecraft-server # Minecraft 服务器可执行文件
│       └── etc/
│           └── minecraft-server/ # Minecraft 服务器配置目录
│               ├── eula.txt # 用户协议文件
│               ├── java-args.txt # Java 参数配置
│               └── server.properties # 服务器配置文件
├── var/
│   ├── log/
│   │   └── minecraft-server/ # 日志和调试输出目录
│   └── db/
│       └── minecraft-server/ # 世界文件目录
```

### 安装 OpenJDK

过旧的 JDK 版本不受服务器端支持，本文测试使用 JDK 21 可正常运行，该版本在服务器环境中表现稳定。

使用 pkg 安装 OpenJDK 21：

```sh
# pkg install openjdk21
```

或者使用 Ports 编译安装 OpenJDK 21，可针对服务器硬件进行优化：

```sh
# cd /usr/ports/java/openjdk21/
# make install clean
```

### 使用 Ports 或官方服务器程序

本小节介绍两种搭建 Minecraft 服务器的方法。用户可根据需求选择适合的方式。

经过测试 [Minecraft 官方服务器端](https://www.minecraft.net/en-us/download/server)可以直接在 FreeBSD 15.0 上运行，只需安装 OpenJDK 即可。官方服务器程序为纯 Java 程序，具有良好的跨平台兼容性。Ports 版本则提供了 FreeBSD 特定的集成配置，包括服务管理脚本和默认配置文件。

也可使用 Ports 中提供的 `games/minecraft-server`：

```sh
# cd /usr/ports/games/minecraft-server/
# make install clean
```

查看配置信息：

```sh
# pkg info -D minecraft-server
minecraft-server-1.21.1:
On install:
When you first run minecraft-server, it will populate the file
/usr/local/etc/minecraft-server/eula.txt
首次运行 minecraft-server 时，它会生成文件 /usr/local/etc/minecraft-server/eula.txt

It is required to read the EULA, and then set eula=true
需要阅读 EULA，然后将 eula=true 设置为已接受。

- Configuration files can be found in /usr/local/etc/minecraft-server/
- 配置文件可以在 /usr/local/etc/minecraft-server/ 找到
- Log and debug output files can be found in /var/log/minecraft-server/
- 日志和调试输出文件可以在 /var/log/minecraft-server/ 找到
- World files can be found in /var/db/minecraft-server/
- 世界文件可以在 /var/db/minecraft-server/ 找到

Without daemon option:
- To run the server, run /usr/local/bin/minecraft-server # 运行方法
- To edit java's parameters, edit /usr/local/etc/minecraft-server/java-args.txt # java 参数
- To run with a specific version of Java, set environment variable JAVA_VERSION, # 指定某一 java 版本
  for example:
    export JAVA_VERSION=22
    /usr/local/bin/minecraft-server
  or:
    JAVA_VERSION=22 /usr/local/bin/minecraft-server

With daemon option:
- The service has been installed with the name 'minecraft'
- 服务已安装，名称为 'minecraft'
- To adjust maximum memory usage (-Xmx), use minecraft_memx= in /etc/rc.conf  
- 要调整最大内存使用量 (-Xmx)，请在 /etc/rc.conf 中使用 minecraft_memx=
- To adjust initial memory usage (-Xms), use minecraft_mems= in /etc/rc.conf 
- 要调整初始内存使用量 (-Xms)，请在 /etc/rc.conf 中使用 minecraft_mems=
- To add other java parameters, use minecraft_args= in /etc/rc.conf
- 要添加其他 java 参数，请在 /etc/rc.conf 中使用 minecraft_args=
- To run with a specific version of Java, use minecraft_java_version= in /etc/rc.conf 
- 要使用特定版本的 Java，请在 /etc/rc.conf 中使用 minecraft_java_version=
- To see the interactive console, type service minecraft console
- 要查看交互式控制台，请输入 service minecraft console
```

```sh
$ /usr/local/bin/minecraft-server

…………省略一部分…………

[15:52:21] [ServerMain/WARN]: Failed to load eula.txt
[15:52:21] [ServerMain/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.
```

提示需要同意许可协议，请将 `/usr/local/etc/minecraft-server/eula.txt` 中的 `eula=false` 修改为 `eula=true`。

再次运行已安装的 Minecraft 服务器程序：

```sh
# /usr/local/bin/minecraft-server
Starting net.minecraft.server.Main
[15:54:47] [ServerMain/INFO]: Environment: Environment[sessionHost=https://sessionserver.mojang.com, servicesHost=https://api.minecraftservices.com, name=PROD]
[15:54:48] [ServerMain/INFO]: No existing world data, creating new world
[15:54:49] [ServerMain/INFO]: Loaded 1290 recipes
[15:54:49] [ServerMain/INFO]: Loaded 1399 advancements
[15:54:49] [Server thread/INFO]: Starting minecraft server version 1.21.1
[15:54:49] [Server thread/INFO]: Loading properties
[15:54:49] [Server thread/INFO]: Default game type: SURVIVAL
[15:54:49] [Server thread/INFO]: Generating keypair
[15:54:49] [Server thread/INFO]: Starting Minecraft server on *:25565
[15:54:50] [Server thread/INFO]: Using default channel type
[15:54:50] [Server thread/INFO]: Preparing level "world"
[15:54:53] [Server thread/INFO]: Preparing start region for dimension minecraft:overworld
[15:54:53] [Worker-Main-2/INFO]: Preparing spawn area: 2%
[15:54:54] [Worker-Main-2/INFO]: Preparing spawn area: 2%
[15:54:54] [Worker-Main-3/INFO]: Preparing spawn area: 2%
[15:54:55] [Worker-Main-1/INFO]: Preparing spawn area: 2%
[15:54:55] [Worker-Main-2/INFO]: Preparing spawn area: 18%
[15:54:56] [Worker-Main-1/INFO]: Preparing spawn area: 51%
[15:54:56] [Worker-Main-3/INFO]: Preparing spawn area: 51%
[15:54:56] [Server thread/INFO]: Time elapsed: 3317 ms
[15:54:56] [Server thread/INFO]: Done (6.876s)! For help, type "help"
```

按 **Ctrl** + **C** 中断程序运行。

在当前默认配置下，服务器启用了正版验证机制。该机制通过与 Mojang 服务器通信验证玩家身份，非正版用户和未登录用户无法连接该服务器。如需在局域网或测试环境中使用，可关闭正版验证功能。在 `/usr/local/etc/minecraft-server/server.properties` 文件中，将 `online-mode=true` 修改为 `online-mode=false` 即可关闭正版验证功能，该操作会降低服务器安全性，请在受控环境中使用。

然后再次运行已安装的 Minecraft 服务器程序：

```sh
# /usr/local/bin/minecraft-server
Starting net.minecraft.server.Main
[18:47:47] [ServerMain/INFO]: Environment: Environment[sessionHost=https://sessionserver.mojang.com, servicesHost=https://api.minecraftservices.com, name=PROD]
[18:47:51] [ServerMain/INFO]: Loaded 1337 recipes
[18:47:51] [ServerMain/INFO]: Loaded 1448 advancements
[18:47:51] [Server thread/INFO]: Starting minecraft server version 1.21.3
[18:47:51] [Server thread/INFO]: Loading properties
[18:47:51] [Server thread/INFO]: Default game type: SURVIVAL
[18:47:51] [Server thread/INFO]: Generating keypair
[18:47:51] [Server thread/INFO]: Starting Minecraft server on *:25565
[18:47:51] [Server thread/INFO]: Using default channel type
[18:47:52] [Server thread/WARN]: **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
[18:47:52] [Server thread/WARN]: The server will make no attempt to authenticate usernames. Beware.
[18:47:52] [Server thread/WARN]: While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.
[18:47:52] [Server thread/WARN]: To change this, set "online-mode" to "true" in the server.properties file.
[18:47:52] [Server thread/INFO]: Preparing level "world"
[18:47:52] [Server thread/INFO]: Preparing start region for dimension minecraft:overworld
[18:47:53] [Worker-Main-5/INFO]: Preparing spawn area: 0%
[18:47:53] [Worker-Main-5/INFO]: Preparing spawn area: 0%
[18:47:53] [Worker-Main-5/INFO]: Preparing spawn area: 0%
[18:47:53] [Server thread/INFO]: Time elapsed: 1233 ms
[18:47:53] [Server thread/INFO]: Done (1.709s)! For help, type "help"
[18:48:34] [Server thread/INFO]: ykla[/127.0.0.1:37462] logged in with entity id 39 at (-1.5, 63.0, 1.5)
[18:48:34] [Server thread/INFO]: ykla joined the game
```

![FreeBSD 运行 Minecraft](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-94de63b629d63ca83d4910bfc1c935a182b13c2a%2Fmc3.png?alt=media)

## 课后习题

1. 查找 HMCL 启动器的官方源代码，尝试将其移植到 FreeBSD Ports 中，并构建一个可通过 pkg 安装的包，分析其移植过程中需要解决的 Java 应用适配问题。
2. 优化 Minecraft 服务器，使其资源占用最优，性能最优。
