~jadedctrl/gem-xwx-moe

~jadedctrl/gem-xwx-moe/gemujo_ludo/mods.niaj/fasado/formspecs/samples.lua
 ..
0 -------------------------------------------------------------------------------------------
1 -- How to try this example:
2 -- 1) Move this file into a new "afs_test" directory under mods and rename it "init.lua".
3 -- 2) Create a "depends.txt" file in the new directory with the following lines of text:
4 -- nyancat
5 -- formspecs
6 -- 3) Launch your Minetest server and enable the "afs_test" mod. Then, login as usual!
7 -------------------------------------------------------------------------------------------
8
9 minetest.register_privilege( "uptime", "View the uptime of the server interactively" )
10
11 local get_nyancat_formspec = function( meta )
12 local uptime = minetest.get_server_uptime( )
13 local formspec = "size[4,3]"
14 .. default.gui_bg_img
15 .. string.format( "label[0.5,0.5;%s %0.1f %s]",
16 minetest.colorize( "#FFFF00", "Server Uptime:" ),
17 meta.is_minutes == true and uptime / 60 or uptime,
18 meta.is_minutes == true and "mins" or "secs"
19 )
20 .. "checkbox[0.5,1;is_minutes;Show Minutes;" .. tostring( meta.is_minutes ) .. "]"
21 .. "button[0.5,2;2.5,1;update;Refresh]"
22 .. "hidden[view_count;1;number]"
23 .. "hidden[view_limit;10;number]" -- limit the number of refreshes!
24 return formspec
25 end
26
27 minetest.override_item( "nyancat:nyancat", {
28 description = "System Monitor",
29
30 on_open = function( meta, player )
31 local player_name = player:get_player_name( )
32
33 if meta.is_minutes == nil then meta.is_minutes = true end
34
35 if minetest.check_player_privs( player, "uptime" ) then
36 return get_nyancat_formspec( meta )
37 else
38 minetest.chat_send_player( player_name, "Your privileges are insufficient." )
39 end
40 end,
41 on_close = function( meta, player, fields )
42 local player_name = player:get_player_name( )
43
44 if not minetest.check_player_privs( player, "uptime" ) then return end
45
46 if fields.update then
47 if meta.view_count == meta.view_limit then
48 minetest.destroy_form( player_name )
49 minetest.chat_send_player( player_name, "You've exceeded the refresh limit." )
50 else
51 meta.view_count = meta.view_count + 1
52 minetest.update_form( player_name, get_nyancat_formspec( meta ) )
53 end
54
55 elseif fields.is_minutes then
56 meta.is_minutes = fields.is_minutes == "true"
57 minetest.update_form( player_name, get_nyancat_formspec( meta ) )
58 end
59 end
60 } )
61
62 minetest.register_chatcommand( "uptime", {
63 description = "View the uptime of the server interactively",
64 func = function( player_name, param )
65 local is_refresh = true
66
67 local get_formspec = function( )
68 local uptime = minetest.get_server_uptime( )
69
70 local formspec = "size[4,2]"
71 .. default.gui_bg_img
72 .. string.format( "label[0.5,0.5;%s %d secs]",
73 minetest.colorize( "#FFFF00", "Server Uptime:" ), uptime
74 )
75 .. "checkbox[0.5,1;is_refresh;Auto Refresh;" .. tostring( is_refresh ) .. "]"
76 return formspec
77 end
78 local on_close = function( meta, player, fields )
79 if fields.quit == minetest.FORMSPEC_SIGTIME then
80 minetest.update_form( player_name, get_formspec( ) )
81
82 elseif fields.is_refresh then
83 is_refresh = fields.is_refresh == "true"
84 if is_refresh == true then
85 minetest.get_form_timer( player_name ).start( 1 )
86 else
87 minetest.get_form_timer( player_name ).stop( )
88 end
89 end
90 end
91
92 minetest.create_form( nil, player_name, get_formspec( ), on_close )
93 minetest.get_form_timer( player_name ).start( 1 )
94 end
95 } )