Repository navigation
Expand file tree
/
Copy pathlist sync.lua
More file actions
115 lines (94 loc) · 3.4 KB
/
Copy pathlist sync.lua
File metadata and controls
115 lines (94 loc) · 3.4 KB
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
-- 定义配置文件路径
local configFilePath = "./Resources/Server/ServerInfo/config.yml"
-- 存储配置的变量
local config = {
debug = true,
update_rate = 3
}
-- 加载并解析配置文件
local function loadConfig()
local file, err = io.open(configFilePath, "r")
if not file then
print("Failed to open config file: " .. configFilePath .. " Error: " .. err)
return
end
local content = file:read("*a")
file:close()
-- 解析 debug 配置
local debugMatch = string.match(content, "debug:%s*(%S+)")
if debugMatch then
config.debug = debugMatch == "true"
end
-- 解析 update rate 配置
local updateRateMatch = string.match(content, "update% rate:%s*(%d+)")
if updateRateMatch then
config.update_rate = tonumber(updateRateMatch)
end
print("\27[32m[ServerInfo]\27[0m ServerInfo plugin 1.6 loaded")
print("By Slime")
print("Debug mode: " .. (config.debug and "Enabled" or "Disabled"))
print("Update interval: " .. config.update_rate .. "s")
end
-- 从 path.yml 文件中加载玩家列表路径
local function loadPlayersFilePath()
local pathConfigFilePath = "./Resources/Server/ServerInfo/path.yml"
local file, err = io.open(pathConfigFilePath, "r")
if not file then
print("Failed to open path config file: " .. pathConfigFilePath .. " Error: " .. err)
return nil
end
local playersFilePath = nil
for line in file:lines() do
local pathMatch = string.match(line, "player% list% path:%s*(.+)")
if pathMatch then
playersFilePath = pathMatch
break
end
end
file:close()
if playersFilePath then
return playersFilePath
else
print("Failed to find player list path in path config file.")
return nil
end
end
-- 定义玩家列表文件路径(从 path.yml 文件中读取)
local playersFilePath = loadPlayersFilePath()
-- 更新玩家文件
function updatePlayersFile()
if config.debug then
print("\27[32m[ServerInfo]\27[0m Syncing player list txt...")
end
if not playersFilePath then
print("Player list file path not found.")
return
end
local file, err = io.open(playersFilePath, "w") -- 打开文件用于写入,如果文件不存在则创建
if not file then
print("Failed to open file: " .. playersFilePath .. " Error: " .. err)
return
end
local playerCount = MP.GetPlayerCount() -- 获取当前在线玩家数量
file:write("Online players: " .. playerCount .. "\n")
if playerCount == 0 then
file:write("No players online.\n")
else
local players = MP.GetPlayers() -- 获取所有玩家的 ID 和名称
for pid, name in pairs(players) do
file:write(name .. "\n")
end
end
file:close() -- 关闭文件
end
-- 在服务器启动时调用一次更新函数
function onInit_ServerInfo()
loadConfig()
updatePlayersFile()
-- 注册一个自定义事件 "UpdatePlayersEvent",并绑定到 updatePlayersFile
MP.RegisterEvent("UpdatePlayersEvent", "updatePlayersFile")
-- 创建一个定时器,根据配置的 update_rate 设置间隔时间(单位:毫秒)
MP.CreateEventTimer("UpdatePlayersEvent", config.update_rate * 1000)
end
-- 注册服务器初始化事件
MP.RegisterEvent("onInit", "onInit_ServerInfo")