泰拉瑞亚服务器搭建教程(带 MOD)

搭建 Terraria 服务器的好处在于,能够不受限制的随时加入。如果客户端和服务端在同一台电脑上(也就是最寻常的一个人当房主其他人加入的模式),那么只有在房主玩游戏的时候别人才能加入。

写这份教程之前,我曾经搭建成功过一次,具体过程几乎全忘了,如今只能从头开始。这次我会把搭建的过程记录下来,一方面为我下次搭建提供便利(如果真的还有下次的话),另一方面,希望能帮助到正在阅读这篇文章的你。

Update 2

原来有 docker 镜像,根本不需要这些繁琐的步骤。(注:这个镜像解决了魔法存储的问题。)

https://github.com/Rfvgyhn/tmodloader-docker

Update

下载游戏服务器并不需要借助 steam,官方已经提供了服务器所需的文件。下载链接可以在这里找到,下载后解压即可。

如果要运行原版的话,跳过 tModLoader 覆盖文件的部分,运行 tModLoaderServer 改为运行 TerrariaServer 即可。

环境说明

阿里云 ECS:2C4G,1M 带宽,Ubuntu 18.04 镜像

步骤

安装 SteamCMD

1
2
apt update
apt install steamcmd

如果出现 E: Unable to locate package steamcmd,在 /etc/apt/sources.list 中添加遗漏的 multiverse 源(其实就是一些闭源软件的源),注意 Ubuntu 的版本代号要保证一致(比如我的就是 bionic)。修改后重新执行更新和安装的命令。

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
## /etc/apt/sources.list 文件内容

## 修改后
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

## 修改前
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic main
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic main

deb http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic-updates main
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic-updates main

deb http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic universe
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic universe
deb http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic-updates universe
deb-src http://mirrors.cloud.aliyuncs.com/ubuntu/ bionic-updates universe

创建并切换用户

1
2
3
4
useradd -m steam
su steam
bash # 切换到 bash 以保证命令行体验
cd ~

安装游戏

Terraria 必须登陆后才能下载

1
2
3
4
steamcmd # 进入 Steam> 交互模式
login <你的 steam 用户名>
# 然后输入密码(以及两步验证)
app_update 105600 # 安装 Terraria

安装 tModLoader

访问 https://github.com/tModLoader/tModLoader/releases 挑一个最新的版本并复制链接(选 Linux 版,文件名形如 tModLoader.Linux.v0.11.6.2.tar.gz

1
2
3
4
5
6
cd ~/.steam/steam/SteamApps/common/Terraria/
wget -c https://github.com/tModLoader/tModLoader/releases/download/v0.11.6.2/tModLoader.Linux.v0.11.6.2.tar.gz # 自行替换文件名
tar -zxvf tModLoader.Linux.v0.11.6.2.tar.gz # 解压文件
chmod +x Terraria
chmod +x tModLoaderServer # 增加执行权限
./tModLoaderServer # 运行

运行后会看到选择世界和 MOD 管理的菜单。

添加 MOD

首次运行 tModLoaderServer 后会自动创建一些目录,把需要的 Mod 拷贝到 ~/.local/share/Terraria/ModLoader/Mods 下就好了。开启的话需要进 tModLoaderServer,然后选择世界(或者先创建世界)就好了。

开放端口

进阿里云的 console,找到修改安全组规则的地方,添加安全组规则,协议类型全部,授权对象 0.0.0.0/0 就好了。不过这是无脑开放所有端口和协议的做法,其实开启 7777 端口的 TCP 和 UDP 就够了。

一个自制脚本

cmd 变量中的路径和世界名字按需要更改,脚本提供了以下功能:

  • 每隔 3 分钟自动存档
  • 每隔 15 分钟报时
  • 玩家加入时发送欢迎文字
  • 玩家在游戏中输入 exit 后过 60s 关闭服务器
  • 玩家在游戏中输入 dawn 后切换到黎明
  • 每 15 分钟自动备份存档,注意硬盘容量的问题,按照大世界 10M 来算的话,大概一天要用掉 1G 的空间。

使用方法:python3 xxx.py WorldName(可以在末尾追加额外参数,如 -port 7778-password secret),如果确定正常后,为了保证后台运行,可以在命令前加 screen(需要安装 screen 包)。

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
import subprocess
from threading import Thread
from queue import Queue, Empty
import psutil
from datetime import datetime
import time
import traceback
import sys
import os

world_name = sys.argv[1]

server_path = "/home/steam/.steam/SteamApps/common/Terraria"
world_path = "/home/steam/.local/share/Terraria/ModLoader/Worlds"
backup_path = world_path + "/Backups"

cmd = ("%s/tModLoaderServer.bin.x86_64 -world %s/%s.wld" % (server_path,
world_path,
world_name)).split() + sys.argv[2:]

p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)


def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()


q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q), daemon=True)
t.start()


pid = p.pid


def monitor():
print(pid)
ready = False

while psutil.pid_exists(pid):
def write(s):
s += '\n'
p.stdin.write(s.encode())
p.stdin.flush()
try:
line = q.get_nowait().decode()
print(line[:-1])
if not ready and "Type 'help' for a list of commands." in line:
ready = True
if ready:
if "exit" in line:
write("say Closing in 60s...")
time.sleep(60)
write("say Now closing...")
write("exit")
if "dawn" in line:
write("say Switch game time 4:30AM.")
write("dawn")
if "dusk" in line:
write("say Switch game time 7:30PM.")
write("dusk")
if "has joined." in line:
write("say Welcome!")
except Empty:
if ready:
now = datetime.now()
if now.second < 5:
if now.minute % 15 == 0:
write("say Current time: %02d:%02d." % (now.hour, now.minute))
for ext in [".wld", ".twld"]:
os.system("cp %s %s" % (world_path + '/' + world_name + ext,
backup_path + '/' + world_name + datetime.now().strftime("-%Y-%M-%d-%H-%M") + ext))

if now.minute % 3 == 1:
# write("say Now saving...")
write("save")
time.sleep(5)


try:
monitor()
except Exception as e:
p.stdin.write(b'exit')
traceback.print_exc()

为了安装名为 psutil 的 python 包,需要 root 下运行

1
2
apt install python3-pip
pip3 install psutil

Magic Storage 魔法存储 无法使用的问题的解决方法(显示为 0/0,大量物块消失)

背景

对我来说,魔法存储是最不可或缺的 MOD,对于多人模式来说更是如此。用大量箱子分类物品,还要定时扔掉没用的东西,合成物品之前还要先把东西一个个拿出来,会消耗不少时间。尤其是玩得多了(比如超过一周目)之后,可能完全无法从中获取乐趣,此时魔法存储就是救世主了。

然而在多人游戏中, 辛辛苦苦挖到钻石之后造出魔法存储之后,发现上限为 0,往里面放了物品后还是 0/0。然后我就重新进了游戏,发现以魔法存储为中心,很大一片区域的物块都消失了。由于我把魔法存储放在了家里,空中还有 NPC 不停往下掉,就像是在下 NPC 雨,颇为壮观。如果把存档从服务器拷贝到本地运行的话,完全不会有任何问题。于是在查了好久原因和解决方法之后,终于找到了便捷有效的方案。

解决方法

安装 mono 后把缺失的共享链接库拷贝到 TR Server 所在的文件夹即可。

1
2
apt-get install mono-runtime
cp /usr/lib/libMonoPosixHelper.so /home/steam/.steam/SteamApps/common/Terraria

原因

直接参考官方的 issue 吧,但是令人惊讶的是,这个问题已经一年多了还没得到妥善处理,看来 Linux 服务器也不受待见啊。

https://github.com/blushiemagic/MagicStorage/issues/41#issuecomment-450196167

https://github.com/blushiemagic/MagicStorage/issues/96

参考链接

https://nodecraft.com/support/games/terraria/tmodloader-server-guide

https://developer.valvesoftware.com/wiki/SteamCMD

https://www.jxtxzzw.com/archives/3629 注:里面提到的是本人哦

https://terraria.gamepedia.com/Server 比较重要,命令行参数都在这