2013-06-19 08:58:07 +02:00
|
|
|
--[[
|
|
|
|
RandomMessages mod by arsdragonfly.
|
|
|
|
arsdragonfly@gmail.com
|
|
|
|
6/19/2013
|
|
|
|
--]]
|
|
|
|
--Time between two subsequent messages.
|
2015-10-02 05:02:01 +02:00
|
|
|
local MESSAGE_INTERVAL = 0
|
2016-10-04 01:43:32 +02:00
|
|
|
-- Added default messages file
|
|
|
|
local default_messages_file = "default_random_messages"
|
2013-06-19 08:58:07 +02:00
|
|
|
|
|
|
|
math.randomseed(os.time())
|
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
random_messages = {}
|
|
|
|
random_messages.messages = {} --This table contains all messages.
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2017-08-04 15:07:28 +02:00
|
|
|
-- Load support for intllib.
|
|
|
|
local MP = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
local S, NS = dofile(MP.."/intllib.lua")
|
|
|
|
|
2015-10-02 05:42:31 +02:00
|
|
|
function table.count( t )
|
|
|
|
local i = 0
|
|
|
|
for k in pairs( t ) do i = i + 1 end
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
2015-10-02 05:35:45 +02:00
|
|
|
function table.random( t )
|
2015-10-02 05:42:31 +02:00
|
|
|
local rk = math.random( 1, table.count( t ) )
|
2015-10-02 05:02:01 +02:00
|
|
|
local i = 1
|
|
|
|
for k, v in pairs( t ) do
|
|
|
|
if ( i == rk ) then return v, k end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
function random_messages.initialize() --Set the interval in minetest.conf.
|
2017-06-18 21:05:25 +02:00
|
|
|
minetest.settings:set("random_messages_interval",120)
|
|
|
|
minetest.settings:save();
|
2015-10-02 05:02:01 +02:00
|
|
|
return 120
|
2014-03-22 05:02:02 +01:00
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
function random_messages.set_interval() --Read the interval from minetest.conf(set it if it doesn'st exist)
|
2017-06-18 21:05:25 +02:00
|
|
|
MESSAGE_INTERVAL = tonumber(minetest.settings:get("random_messages_interval")) or random_messages.initialize()
|
2014-03-22 05:02:02 +01:00
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2015-10-02 05:02:01 +02:00
|
|
|
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
|
|
|
|
|
2013-06-19 08:58:07 +02:00
|
|
|
function random_messages.read_messages()
|
|
|
|
local line_number = 1
|
2016-10-04 01:43:32 +02:00
|
|
|
-- define input
|
2013-06-19 08:58:07 +02:00
|
|
|
local input = io.open(minetest.get_worldpath().."/random_messages","r")
|
2016-10-04 01:43:32 +02:00
|
|
|
-- no input file found
|
2013-06-19 08:58:07 +02:00
|
|
|
if not input then
|
2016-10-04 01:43:32 +02:00
|
|
|
-- look for default file
|
|
|
|
local default_input = io.open(minetest.get_modpath("random_messages").."/"..default_messages_file,"r")
|
2014-03-22 05:02:02 +01:00
|
|
|
local output = io.open(minetest.get_worldpath().."/random_messages","w")
|
2016-10-04 01:43:32 +02:00
|
|
|
if not default_input then
|
|
|
|
-- blame the admin if not found
|
2017-08-04 15:07:28 +02:00
|
|
|
output:write(S("Blame the server admin! He/She has probably not edited the random messages yet.\n"))
|
|
|
|
output:write(S("Tell your dumb admin that this line is in (worldpath)/random_messages\n"))
|
2017-10-06 02:56:20 +02:00
|
|
|
return
|
2016-10-04 01:43:32 +02:00
|
|
|
else
|
|
|
|
-- or write default_input content in worldpath message file
|
|
|
|
local content = default_input:read("*all")
|
|
|
|
output:write(content)
|
|
|
|
end
|
2014-03-22 05:02:02 +01:00
|
|
|
io.close(output)
|
2016-10-04 01:43:32 +02:00
|
|
|
io.close(default_input)
|
2014-03-22 05:02:02 +01:00
|
|
|
input = io.open(minetest.get_worldpath().."/random_messages","r")
|
2013-06-19 08:58:07 +02:00
|
|
|
end
|
2016-10-04 01:43:32 +02:00
|
|
|
-- we should have input by now, so lets read it
|
2013-06-19 08:58:07 +02:00
|
|
|
for line in input:lines() do
|
2014-03-22 05:02:02 +01:00
|
|
|
random_messages.messages[line_number] = line
|
|
|
|
line_number = line_number + 1
|
2013-06-19 08:58:07 +02:00
|
|
|
end
|
2014-03-22 05:02:02 +01:00
|
|
|
io.close(input)
|
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
|
|
|
function random_messages.display_message(message_number)
|
2016-02-14 20:29:55 +01:00
|
|
|
local msg = random_messages.messages[message_number] or message_number
|
|
|
|
if msg then
|
|
|
|
minetest.chat_send_all(msg)
|
2013-06-19 08:58:07 +02:00
|
|
|
end
|
2014-03-22 05:02:02 +01:00
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
function random_messages.show_message()
|
2015-10-02 05:02:01 +02:00
|
|
|
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)
|
2014-03-22 05:02:02 +01:00
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
--When server starts:
|
|
|
|
random_messages.set_interval()
|
2013-06-19 08:58:07 +02:00
|
|
|
random_messages.read_messages()
|
|
|
|
|
2014-03-22 05:02:02 +01:00
|
|
|
local TIMER = 0
|
2017-10-06 03:14:45 +02:00
|
|
|
if random_messages.messages[1] then
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
TIMER = TIMER + dtime;
|
|
|
|
if TIMER > MESSAGE_INTERVAL then
|
|
|
|
random_messages.show_message()
|
|
|
|
TIMER = 0
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2013-06-19 08:58:07 +02:00
|
|
|
|
2015-10-02 05:02:01 +02:00
|
|
|
local register_chatcommand_table = {
|
|
|
|
params = "viewmessages | removemessage <number> | addmessage <number>",
|
2015-10-02 05:35:45 +02:00
|
|
|
privs = {server = true},
|
2017-08-04 15:07:28 +02:00
|
|
|
description = S("View and/or alter the server's random messages"),
|
2015-10-02 05:02:01 +02:00
|
|
|
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
|
2017-08-04 15:07:28 +02:00
|
|
|
return false,S("ERROR: No such message.")
|
2015-10-02 05:02:01 +02:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
t) then return end
|
|
|
|
random_messages.remove_message(t[2])
|
|
|
|
elseif t[1] == "addmessage" then
|
|
|
|
if not t[2] then
|
2017-08-04 15:07:28 +02:00
|
|
|
minetest.chat_send_player(name,S("ERROR: No message."))
|
2015-10-02 05:02:01 +02:00
|
|
|
else
|
|
|
|
random_messages.add_message(t)
|
|
|
|
end
|
|
|
|
else
|
2017-08-04 15:07:28 +02:00
|
|
|
minetest.chat_send_player(name,S("ERROR: Invalid command."))
|
2015-10-02 05:02:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
minetest.register_chatcommand("random_messages", register_chatcommand_table)
|
|
|
|
minetest.register_chatcommand("rmessages", register_chatcommand_table)
|