forked from nalc/random_messages
Add live message adding & removal
This commit is contained in:
parent
5fd961d79c
commit
c36f43c759
83
init.lua
83
init.lua
|
@ -4,23 +4,41 @@ arsdragonfly@gmail.com
|
|||
6/19/2013
|
||||
--]]
|
||||
--Time between two subsequent messages.
|
||||
local MESSAGE_INTERVAL = 0
|
||||
local MESSAGE_INTERVAL = 0
|
||||
|
||||
math.randomseed(os.time())
|
||||
|
||||
random_messages = {}
|
||||
random_messages.messages = {} --This table contains all messages.
|
||||
|
||||
function table.Random( t )
|
||||
local rk = math.random( 1, table.Count( t ) )
|
||||
local i = 1
|
||||
for k, v in pairs( t ) do
|
||||
if ( i == rk ) then return v, k end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
function random_messages.initialize() --Set the interval in minetest.conf.
|
||||
minetest.setting_set("random_messages_interval",120)
|
||||
minetest.setting_save();
|
||||
return 120
|
||||
return 120
|
||||
end
|
||||
|
||||
function random_messages.set_interval() --Read the interval from minetest.conf(set it if it doesn'st exist)
|
||||
MESSAGE_INTERVAL = tonumber(minetest.setting_get("random_messages_interval")) or random_messages.initialize()
|
||||
end
|
||||
|
||||
function random_messages.check_params(name,func,params)
|
||||
local stat,msg = func(params)
|
||||
if not stat then
|
||||
minetest.chat_send_player(name,msg)
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function random_messages.read_messages()
|
||||
local line_number = 1
|
||||
local input = io.open(minetest.get_worldpath().."/random_messages","r")
|
||||
|
@ -45,7 +63,33 @@ function random_messages.display_message(message_number)
|
|||
end
|
||||
|
||||
function random_messages.show_message()
|
||||
random_messages.display_message(math.random(#random_messages.messages))
|
||||
random_messages.display_message(table.random(random_messages.messages))
|
||||
end
|
||||
|
||||
function random_messages.list_messages()
|
||||
local str = ""
|
||||
for k,v in pairs(random_messages.messages) do
|
||||
str = str .. k .. " | " .. v .. "\n"
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
function random_messages.remove_message(k)
|
||||
table.remove(random_messages.messages,k)
|
||||
random_messages.save_messages()
|
||||
end
|
||||
|
||||
function random_messages.add_message(t)
|
||||
table.insert(random_messages.messages,table.concat(t," ",2))
|
||||
random_messages.save_messages()
|
||||
end
|
||||
|
||||
function random_messages.save_messages()
|
||||
local output = io.open(minetest.get_worldpath().."/random_messages","w")
|
||||
for k,v in pairs(random_messages.messages) do
|
||||
output:write(v .. "\n")
|
||||
end
|
||||
io.close(output)
|
||||
end
|
||||
|
||||
--When server starts:
|
||||
|
@ -61,3 +105,36 @@ minetest.register_globalstep(function(dtime)
|
|||
end
|
||||
end)
|
||||
|
||||
local register_chatcommand_table = {
|
||||
params = "viewmessages | removemessage <number> | addmessage <number>",
|
||||
description = "View and/or alter the server's random messages",
|
||||
func = function(name,param)
|
||||
local t = string.split(param, " ")
|
||||
if t[1] == "viewmessages" then
|
||||
minetest.chat_send_player(name,random_messages.list_messages())
|
||||
elseif t[1] == "removemessage" then
|
||||
if not random_messages.check_params(
|
||||
name,
|
||||
function (params)
|
||||
if not tonumber(params[2]) or
|
||||
random_messages.messages[tonumber(params[2])] == nil then
|
||||
return false,"ERROR: No such message."
|
||||
end
|
||||
return true
|
||||
end,
|
||||
t) then return end
|
||||
random_messages.remove_message(t[2])
|
||||
elseif t[1] == "addmessage" then
|
||||
if not t[2] then
|
||||
minetest.chat_send_player(name,"ERROR: No message.")
|
||||
else
|
||||
random_messages.add_message(t)
|
||||
end
|
||||
else
|
||||
minetest.chat_send_player(name,"ERROR: Invalid command.")
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
minetest.register_chatcommand("random_messages", register_chatcommand_table)
|
||||
minetest.register_chatcommand("rmessages", register_chatcommand_table)
|
||||
|
|
Loading…
Reference in New Issue
Block a user