missions/form.newstep.lua

97 lines
2.0 KiB
Lua
Raw Normal View History

2018-07-22 21:35:26 +02:00
local FORMNAME = "mission_block_newstep"
missions.form.newstep = function(pos, node, player)
2018-07-23 10:16:47 +02:00
local step_buttons = ""
local offset = 1
2018-07-26 08:18:57 +02:00
local list = ""
2018-07-23 10:16:47 +02:00
for i,spec in ipairs(missions.steps) do
2018-07-26 08:18:57 +02:00
2018-08-04 19:50:43 +02:00
local allowed = true
-- check privs
if spec.privs and not minetest.check_player_privs(player:get_player_name(), spec.privs) then
allowed = false
-- continue?
end
if allowed then
list = list .. minetest.formspec_escape(spec.name)
if i < #missions.steps then
-- not end of list
list = list .. ","
end
2018-07-26 08:18:57 +02:00
end
2018-07-23 10:16:47 +02:00
end
2018-07-24 17:47:44 +02:00
local formspec = "size[8,10;]" ..
2018-07-22 21:35:26 +02:00
"label[0,0;New step]" ..
2018-07-26 08:18:57 +02:00
"textlist[0,1;8,7;steptype;" .. list .. "]" ..
2018-08-23 09:29:03 +02:00
"button[0,9;8,1;add;Add]"
2018-07-22 21:35:26 +02:00
minetest.show_formspec(player:get_player_name(),
FORMNAME .. ";" .. minetest.pos_to_string(pos),
formspec
)
end
2018-07-26 08:18:57 +02:00
local selected_newstep_index = {} -- playername -> index
2018-07-22 21:35:26 +02:00
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)
2018-07-24 14:54:59 +02:00
if not missions.check_owner(pos, player) then
return
end
2018-07-23 09:24:50 +02:00
2018-07-26 08:18:57 +02:00
if fields.steptype then
parts = fields.steptype:split(":")
if parts[1] == "CHG" then
local selected_step = tonumber(parts[2])
selected_newstep_index[player:get_player_name()] = selected_step
end
end
if fields.add then
local index = selected_newstep_index[player:get_player_name()]
local spec = missions.steps[index]
2018-07-24 15:04:47 +02:00
2018-07-26 08:18:57 +02:00
-- check privs
if spec.privs and not minetest.check_player_privs(player:get_player_name(), spec.privs) then
minetest.chat_send_player(player:get_player_name(), "Missing privs: " .. dump(spec.privs))
return
end
2018-07-24 15:04:47 +02:00
2018-07-26 08:18:57 +02:00
local stepdata = spec.create()
local step = {
type = spec.type,
name = spec.name,
data = stepdata
}
2018-07-23 09:24:50 +02:00
2018-07-26 08:18:57 +02:00
local steps = missions.get_steps(pos)
table.insert(steps, step)
2018-07-23 09:24:50 +02:00
2018-07-26 08:18:57 +02:00
missions.set_steps(pos, steps)
local stepnumber = #steps
2018-07-23 10:16:47 +02:00
2018-08-23 09:29:03 +02:00
missions.show_step_editor(pos, node, player, stepnumber, step, stepdata)
2018-07-22 21:35:26 +02:00
end
end)
2018-07-23 10:16:47 +02:00