people/form.lua

149 lines
4.7 KiB
Lua

local dbg
if moddebug then dbg=moddebug.dbg("people") else dbg={v1=function() end,v2=function() end,v3=function() end} end
local lastformbyplayer = {}
minetest.register_on_player_receive_fields(function(sender, formname, fields)
if formname == "people:form" then
-- The main form...
if fields.quit then return end
dbg.v2("Received form fields "..dump(fields))
local playername = sender:get_player_name()
local entity = lastformbyplayer[playername]
if not entity then return end
if playername ~= entity.owner then return end
if fields.inventory then
local formspec = "size[8,9]"..
"list[detached:people_"..entity.name..";main;0,0;8,4;]"..
"list[current_player;main;0,5;8,4;]"
minetest.show_formspec(playername, "people:invform", formspec)
elseif fields.skin then
minetest.show_formspec(playername, "people:skinform", skins.formspec.main(playername))
elseif fields.program then
if string.sub(entity.fcode, 1, 1) ~= "@" then
entity.code = fields.code
else
entity.code = entity.fcode
end
-- Some things get reset when programming...
entity.gather = {items={}, nodes={}, topnodes={}, plant=nil,
animal = nil, -- name of animal to gather from
animal_wield = nil, -- when animal set, the item to wield
}
entity.action = nil
entity.wait = 0
local event = {type="program", programmer=playername}
local err = people.exec_event(entity, event)
if err then dbg.v1("Lua error "..err) end
elseif fields.preset then
if fields.preset == "Custom" then
if string.sub(entity.fcode, 1, 1) == "@" then
local f = string.sub(entity.fcode, 2)
entity.fcode = "HUH?"
for _, p in pairs(people.presets) do
if p.name == f then
entity.fcode = p.code
break
end
end
end
else
entity.fcode = "@"..fields.preset
end
people.show_form(entity, playername)
end
elseif formname == "people:invform" then
-- The inventory form...
-- Just a placeholder - doesn't need any handling currently
elseif formname == "people:skinform" then
local playername = sender:get_player_name()
local entity = lastformbyplayer[playername]
if not entity then return end
if playername ~= entity.owner then return end
if fields.main then
people.show_form(entity, playername)
return
end
for field, _ in pairs(fields) do
if string.sub(field,0,string.len("skins_set_")) == "skins_set_" then
local req = skins.list[tonumber(string.sub(field,string.len("skins_set_")+1))]
entity.props.textures = {req..".png"}
entity:update_props()
end
if string.sub(field,0,string.len("skins_page_")) == "skins_page_" then
skins.pages[playername] = tonumber(string.sub(field,string.len("skins_page_")+1))
minetest.show_formspec(playername, "people:skinform", skins.formspec.main(playername))
end
end
end
end)
--- Show the right-click form for a person
-- When editing code, this will read and operate on entity.fcode during
-- the editing process. Only when changes are committed will it copy
-- that back to entity.code.
-- @param entity The entity to get the formspec for
-- @param playername The name of the player requesting it
people.show_form = function(entity, playername)
lastformbyplayer[playername] = entity
local formspec
local message = "Hello, my name is "..entity.name
if playername == entity.owner then
local prelist = "Custom"
for _, p in ipairs(people.presets) do
if not p.priv or minetest.check_player_privs(playername, {server=true}) then
prelist = prelist..","..p.name
end
end
formspec = "size[10,8]"..
"label[0,0;"..message.."]"
local presetnum
if string.sub(entity.fcode, 1, 1) ~= "@" then
formspec = formspec.."textarea[0.2,0.6;10.2,6;code;;"..minetest.formspec_escape(entity.fcode).."]"
presetnum = 0
else
for i, p in ipairs(people.presets) do
if p.name == string.sub(entity.fcode, 2) then
presetnum = i
break
end
end
end
if not presetnum then
presetnum = 1
entity.fcode = "@Example"
end
formspec = formspec.."button[1.5,5;2.5,1;program;Program]"..
"button[4,5;2.5,1;inventory;Inventory]"..
"button[6.5,5;2.5,1;skin;Skin]"..
"label[0,7;Preset]"..
"dropdown[2,7;3;preset;"..prelist..";"..presetnum + 1 .."]"
else
formspec = "size[8,4]"
.."label[0,0;"..message.."]"
end
dbg.v3("Showing formspec to "..playername.." : "..formspec)
minetest.show_formspec(playername, "people:form", formspec)
end