chatcommand add internal

This commit is contained in:
AliasAlreadyTaken 2023-09-22 09:33:35 +02:00
parent d5efa16321
commit bba74e75aa
2 changed files with 60 additions and 11 deletions

12
api.lua
View File

@ -27,4 +27,16 @@ function yl_announcements.delete(a_id)
data = "Could not find announcement with id " .. a_id
end
return success, data
end
function yl_announcements.copy(a_id)
local announcement = yl_announcements.get_announcement(a_id)
local content = minetest.formspec_escape(dump(announcement))
local formspec = "formspec_version[6]" ..
"size[16,2]" ..
"button_exit[15.4,0.1;0.5,0.5;X;X]" ..
"textarea[0.05,0.05;15.3,1.9;;;" ..
content ..
"]"
return formspec or ""
end

View File

@ -2,26 +2,62 @@
-- Storage
function yl_announcements.load_announcements()
yl_announcements.data = minetest.parse_json(yl_announcements.mod_storage:get_string("data")) or {}
local data = yl_announcements.modstorage:get("data") or "[]"
yl_announcements.data = minetest.parse_json(data) or {}
end
function yl_announcements.save_announcements(data)
yl_announcements.mod_storage:set_string("data", minetest.write_json(data))
yl_announcements.modstorage:set_string("data", minetest.write_json(data))
end
-- Chatcommands
function yl_announcements.chatcommand_announcement_add(name, param) -- param is a numerical a_id
function yl_announcements.chatcommand_announcement_add(name, param) -- param is a string containing a message and more
-- defense
local player = minetest.get_player_by_name(name)
if not player then
return false, "Player not online"
end
if (not param) or (type(param) ~= "string") or (param == "") then
return false, "Requirements not met"
end
-- Create announcement
local announcement = string.split(param, "$", true)
core.log("action","announcement="..dump(announcement))
local success, data, a_id = yl_announcements.add(name,announcement)
-- Save
if success == false then
return false, data
else
yl_announcements.save_announcements(data)
return true, "Add announcement of ID " .. a_id
end
end
function yl_announcements.chatcommand_announcement_copy(name, param) -- param is a numerical a_id
-- defense
local player = minetest.get_player_by_name(name)
if not player then
return false, "Player not online"
end
if param == "" then
return false, "Announcement ID missing"
end
local n_param = tonumber(param)
if type(n_param) ~= "number" then
return false, "Announcement ID not a number"
end
local announcement = yl_announcements.get_announcement(n_param)
if announcement == yl_announcements.error then
return false, "Cannot find announcement with given ID"
end
-- Send the formspec
local formspec = yl_announcements.copy(a_id)
minetest.show_formspec(name, "yl_announcements:copy", formspec)
-- Report
return true, "Copied announcement ID " .. n_param
end
function yl_announcements.chatcommand_announcement_delete(name, param) -- param is a numerical a_id
-- defense
local player = minetest.get_player_by_name(name)
@ -46,18 +82,19 @@ function yl_announcements.chatcommand_announcement_delete(name, param) -- param
return false, data
else
yl_announcements.save_announcements(data)
return true, "Deleted announcement ID " .. param
return true, "Deleted announcement ID " .. n_param
end
end
function yl_announcements.chatcommand_announcement_list_all(name, param)
function yl_announcements.chatcommand_announcement_list_all(name, param) -- param must be empty
end
function yl_announcements.chatcommand_announcement_list(name, param)
function yl_announcements.chatcommand_announcement_list(name, param) -- param is a numerical a_id
end
function yl_announcements.chatcommand_announcement_say_all(name, param)
function yl_announcements.chatcommand_announcement_say_all(name, param) -- param must be empty
end
function yl_announcements.chatcommand_announcement_say(name, param)
function yl_announcements.chatcommand_announcement_say(name, param) -- param is a numerical a_id
end