jail/main_form.lua

93 lines
3.0 KiB
Lua

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by vinamin.
--- DateTime: 26.02.2024 13:32
---
local S = minetest.get_translator("jail")
local select_player_jail_id = ''
local select_player_name_to_jail = ''
local online_players_list = {}
local function get_formspec_main(name)
local form_list_persones = ""
local online_player = ''
for k, player in ipairs(minetest.get_connected_players()) do
online_player = online_player .. player:get_player_name() .. ","
table.insert(online_players_list, k, player:get_player_name())
end
for i = 1, #prisoners_list do
form_list_persones = form_list_persones .. i .. " " ..prisoners_list[i] ..","
end
local formspec = {
"size[13,11]",
"label[0.3,0.3;" .. S("Players online") .. "]",
"textlist[0.3,0.7;4.9,9.8;online;" .. online_player .. "]",
"label[7.9,0.3;" .. S("Prisoners") .. "]",
"textlist[7.9,0.7;4.8,9.8;jail_players;" .. form_list_persones .. "]",
"button[5.4,0.7;2.3,0.8;add;".. S("To jail") .. "]",
"button[5.4,1.8;2.3,0.8;release;".. S("To freedom") .. "]"
}
return table.concat(formspec, "")
end
minetest.register_chatcommand("jailgui", {
description = S("Graphical prisoner management shell"),
privs = { jail = true },
func = function(name)
minetest.show_formspec(name, "jail:formspec_main", get_formspec_main(name))
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
local event = ''
local pname = player:get_player_name()
if formname ~= "jail:formspec_main" then
return
end
if fields.add then
if select_player_name_to_jail == '' then return end
jail.add_jail(pname, select_player_name_to_jail)
minetest.show_formspec(pname, "jail:formspec_main", get_formspec_main(pname))
select_player_name_to_jail = ''
end
--TODO: choosing a player to put in jail
if fields.online then
event = minetest.explode_textlist_event(fields.online)
if event.type == 'CHG' then
select_player_name_to_jail = online_players_list[event.index]
minetest.chat_send_player(pname, S("Selected player:") .. " " .. select_player_name_to_jail)
end
end
--TODO: selecting a player to release
if fields.jail_players then
event = minetest.explode_textlist_event(fields.jail_players)
if event.type == 'CHG' then
select_player_jail_id = event.index
if type(array) ~= 'nil' then
minetest.chat_send_all(S("Prisoner selected") .." id: ".. select_player_jail_id ..
", name:" .. prisoners_list[select_player_jail_id])
end
end
end
if fields.release then
if select_player_jail_id == '' then return end
jail.release(pname, select_player_jail_id)
minetest.show_formspec(pname, "jail:formspec_main", get_formspec_main(pname))
select_player_jail_id = ''
end
end)