2013-12-10 07:58:53 +01:00
worldedit = worldedit or { }
--[[
Example :
worldedit.register_gui_function ( " worldedit_gui_hollow_cylinder " , {
name = " Make Hollow Cylinder " ,
privs = { worldedit = true } ,
get_formspec = function ( name ) return " some formspec here " end ,
on_select = function ( name ) print ( name .. " clicked the button! " ) end ,
} )
Use ` nil ` for the ` options ` parameter to unregister the function associated with the given identifier .
Use ` nil ` for the ` get_formspec ` field to denote that the function does not have its own screen .
Use ` nil ` for the ` privs ` field to denote that no special privileges are required to use the function .
If the identifier is already registered to another function , it will be replaced by the new one .
2013-12-21 01:50:13 +01:00
The ` on_select ` function must not call ` worldedit.show_page `
2013-12-10 07:58:53 +01:00
] ]
2013-12-10 23:07:10 +01:00
worldedit.pages = { } --mapping of identifiers to options
2013-12-10 07:58:53 +01:00
local identifiers = { } --ordered list of identifiers
worldedit.register_gui_function = function ( identifier , options )
2013-12-10 23:07:10 +01:00
worldedit.pages [ identifier ] = options
2013-12-10 07:58:53 +01:00
table.insert ( identifiers , identifier )
end
2013-12-10 23:07:10 +01:00
--[[
Example :
worldedit.register_gui_handler ( " worldedit_gui_hollow_cylinder " , function ( name , fields )
print ( minetest.serialize ( fields ) )
end )
] ]
worldedit.register_gui_handler = function ( identifier , handler )
2014-07-11 02:17:12 +02:00
local enabled = true
2013-12-10 23:07:10 +01:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2014-07-11 02:17:12 +02:00
if not enabled then return false end
enabled = false
minetest.after ( 0.2 , function ( ) enabled = true end )
2013-12-10 23:07:10 +01:00
local name = player : get_player_name ( )
2014-07-11 02:17:12 +02:00
2013-12-10 23:07:10 +01:00
--ensure the player has permission to perform the action
local entry = worldedit.pages [ identifier ]
if entry and minetest.check_player_privs ( name , entry.privs or { } ) then
return handler ( name , fields )
end
return false
end )
end
2013-12-12 21:34:05 +01:00
worldedit.get_formspec_header = function ( identifier )
local entry = worldedit.pages [ identifier ] or { }
return " button[0,0;2,0.5;worldedit_gui;Back] " ..
string.format ( " label[2,0;WorldEdit GUI > %s] " , entry.name or " " )
end
2013-12-10 07:58:53 +01:00
local get_formspec = function ( name , identifier )
2013-12-10 23:07:10 +01:00
if worldedit.pages [ identifier ] then
return worldedit.pages [ identifier ] . get_formspec ( name )
2013-12-10 07:58:53 +01:00
end
2013-12-12 21:34:05 +01:00
return worldedit.pages [ " worldedit_gui " ] . get_formspec ( name ) --default to showing main page if an unknown page is given
2013-12-10 07:58:53 +01:00
end
2013-12-30 20:27:47 +01:00
--implement worldedit.show_page(name, page) in different ways depending on the available APIs
if unified_inventory then --unified inventory installed
2013-12-21 01:50:13 +01:00
local old_func = worldedit.register_gui_function
worldedit.register_gui_function = function ( identifier , options )
old_func ( identifier , options )
2013-12-21 06:06:32 +01:00
unified_inventory.register_page ( identifier , { get_formspec = function ( player ) return { formspec = options.get_formspec ( player : get_player_name ( ) ) } end } )
2013-12-21 01:50:13 +01:00
end
unified_inventory.register_button ( " worldedit_gui " , {
type = " image " ,
image = " inventory_plus_worldedit_gui.png " ,
} )
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2013-12-21 06:06:32 +01:00
local name = player : get_player_name ( )
2013-12-22 19:46:59 +01:00
if fields.worldedit_gui then --main page
worldedit.show_page ( name , " worldedit_gui " )
return true
elseif fields.worldedit_gui_exit then --return to original page
2014-01-19 06:12:34 +01:00
local player = minetest.get_player_by_name ( name )
if player then
unified_inventory.set_inventory_formspec ( player , " craft " )
end
2013-12-21 01:50:13 +01:00
return true
end
return false
end )
worldedit.show_page = function ( name , page )
2014-01-19 06:12:34 +01:00
local player = minetest.get_player_by_name ( name )
if player then
player : set_inventory_formspec ( get_formspec ( name , page ) )
end
2013-12-21 01:50:13 +01:00
end
2013-12-30 20:27:47 +01:00
elseif inventory_plus then --inventory++ installed
2013-12-10 07:58:53 +01:00
minetest.register_on_joinplayer ( function ( player )
2015-02-16 22:55:09 +01:00
local can_worldedit = minetest.check_player_privs ( player : get_player_name ( ) , { worldedit = true } )
if can_worldedit then
inventory_plus.register_button ( player , " worldedit_gui " , " WorldEdit " )
end
2013-12-10 07:58:53 +01:00
end )
2013-12-21 01:50:13 +01:00
--show the form when the button is pressed and hide it when done
local gui_player_formspecs = { }
2013-12-10 07:58:53 +01:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2013-12-21 01:50:13 +01:00
local name = player : get_player_name ( )
2013-12-10 07:58:53 +01:00
if fields.worldedit_gui then --main page
2013-12-21 01:50:13 +01:00
gui_player_formspecs [ name ] = player : get_inventory_formspec ( )
worldedit.show_page ( name , " worldedit_gui " )
return true
elseif fields.worldedit_gui_exit then --return to original page
if gui_player_formspecs [ name ] then
inventory_plus.set_inventory_formspec ( player , gui_player_formspecs [ name ] )
end
2013-12-10 07:58:53 +01:00
return true
end
return false
end )
worldedit.show_page = function ( name , page )
2014-01-19 06:12:34 +01:00
local player = minetest.get_player_by_name ( name )
if player then
inventory_plus.set_inventory_formspec ( player , get_formspec ( name , page ) )
end
2013-12-10 07:58:53 +01:00
end
2013-12-30 20:27:47 +01:00
else --fallback button
local player_formspecs = { }
local update_main_formspec = function ( name )
local formspec = player_formspecs [ name ]
if not formspec then
return
end
local player = minetest.get_player_by_name ( name )
2014-01-19 06:12:34 +01:00
if not player then --this is in case the player signs off while the media is loading
return
end
2014-01-03 05:17:25 +01:00
if ( minetest.check_player_privs ( name , { creative = true } ) or minetest.setting_getbool ( " creative_mode " ) ) and creative_inventory then --creative_inventory is active, add button to modified formspec
2014-01-03 00:03:52 +01:00
formspec = player : get_inventory_formspec ( ) .. " image_button[6,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;] "
2013-12-30 20:27:47 +01:00
else
formspec = formspec .. " image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;] "
end
player : set_inventory_formspec ( formspec )
end
minetest.register_on_joinplayer ( function ( player )
local name = player : get_player_name ( )
minetest.after ( 1 , function ( )
2014-01-19 06:12:34 +01:00
if minetest.get_player_by_name ( name ) then --ensure the player is still signed in
player_formspecs [ name ] = player : get_inventory_formspec ( )
minetest.after ( 0.01 , function ( )
update_main_formspec ( name )
end )
end
2013-12-30 20:27:47 +01:00
end )
end )
minetest.register_on_leaveplayer ( function ( player )
player_formspecs [ player : get_player_name ( ) ] = nil
end )
2014-01-03 00:17:28 +01:00
local gui_player_formspecs = { }
2013-12-30 20:27:47 +01:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
local name = player : get_player_name ( )
if fields.worldedit_gui then --main page
2014-01-03 00:17:28 +01:00
gui_player_formspecs [ name ] = player : get_inventory_formspec ( )
2013-12-30 20:27:47 +01:00
worldedit.show_page ( name , " worldedit_gui " )
return true
elseif fields.worldedit_gui_exit then --return to original page
2014-01-03 00:17:28 +01:00
if gui_player_formspecs [ name ] then
player : set_inventory_formspec ( gui_player_formspecs [ name ] )
end
2013-12-30 20:27:47 +01:00
return true
else --deal with creative_inventory setting the formspec on every single message
minetest.after ( 0.01 , function ( )
update_main_formspec ( name )
end )
return false --continue processing in creative inventory
end
end )
2013-12-21 01:50:13 +01:00
worldedit.show_page = function ( name , page )
2013-12-30 20:27:47 +01:00
local player = minetest.get_player_by_name ( name )
2014-01-19 06:12:34 +01:00
if player then
player : set_inventory_formspec ( get_formspec ( name , page ) )
end
2013-12-21 01:50:13 +01:00
end
2013-12-10 07:58:53 +01:00
end
worldedit.register_gui_function ( " worldedit_gui " , {
name = " WorldEdit GUI " ,
get_formspec = function ( name )
2013-12-24 20:07:42 +01:00
--create a form with all the buttons arranged in a grid
2013-12-10 07:58:53 +01:00
local buttons , x , y , index = { } , 0 , 1 , 0
local width , height = 3 , 0.8
local columns = 5
for i , identifier in pairs ( identifiers ) do
if identifier ~= " worldedit_gui " then
2013-12-10 23:07:10 +01:00
local entry = worldedit.pages [ identifier ]
2013-12-10 07:58:53 +01:00
table.insert ( buttons , string.format ( ( entry.get_formspec and " button " or " button_exit " ) ..
" [%g,%g;%g,%g;%s;%s] " , x , y , width , height , identifier , minetest.formspec_escape ( entry.name ) ) )
index , x = index + 1 , x + width
if index == columns then --row is full
x , y = 0 , y + height
index = 0
end
end
end
2013-12-17 00:55:56 +01:00
if index == 0 then --empty row
y = y - height
end
2013-12-12 21:34:05 +01:00
return string.format ( " size[%g,%g] " , math.max ( columns * width , 5 ) , math.max ( y + 0.5 , 3 ) ) ..
2013-12-21 01:50:13 +01:00
" button[0,0;2,0.5;worldedit_gui_exit;Back] " ..
2013-12-10 07:58:53 +01:00
" label[2,0;WorldEdit GUI] " ..
table.concat ( buttons )
end ,
} )
2013-12-10 23:07:10 +01:00
worldedit.register_gui_handler ( " worldedit_gui " , function ( name , fields )
2013-12-21 01:50:13 +01:00
for identifier , entry in pairs ( worldedit.pages ) do --check for WorldEdit GUI main formspec button selection
if fields [ identifier ] and identifier ~= " worldedit_gui " then
2013-12-10 23:07:10 +01:00
--ensure player has permission to perform action
2013-12-12 21:34:05 +01:00
local has_privs , missing_privs = minetest.check_player_privs ( name , entry.privs or { } )
if not has_privs then
worldedit.player_notify ( name , " you are not allowed to use this function (missing privileges: " .. table.concat ( missing_privs , " , " ) .. " ) " )
2013-12-10 23:07:10 +01:00
return false
end
if entry.on_select then
entry.on_select ( name )
end
if entry.get_formspec then
worldedit.show_page ( name , identifier )
end
return true
end
end
return false
end )
2014-01-03 05:17:25 +01:00
dofile ( minetest.get_modpath ( minetest.get_current_modname ( ) ) .. " /functionality.lua " )