-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.lua
More file actions
158 lines (139 loc) · 3.84 KB
/
api.lua
File metadata and controls
158 lines (139 loc) · 3.84 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
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
function jails:jail(playerName, jailName)
jailName = jailName or self.default
local jail = self.jails[jailName]
if not jail then
return false, "Jail does not exist."
end
if self:getJail(playerName) then
return false, "Already jailed."
end
if not self:playerExists(playerName) then
return false, "Player does not exist."
end
local pos, message
local player = minetest.get_player_by_name(playerName)
if player then
pos = player:getpos()
player:setpos(jail.pos)
if jails.announce then
minetest.chat_send_all(playerName.." has been jailed!")
else
minetest.chat_send_player(playerName, "You have been jailed.")
end
else
message = "That player is not online right now."
.." They will be jailed when they next connect."
end
jail.captives[playerName] = {
privs = minetest.get_player_privs(playerName),
pos = pos,
}
minetest.set_player_privs(playerName, {})
local ok, msg = self:save()
if not ok then return ok, msg end
return true, message
end
function jails:unjail(playerName)
for name, jail in pairs(self.jails) do
local playerData = jail.captives[playerName]
if playerData then
self:release(playerName, playerData)
jail.captives[playerName] = nil
return self:save()
end
end
return false, "Player not jailed."
end
function jails:getJail(playerName)
for jailName, jail in pairs(self.jails) do
if jail.captives[playerName] then
return jailName, jail
end
end
end
function jails:add(jailName, pos)
self.jails[jailName] = {
pos = pos,
captives = {},
}
return self:save()
end
function jails:remove(jailName, newJailName)
jailName = jailName or self.default
local jail = self.jails[jailName]
if not jail then
return false, "Jail does not exist."
end
local newJail
if newJailName then
if newJailName == jailName then
return false, "Cannot replace a jail with itself."
end
newJail = self.jails[newJailName]
if not newJail then
return false, "Jail to transfer to does not exist."
end
for playerName, playerData in pairs(jail.captives) do
newJail.captives[playerName] = playerData
local player = minetest.get_player_by_name(playerName)
if player then
player:setpos(newJail.pos)
end
end
else
for playerName, playerData in pairs(jail.captives) do
self:release(playerName, playerData)
end
end
self.jails[jailName] = nil
return self:save()
end
local fallbackSpawn = {x=0, y=8, z=0}
function jails:getSpawnPos(oldCaptivePos)
return oldCaptivePos or minetest.setting_get_pos("static_spawnpoint") or fallbackSpawn
end
function jails:save()
local dataStr = minetest.serialize(self.jails)
if not dataStr then
minetest.log("error", "[jails] Failed to serialize jail data!")
return false, "Serialization failed!"
end
local file, err = io.open(self.filename, "w")
if err then
minetest.log("error", "[jails] Failed to open jail file for saving!")
return false, err
end
file:write(dataStr)
file:close()
return true
end
function jails:load()
local file, err = io.open(self.filename, "r")
if err then return false, err end
local str = file:read("*a")
file:close()
if str == "" then return false, "Jail file is empty!" end
local jails = minetest.deserialize(str)
if not jails then return false, "Failed to deserialize jail data!" end
self.jails = jails
return true
end
--------------
-- Internal --
--------------
function jails:playerExists(playerName)
return (minetest.builtin_auth_handler or minetest.auth_handler)
.get_auth(playerName) and true or false
end
function jails:release(playerName, playerData)
local player = minetest.get_player_by_name(playerName)
if player then
player:setpos(self:getSpawnPos(playerData.pos))
end
minetest.set_player_privs(playerName, playerData.privs)
if self.announce then
minetest.chat_send_all(playerName.." has been freed from jail!")
else
minetest.chat_send_player(playerName, "You have been freed from jail.")
end
end