forked from octacian/spawnpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
352 lines (291 loc) · 8.14 KB
/
init.lua
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
-- spawnpoint/init.lua
spawnpoint = {}
local storage
local path = minetest.get_worldpath().."/spawnpoint.conf"
local data = Settings(path)
----------------------
--- RANDOM NUMBERS ---
----------------------
math.randomseed(os.time())
math.random(); math.random(); math.random()
local spawnoffsetamount = 2
if minetest.get_mod_storage then
storage = minetest.get_mod_storage()
end
-- [function] Log
function spawnpoint.log(content, log_type)
if not content then return false end
if log_type == nil then log_type = "action" end
minetest.log(log_type, "[SpawnPoint] "..content)
end
----------------------
----- GLOBALSTEP -----
----------------------
local moved = {}
local huds = {}
local pos = {}
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if pos[name] and spawnpoint.do_not_move then
if not moved[name] and not vector.equals(pos[name], player:getpos()) then
moved[name] = true
player:hud_remove(huds[name])
minetest.chat_send_player(name, "Teleportation interrupted! (Player moved)")
end
end
end
end)
----------------------
-- HELPER FUNCTIONS --
----------------------
-- [local function] Count table contents
local function count(t)
local count = 0
for _, i in pairs(t) do
count = count + 1
end
return count
end
-- [local function] Check if table is empty
local function is_empty(t)
if t.fields then
return count(t.fields) == 0
else
return count(t) == 0
end
end
-- [function] Load
function spawnpoint.load()
if data and not is_empty(data:to_table()) then
spawnpoint.time = tonumber(data:get("time"))
spawnpoint.do_not_move = data:get_bool("do_not_move")
local pos = data:get("pos")
if pos then
spawnpoint.pos = minetest.string_to_pos(pos)
end
if storage then
os.remove(path)
end
elseif storage and not is_empty(storage:to_table()) then
local pos = storage:get_string("pos")
if pos then
spawnpoint.pos = minetest.string_to_pos(pos)
end
local do_not_move = storage:get_string("do_not_move")
if do_not_move == "true" or do_not_move == true then
spawnpoint.do_not_move = true
else
spawnpoint.do_not_move = false
end
spawnpoint.time = storage:get_float("time")
else
local f = io.open(path, "r")
if f then
local res = f:read("*all"):split("\n", true)
spawnpoint.time = tonumber(res[1])
if res[2] == "true" or res[2] == true then
spawnpoint.do_not_move = true
else
spawnpoint.do_not_move = false
end
if res[3] then
spawnpoint.pos = minetest.string_to_pos(res[3])
end
f:close()
-- Clear file
os.remove(path)
end
end
end
-- [function] Save
function spawnpoint.save()
if storage then
storage:set_float("time", spawnpoint.time or 0)
storage:set_string("do_not_move", tostring(spawnpoint.do_not_move))
if spawnpoint.pos then
storage:set_string("pos", minetest.pos_to_string(spawnpoint.pos))
end
return true
elseif data then
data:set("time", tostring(spawnpoint.time))
data:set_bool("do_not_move", spawnpoint.do_not_move)
if spawnpoint.pos then
data:set("pos", minetest.pos_to_string(spawnpoint.pos))
end
data:write()
return true
end
end
-- [function] Set
function spawnpoint.set(pos)
if type(pos) == "string" then
pos = minetest.string_to_pos(pos)
end
if type(pos) == "table" then
spawnpoint.pos = pos
spawnpoint.save()
spawnpoint.log("Set spawnpoint to "..minetest.pos_to_string(pos))
end
end
-- [function] Bring
function spawnpoint.bring(player)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
if player and spawnpoint.pos then
local pos = spawnpoint.pos
-- Randomize Spawnpoint with offset
local x_offset = math.random(-spawnoffsetamount, spawnoffsetamount);
local z_offset = math.random(-spawnoffsetamount, spawnoffsetamount);
player:setpos({x=pos.x+x_offset, y=pos.y+0.5, z=pos.z+z_offset})
end
end
-- [function] Begin Countdown
function spawnpoint.begin(player, time)
if not time then
time = spawnpoint.time
end
if type(player) == string then
player = minetest.get_player_by_name(player)
end
local name = player:get_player_name()
if player and time and time ~= 0 then
local move = "Do not move!"
if spawnpoint.do_not_move ~= true then
move = ""
end
local seconds = "s"
if time < 2 then
seconds = ""
end
-- Send to chat
minetest.chat_send_player(name, "Teleportation will be complete in "..time..
" second"..seconds..". "..move)
-- Add initial HUD
huds[name] = player:hud_add({
hud_elem_type = "text",
text = "Teleportation Progress: "..time.." seconds remaining!",
position = {x = 0.5, y = 0.5},
number = 0xFFFFFF,
})
local hud = huds[name]
pos[name] = player:getpos()
moved[name] = false
-- Register update callbacks
for i = 1, time do
if i == time then
minetest.after(i, function()
if not moved[name] then
player:hud_remove(hud)
spawnpoint.bring(player)
-- Send to chat
minetest.chat_send_player(name, "Teleportation successful!")
-- Prevent further callbacks from globalstep
moved[name] = true
end
end)
else
minetest.after(i, function()
if not moved[name] then
player:hud_change(hud, "text", "Teleportation Progress: "..time - i.." seconds remaining!")
end
end)
end
end
elseif player then
minetest.chat_send_player(name, "Teleporting to spawn")
spawnpoint.bring(player)
end
end
-------------------
---- CALLBACKS ----
-------------------
spawnpoint.load()
-- [register] On Shutdown
minetest.register_on_shutdown(spawnpoint.save)
-- [register] On Respawn Player
minetest.register_on_respawnplayer(function(player)
spawnpoint.bring(player)
end)
-- [register] On New Player
minetest.register_on_newplayer(function(player)
spawnpoint.bring(player)
end)
-- [register priv] Spawn
minetest.register_privilege("spawn", "Ability to teleport to spawn at will with /spawn")
-- [register cmd] Set spawn
minetest.register_chatcommand("setspawn", {
description = "Set spawn",
privs = {server=true},
func = function(name, param)
local pos = minetest.get_player_by_name(name):getpos()
if param then
local ppos = minetest.string_to_pos(param)
if type(ppos) == "table" then
pos = ppos
end
end
pos = vector.round(pos)
spawnpoint.set(pos)
return true, "Set spawnpoint to "..minetest.pos_to_string(pos)
end,
})
-- [register cmd] Teleport to spawn
minetest.register_chatcommand("spawn", {
description = "Teleport to spawn",
privs = {spawn=true},
func = function(name, param)
local player = minetest.get_player_by_name(name)
if param ~= "" then
local pplayer = minetest.get_player_by_name(param)
if pplayer and minetest.check_player_privs(pplayer, {bring=true}) then
player = pplayer
else
return false, "Cannot teleport another player to spawn without bring privilege"
end
end
if not spawnpoint.pos then
return false, "No spawnpoint set!"
end
spawnpoint.begin(player)
end,
})
-- [register cmd] Manage spawnpoint
minetest.register_chatcommand("spawnpoint", {
description = "Get/Set SpawnPoint information",
func = function(name, param)
if not param or param == "" then
local pos = "Not set!"
if spawnpoint.pos then
pos = minetest.pos_to_string(spawnpoint.pos)
end
return true, "SpawnPoint Position: "..pos
elseif minetest.check_player_privs(minetest.get_player_by_name(name), {server=true}) then
local p = param:split(" ")
if p[1] == "time" then
local num = tonumber(p[2])
if not num then
return true, "SpawnPoint->time: "..dump(spawnpoint.time)
elseif num == spawnpoint.time then
return false, "Time already set to "..p[2].."!"
else
spawnpoint.time = num
spawnpoint.save()
spawnpoint.log("Set time to "..dump(num))
return true, "Set time to "..dump(num)
end
elseif p[1] == "do_not_move" then
local move = minetest.is_yes(p[2])
if move == nil or not p[2] then
return true, "SpawnPoint->do_not_move: "..dump(spawnpoint.do_not_move)
else
spawnpoint.do_not_move = move
spawnpoint.save()
spawnpoint.log("Set do_not_move to "..dump(move))
return true, "Set do_not_move to "..dump(move)
end
end
end
end,
})