-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforceloads.lua
87 lines (75 loc) · 2.31 KB
/
forceloads.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
local forceloads_file = minetest.get_worldpath().."/build_battle_forceloads.ser"
local forceloads_data = {}
local function fl_save()
local serdata = minetest.serialize(forceloads_data)
if not serdata then
return
end
local file, err = io.open(forceloads_file, "w")
if err then
return err
end
file:write(serdata)
file:close()
end
local function fl_load()
local file, err = io.open(forceloads_file, "r")
if err then
minetest.log("info", "[build_battle] could not load forceloads records")
return
end
forceloads_data = minetest.deserialize(file:read("*a"))
file:close()
end
bbattle.register_forceload = function(pos)
minetest.forceload_block(pos)
forceloads_data[minetest.pos_to_string(pos)] = 1
fl_save()
end
bbattle.unregister_forceload = function(pos)
minetest.forceload_free_block(pos)
forceloads_data[minetest.pos_to_string(pos)] = nil
fl_save()
end
-- Cleanup tool
local function clear_all_nonmarkers(playername)
for pos_s,_ in pairs(forceloads_data) do
local pos = minetest.string_to_pos(pos_s)
local node = minetest.get_node(pos)
if node.name == "build_battle:marker" or node.name == "ignore" then
minetest.chat_send_player(playername, "Ignoring "..node.name.." found at "..pos_s)
else
bbattle.unregister_forceload(pos)
minetest.chat_send_player(playername,"Cleared "..node.name.." at "..pos_s)
end
end
end
local function show_forceloads(playername)
minetest.chat_send_player(playername, minetest.serialize(forceloads_data) )
end
local function unload_forceload(playername, pos_s)
if pos_s == "" then
clear_all_nonmarkers(playername)
else
local pos = minetest.string_to_pos(pos_s)
if not pos then
minetest.chat_send_player(playername, "No pos obtained for "..pos_s)
return
end
bbattle.unregister_forceload(pos)
end
end
minetest.register_chatcommand("bbattle_showfl", {
description = "Show forceloaded positions",
func = function(playername, command)
show_forceloads(playername )
end
})
minetest.register_chatcommand("bbattle_clearfl", {
description = "Show forceloaded positions",
func = function(playername, command)
unload_forceload(playername, command)
end
})
-- Initialize
fl_load()