mirror of
https://github.com/ExeVirus/formspec_editor.git
synced 2025-03-22 17:32:22 +00:00
globalstep based formspec updating
This commit is contained in:
parent
4d7f2970c1
commit
6de8e67842
1 changed files with 63 additions and 53 deletions
|
@ -10,7 +10,8 @@ minetest.register_alias("mapgen_water_source", "air")
|
||||||
--Variables
|
--Variables
|
||||||
local modpath = minetest.get_modpath("formspec_edit")
|
local modpath = minetest.get_modpath("formspec_edit")
|
||||||
local insecure_env = minetest.request_insecure_environment()
|
local insecure_env = minetest.request_insecure_environment()
|
||||||
local auto_update_time = 0.2 --seconds
|
local update_time = 0.2 --seconds
|
||||||
|
|
||||||
local error_formspec = [[
|
local error_formspec = [[
|
||||||
formspec_version[4]
|
formspec_version[4]
|
||||||
size[8,2]
|
size[8,2]
|
||||||
|
@ -19,11 +20,17 @@ label[0.375,0.5;Error:formspec.spec is either ]
|
||||||
label[0.375,1;non-existent,or empty]
|
label[0.375,1;non-existent,or empty]
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
--Crash if not singleplayer
|
||||||
|
--TODO: hide the 'Host server' checkbox in main menu then possible
|
||||||
|
if not minetest.is_singleplayer() then
|
||||||
|
error("[formspec_editor] This game doesn't work in multiplayer!")
|
||||||
|
end
|
||||||
|
|
||||||
--function declarations
|
--function declarations
|
||||||
local update_formspec = nil
|
local update_formspec = nil
|
||||||
local load_formspec = nil
|
local load_formspec = nil
|
||||||
local auto_update = nil
|
|
||||||
local turn_off_hud = nil
|
local turn_off_hud = nil
|
||||||
|
local set_sky
|
||||||
|
|
||||||
--Registrations
|
--Registrations
|
||||||
|
|
||||||
|
@ -32,9 +39,8 @@ local turn_off_hud = nil
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
minetest.register_on_joinplayer(
|
minetest.register_on_joinplayer(
|
||||||
function(player_ref,_)
|
function(player_ref,_)
|
||||||
auto_update(player_ref:get_player_name())
|
turn_off_hud(player_ref)
|
||||||
turn_off_hud(player_ref)
|
set_sky(player_ref)
|
||||||
set_sky(player_ref)
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
@ -42,78 +48,82 @@ end
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
minetest.register_on_player_receive_fields(
|
minetest.register_on_player_receive_fields(
|
||||||
function(player_ref, _, fields)
|
function(player_ref, _, fields)
|
||||||
if(fields.quit) then
|
if(fields.quit) then
|
||||||
minetest.request_shutdown()
|
minetest.request_shutdown()
|
||||||
end
|
end
|
||||||
update_formspec(player_ref:get_player_name())
|
update_formspec(player_ref:get_player_name())
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
--function definitions
|
--function definitions
|
||||||
|
|
||||||
-----------------------------------
|
|
||||||
--auto_update()
|
|
||||||
-----------------------------------
|
|
||||||
auto_update = function(player_name)
|
|
||||||
update_formspec(player_name)
|
|
||||||
minetest.after(auto_update_time,auto_update,player_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--update_formspec()
|
--update_formspec()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
update_formspec = function(player_name)
|
update_formspec = function(player_name)
|
||||||
minetest.after(0.1,
|
minetest.after(0.1,
|
||||||
function(player_name)
|
function(player_name)
|
||||||
minetest.show_formspec(player_name, "fs", load_formspec())
|
minetest.show_formspec(player_name, "fs", load_formspec())
|
||||||
end,
|
end,
|
||||||
player_name)
|
player_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--load_formspec()
|
--load_formspec()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
load_formspec = function()
|
load_formspec = function()
|
||||||
local io = insecure_env.io
|
local io = insecure_env.io
|
||||||
local file = io.open(modpath .. "/formspec.spec", "rb")
|
local file = io.open(modpath .. "/formspec.spec", "rb")
|
||||||
if file == nil then
|
if file == nil then
|
||||||
return error_formspec
|
return error_formspec
|
||||||
else
|
else
|
||||||
local content = file:read("*all")
|
local content = file:read("*all")
|
||||||
file:close()
|
file:close()
|
||||||
if content == nil then
|
if content == nil then
|
||||||
return error_formspec
|
return error_formspec
|
||||||
else
|
else
|
||||||
return content
|
return content
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--turn_off_hud()
|
--turn_off_hud()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
turn_off_hud = function(player_ref)
|
turn_off_hud = function(player_ref)
|
||||||
local flags = {
|
local flags = {
|
||||||
hotbar = false,
|
hotbar = false,
|
||||||
healthbar = false,
|
healthbar = false,
|
||||||
crosshair = false,
|
crosshair = false,
|
||||||
wielditem = false,
|
wielditem = false,
|
||||||
breathbar = false,
|
breathbar = false,
|
||||||
minimap = false,
|
minimap = false,
|
||||||
minimap_radar = false,
|
minimap_radar = false,
|
||||||
}
|
}
|
||||||
player_ref:hud_set_flags(flags)
|
player_ref:hud_set_flags(flags)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--set_sky()
|
--set_sky()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
set_sky = function(player_ref)
|
set_sky = function(player_ref)
|
||||||
local sky = {
|
local sky = {
|
||||||
base_color = "#AAF",
|
base_color = "#AAF",
|
||||||
type = "plain",
|
type = "plain",
|
||||||
clouds = false,
|
clouds = false,
|
||||||
}
|
}
|
||||||
player_ref:set_sky(sky)
|
player_ref:set_sky(sky)
|
||||||
player_ref:override_day_night_ratio(0)
|
player_ref:override_day_night_ratio(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local time = 0
|
||||||
|
minetest.register_globalstep(function(dtime)
|
||||||
|
time = time + dtime
|
||||||
|
if time >= update_time then
|
||||||
|
local player = minetest.get_connected_players()[1]
|
||||||
|
if player then
|
||||||
|
update_formspec(player:get_player_name())
|
||||||
|
end
|
||||||
|
time = 0
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
Loading…
Add table
Reference in a new issue