-
Notifications
You must be signed in to change notification settings - Fork 7
/
form.newstep.lua
115 lines (87 loc) · 2.43 KB
/
form.newstep.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
local FORMNAME = "mission_block_newstep"
local function get_mission_steps_for_player(player)
local list = {}
for i,spec in ipairs(missions.steps) do
local allowed = true
-- check privs
if spec.privs and not minetest.check_player_privs(player:get_player_name(), spec.privs) then
allowed = false
end
if allowed then
table.insert(list, spec)
end
end
return list
end
missions.form.newstep = function(pos, node, player)
local steps = get_mission_steps_for_player(player)
local list = ""
for i,spec in ipairs(steps) do
list = list .. minetest.formspec_escape(spec.name)
if i < #steps then
-- not end of list
list = list .. ","
end
end
local formspec = "size[8,10;]" ..
"label[0,0;New step]" ..
"textlist[0,1;8,7;steptype;" .. list .. "]" ..
"button[0,9;8,1;add;Add]" ..
missions.FORMBG
minetest.show_formspec(player:get_player_name(),
FORMNAME .. ";" .. minetest.pos_to_string(pos),
formspec
)
end
local selected_newstep_index = {} -- playername -> index
minetest.register_on_player_receive_fields(function(player, formname, fields)
local parts = formname:split(";")
local name = parts[1]
if name ~= FORMNAME then
return
end
local pos = minetest.string_to_pos(parts[2])
local node = minetest.get_node(pos)
if not missions.check_owner(pos, player) then
return
end
local player_name = player:get_player_name()
if fields.steptype then
parts = fields.steptype:split(":")
local command = parts[1]
local selected_step = tonumber(parts[2])
if "CHG" == command -- changed selection
or "DCL" == command -- double clicked
then
selected_newstep_index[player_name] = selected_step
if "DCL" == command then fields.add = true end
end
end
if fields.add then
local steps = get_mission_steps_for_player(player)
local index = selected_newstep_index[player_name]
local spec = steps[index]
if not spec then
return
end
-- check privs
if spec.privs and not minetest.check_player_privs(player_name, spec.privs) then
minetest.chat_send_player(player_name, "Missing privs: " .. dump(spec.privs))
return
end
local stepdata = nil
if spec.create then
stepdata = spec.create()
end
local step = {
type = spec.type,
name = spec.name,
data = stepdata
}
steps = missions.get_steps(pos)
table.insert(steps, step)
missions.set_steps(pos, steps)
local stepnumber = #steps
missions.show_step_editor(pos, node, player, stepnumber, step, stepdata)
end
end)