Add autorun feature (#1)

This commit is contained in:
Zemtzov7 2024-01-22 13:53:18 +05:00 committed by GitHub
parent 66ef92ad79
commit 859a2c5a7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 4 deletions

View File

@ -26,6 +26,7 @@ If you don't want the buttons at all, you can add
`def` can be a table containing `code` (or `func`), and optionally `owner`.
If `persistent` is specified, this snippet will remain registered across
reboots.
If `autorun` is specified, this snippet will auto run on server start. (Snippet must be persistent to support autorun)
- `snippets.unregister_snippet(name)`: The opposite of
`snippets.register_snippet`.
- `snippets.registered_snippets`: A table containing the above snippets.

View File

@ -36,10 +36,10 @@ function snippets.update_console(name)
local selected, unaved = 0, false
local selected_snippet = form.context.selected_snippet
for id, snippet in ipairs(snippet_list) do
formspec = formspec .. ',##' .. minetest.formspec_escape(snippet)
local def = snippets.registered_snippets[snippet]
formspec = formspec .. ',' .. (def.autorun and "#ff7777\\[A\\] " or "##") .. minetest.formspec_escape(snippet)
if snippet == selected_snippet then
selected = id
local def = snippets.registered_snippets[snippet]
if (def and def.code or '') ~= form.context.code then
formspec = formspec .. ' (unsaved)'
end
@ -65,11 +65,11 @@ function snippets.update_console(name)
formspec = formspec .. ';1'
end
formspec = formspec ..
']button[3.9,5.14;10.21,0.81;reset;Reset]' ..
']button[3.9,5.14;'..(selected_snippet and '8.8' or '10.21')..',0.81;reset;Reset]' ..
'box[3.9,0.4;10,4.5;#ffffff]'
else
formspec = formspec .. ';1]' ..
'button[3.9,5.14;10.21,0.81;run;Run]'
'button[3.9,5.14;'..(selected_snippet and '8.8' or '10.21')..',0.81;run;Run]'
end
if not form.context.code then form.context.code = '' end
@ -96,6 +96,9 @@ function snippets.update_console(name)
(form.context.text and '' or 'code') .. ';Snippet: ' ..
minetest.formspec_escape(snippet .. ', owner: ' .. owner) .. ';' ..
code .. ']'
if selected_snippet then
formspec = formspec .. 'checkbox[12.7,5.05;autorun;Autorun;'..tostring(def and (def.autorun or false))..']'
end
form:set_formspec(formspec)
end
@ -240,6 +243,7 @@ function callback(form, fields)
owner = name,
code = form.context.code,
persistent = true,
autorun = form.context.autorun
})
end
snippets.show_console(name)
@ -251,6 +255,8 @@ function callback(form, fields)
'field[filename;Please enter a new snippet name.;]')
saveform:add_callback(saveform_callback)
saveform:show()
elseif fields.autorun and form.context.selected_snippet then
form.context.autorun = fields.autorun == "true"
elseif fields.quit then
form.text = nil
end

View File

@ -24,3 +24,11 @@ local enable_buttons = minetest.settings:get_bool('snippets.enable_buttons')
if enable_buttons or enable_buttons == nil then
dofile(modpath .. '/nodes.lua')
end
minetest.register_on_mods_loaded(function()
for name, def in pairs(snippets.registered_snippets) do
if def.autorun then
snippets.run(name)
end
end
end)

View File

@ -38,6 +38,7 @@ function snippets.register_snippet(name, def)
storage:set_string('>' .. name, minetest.serialize({
code = def.code,
owner = def.owner,
autorun = def.autorun
}))
end