-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
453 lines (418 loc) · 13.9 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
multispawndebug = false
local function save_data(sett, data, def_sp)
if sett ~= nil then
data_ser = minetest.serialize(data)
sett:set("data", data_ser)
sett:set("default", def_sp)
sett:write()
else
minetest.chat_send_all("Saving data failed")
end
end
local function build_id(sp)
--build of index
local si = {}
for _,v in pairs(sp) do
si[v.num] = v.id
end
return si
end
local function rebuild_id(sp, si)
--clearing of index
for v in pairs (si) do
si[v] = nil
end
--rebuild of index
for _,v in pairs(sp) do
si[v.num] = v.id
end
return si
end
local function load_data(sett, field)
local loaded = sett:get("data")
local def = sett:get("default")
if loaded ~= nil then
local data = minetest.deserialize(loaded)
local def = sett:get("default")
else
local data = {
origin = {num = 0, coords = {x = 0, y = 8.5, z = 0}, name = "Origin", id = "origin"},
}
local def = "origin"
end
local return_table = {}
table.insert(return_table, {spawns=data, default=def})
return return_table
end
local function get_nearest_id (playerref, spawnlist)
local player_coords = playerref:getpos()
local minnumber = nil
local spawn = ""
local temp_coords = nil
for _,v in pairs(spawnlist) do
temp_coords = v.coords
distance = tonumber(vector.distance(temp_coords, player_coords))
if minnumber == nil then
minnumber = tonumber(distance)
spawn = v.id
elseif distance < minnumber then
minnumber = tonumber(distance)
spawn = v.id
end
end
return spawn
end
local function print_r(tab,com)
if multispawndebug == true then
print("DEBUG: "..com)
table.foreach(tab, print)
print("-----")
return true
else
return false
end
end
local function debug(var, com)
if multispawndebug == true then
print("DEBUG: "..com)
print(var)
minetest.chat_send_all("DEBUG: "..var.."// "..com)
print("-----")
return true
else
return false
end
end
local config_file = minetest.get_worldpath().."/spawn.conf"
--in case of not existant config file, it
--will create it
local file_desc = io.open(config_file, "a")
file_desc:close()
--create config instance
local config = Settings(config_file)
local data
local default
local spawns = {}
local default_spawn = {}
data = config:get("data")
if data ~= nil then
spawns = minetest.deserialize(data)
default = config:get("default")
if default ~= nil then
default_spawn = spawns[default]
end
else
spawns = {
origin = {num = 0, coords = {x = 0, y = 0, z = 0}, name = "Origin", id = "origin"},
}
default_spawn = spawns.origin
end
save_data(config, spawns, default_spawn.id)
spawn_id = build_id(spawns)
minetest.register_privilege("spawn_admin", {"Allowing to create, modify and delete spawnpoints", give_to_singleplayer = false})
-- Make list of spawns by its numbers
local spawn_id = {}
for _,v in pairs(spawns) do
spawn_id[v.num] = v.id
end
minetest.register_chatcommand("spawn", {
params = "[spawn number|spawnid]",
description = "Spawns player to nearest or specified spawn",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
if param == "" then
local nearestid = get_nearest_id(player, spawns)
player:setpos(spawns[nearestid].coords)
minetest.chat_send_player(name, "You are now at "..spawns[nearestid].name);
elseif type(spawns[param]) == "table" then
player:setpos(spawns[param].coords)
minetest.chat_send_player(name, "You are now at "..spawns[param].name);
elseif type(spawns[spawn_id[tonumber(param)]]) == "table" then
player:setpos(spawns[spawn_id[tonumber(param)]].coords)
minetest.chat_send_player(name, "You are now at "..spawns[spawn_id[tonumber(param)]].name);
else
minetest.chat_send_player(name, "I don't know where is that place. Sorry.");
end
end,
})
minetest.register_chatcommand("spawnset", {
params = "",
privs = {spawn_admin=true},
description = "Set new spawn point.",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
local formspec = "size[8,6]"
formspec = formspec.."label[0,0;New spawn setting]"
formspec = formspec.."field[0.2,1;7,1;sname;Spawn name;]"
formspec = formspec.."field[0.2,2;5,1;sid;Spawn ID;]"
formspec = formspec.."field[5.2,2;2,1;snum;Spawn number;]"
formspec = formspec.."field[0.2,3;1,1;scoordx;X coord;]"
formspec = formspec.."field[1.2,3;1,1;scoordy;Y coord;]"
formspec = formspec.."field[2.2,3;1,1;scoordz;Z coord;]"
formspec = formspec.."label[0,8;]"
formspec = formspec.."button_exit[0,5;8,1;ssubmit;Create spawn]"
minetest.show_formspec(name, "multispawn:spawnset", formspec)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "multispawn:spawnset" and fields.quit ~= "true" then
local x, y, z, sname, sid, snum, err
err = ""
x = tonumber(fields.scoordx) or 0
y = tonumber(fields.scoordy) or 0
z = tonumber(fields.scoordz) or 0
sname = fields.sname
sid = string.lower(fields.sid)
snum = tonumber(fields.snum) or 1
if type(spawns[sid]) == "table" then
err = "Spawn ID already exists"
end
if err == "" then
for _,v in pairs(spawns) do
if v.num == snum then
err = "This spawn number already exists"
break
end
end
end
if err ~= "" then
minetest.chat_send_player(name, err);
local formspec = "size[8,6]"
formspec = formspec.."label[0,0;New spawn setting]"
formspec = formspec.."field[0.2,1;7,1;sname;Spawn name;"..sname.."]"
formspec = formspec.."field[0.2,2;5,1;sid;Spawn ID;"..sid.."]"
formspec = formspec.."field[5.2,2;2,1;snum;Spawn number;"..snum.."]"
formspec = formspec.."field[0.2,3;1,1;scoordx;X coord;"..tostring(x).."]"
formspec = formspec.."field[1.2,3;1,1;scoordy;Y coord;"..tostring(y).."]"
formspec = formspec.."field[2.2,3;1,1;scoordz;Z coord;"..tostring(z).."]"
formspec = formspec.."label[0,8;"..err.."]"
formspec = formspec.."button_exit[0,5;8,1;ssubmit;Create spawn]"
minetest.show_formspec(name, "multispawn:spawnset", formspec)
end
local joined_data = {}
local new_coords = {}
joined_data.name = sname
joined_data.num = snum
joined_data.id = sid
new_coords = {x=tostring(x), y=tostring(y), z=tostring(z)}
joined_data.coords = new_coords
spawns[sid] = {}
spawns[sid] = joined_data
save_data(config, spawns, default_spawn.id)
minetest.chat_send_player(name, "Spawn point "..joined_data.name.." was succesfully created.");
spawn_id = rebuild_id(spawns, spawn_id)
return true
else
-- esc or enter pressed
return false
end
end)
end,
})
minetest.register_chatcommand("spawnedit", {
params = "<spawn number|spawnid>",
privs = {spawn_admin=true},
description = "Edit spawn point.",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
local editedspawn = {}
local tempspawn = ""
if param == "" then
minetest.chat_send_player(name, "You must provide spawnid or spawn number");
return
elseif type(spawns[param]) == "table" then
editedspawn = spawns[param]
tempspawn = spawns[param].id
elseif type(spawns[spawn_id[tonumber(param)]]) == "table" then
editedspawn = spawns[spawn_id[tonumber(param)]]
tempspawn = spawns[spawn_id[tonumber(param)]].id
else
minetest.chat_send_player(name, "I don't know such spawn point. Sorry.");
return
end
local formspec = "size[8,6]"
formspec = formspec.."label[0,0;Spawn editing]"
formspec = formspec.."field[0.2,1;7,1;sname;Spawn name;"..tostring(editedspawn.name).."]"
formspec = formspec.."field[0.2,2;2,1;snum;Spawn number;"..tonumber(editedspawn.num).."]"
formspec = formspec.."field[0.2,3;1,1;scoordx;X coord;"..tonumber(editedspawn.coords.x).."]"
formspec = formspec.."field[1.2,3;1,1;scoordy;Y coord;"..tonumber(editedspawn.coords.y).."]"
formspec = formspec.."field[2.2,3;1,1;scoordz;Z coord;"..tonumber(editedspawn.coords.z).."]"
formspec = formspec.."label[0,8;]"
formspec = formspec.."button_exit[0,5;8,1;ssubmit;Confirm changes]"
minetest.show_formspec(name, "multispawn:spawnedit", formspec)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "multispawn:spawnedit" and fields.quit ~= "true" then
local x, y, z, sname, snum, err
err = ""
x = tonumber(fields.scoordx) or 0
y = tonumber(fields.scoordy) or 0
z = tonumber(fields.scoordz) or 0
sname = fields.sname
snum = tonumber(fields.snum) or 1
if err == "" then
for _,v in pairs(spawns) do
if v.num == snum and tempspawn ~= spawn_id[snum] then
err = "This spawn number already used by "..spawn_id[snum]
break
end
end
end
if err ~= "" then
minetest.chat_send_player(name, err);
local formspec = "size[8,6]"
formspec = formspec.."label[0,0;Spawn editing]"
formspec = formspec.."field[0.2,1;7,1;sname;Spawn name;"..sname.."]"
formspec = formspec.."field[0.2,2;2,1;snum;Spawn number;"..snum.."]"
formspec = formspec.."field[0.2,3;1,1;scoordx;X coord;"..tostring(x).."]"
formspec = formspec.."field[1.2,3;1,1;scoordy;Y coord;"..tostring(y).."]"
formspec = formspec.."field[2.2,3;1,1;scoordz;Z coord;"..tostring(z).."]"
formspec = formspec.."label[0,7;"..err.."]"
formspec = formspec.."button_exit[0,5;8,1;ssubmit;Confirm changes]"
minetest.show_formspec(name, "multispawn:spawnedit", formspec)
end
local joined_data = {}
local new_coords = {}
joined_data.name = sname
joined_data.num = snum
joined_data.id = tempspawn
new_coords = {x=tostring(x), y=tostring(y), z=tostring(z)}
joined_data.coords = new_coords
spawns[tempspawn] = joined_data
save_data(config, spawns, default_spawn.id)
spawn_id = rebuild_id(spawns, spawn_id)
minetest.chat_send_player(name, "Spawn point "..joined_data.name.." was succesfully edited.");
return true
else
-- esc or enter pressed
return false
end
end)
end,
})
minetest.register_chatcommand("spawnlist", {
params = "",
description = "List all possible spawnpoints.",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
minetest.chat_send_player(name, "--- Start of spawn list ---");
for _,v in pairs(spawns) do
minetest.chat_send_player(name, v.num..") "..v.name.." ("..v.id..")");
end
minetest.chat_send_player(name, "--- End of spawn list ---");
end
})
minetest.register_chatcommand("spawnnear", {
params = "[name]",
description = "Show you name and distance of your or <names> nearest spawn",
func = function(name, param)
if param == "" then
local player = minetest.get_player_by_name(name)
if not player then
return
end
local nearestid = get_nearest_id(player, spawns)
minetest.chat_send_player(name, "Nearest spawn is "..spawns[nearestid].name..".");
else
local sparam = tostring(param)
local player = minetest.get_player_by_name(sparam)
if not player then
minetest.chat_send_player(name, sparam.." is not online.");
return
end
local nearestid = get_nearest_id(player, spawns)
print(sparam)
minetest.chat_send_player(name, sparam.."'s nearest spawn is "..spawns[nearestid].name..".");
end
end
})
minetest.register_chatcommand("spawndefault", {
params = "<spawn number|spawnid>",
privs = {spawn_admin=true},
description = "Allows change of default spawn.",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
-- Handling parameter
if param == "" then
minetest.chat_send_player(name, "You must provide spawnid or spawn number");
return
elseif type(spawns[param]) == "table" then
default_spawn = spawns[param]
minetest.chat_send_player(name, "Default spawn point was set to "..default_spawn.name..".");
save_data(config, spawns, default_spawn.id)
elseif type(spawns[spawn_id[tonumber(param)]]) == "table" then
default_spawn = spawns[spawn_id[tonumber(param)]]
minetest.chat_send_player(name, "Default spawn point was set to "..default_spawn.name..".");
save_data(config, spawns, default_spawn.id)
else
minetest.chat_send_player(name, "I don't know such spawn point. Sorry.");
return
end
end
})
minetest.register_chatcommand("spawnremove", {
params = "<spawn number|spawnid>",
privs = {spawn_admin=true},
description = "Allows to remove spawn.",
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return
end
-- Handling parameter
if param == "" then
minetest.chat_send_player(name, "You must provide spawnid or spawn number");
return
elseif type(spawns[param]) == "table" then
minetest.chat_send_player(name, "Spawn point "..param.." was succesfuly removed.");
spawns[param] = nil
spawn_id = rebuild_id(spawns, spawn_id)
if param == default_spawn.id then
local v
for v in pairs (spawns) do
default_spawn = spawns[v]
break
end
minetest.chat_send_player(name, "You removed default spawn point! Default spawn point was set to "..default_spawn.name..".");
end
save_data(config, spawns, default_spawn.id)
elseif type(spawns[spawn_id[tonumber(param)]]) == "table" then
param = spawn_id[tonumber(param)]
minetest.chat_send_player(name, "Spawn point "..spawns[param].name.." was succesfuly removed.");
spawns[param] = nil
spawn_id = rebuild_id(spawns, spawn_id)
if param == default_spawn.id then
local v
for v in pairs (spawns) do
default_spawn = spawns[v]
break
end
minetest.chat_send_player(name, "You removed default spawn point! Default spawn point was set to "..default_spawn.name..".");
end
save_data(config, spawns, default_spawn.id)
else
minetest.chat_send_player(name, "I don't know such spawn point. Sorry.");
return
end
end
})
minetest.register_on_newplayer(function(player)
player:setpos(default_spawn.coords)
return true
end)
minetest.register_on_respawnplayer(function(player, pos)
player:setpos(default_spawn.coords)
return true
end)