random_messages/init.lua

64 lines
1.8 KiB
Lua
Raw Normal View History

2013-06-19 08:58:07 +02:00
--[[
RandomMessages mod by arsdragonfly.
arsdragonfly@gmail.com
6/19/2013
--]]
--Time between two subsequent messages.
local MESSAGE_INTERVAL = 0
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
2014-03-22 05:02:02 +01:00
function random_messages.initialize() --Set the interval in minetest.conf.
2013-06-19 08:58:07 +02:00
minetest.setting_set("random_messages_interval",120)
minetest.setting_save();
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)
2013-06-19 08:58:07 +02:00
MESSAGE_INTERVAL = tonumber(minetest.setting_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
function random_messages.read_messages()
local line_number = 1
local input = io.open(minetest.get_worldpath().."/random_messages","r")
if not input then
2014-03-22 05:02:02 +01:00
local output = io.open(minetest.get_worldpath().."/random_messages","w")
output:write("Blame the server admin! He/She has probably not edited the random messages yet.\n")
output:write("Tell your dumb admin that this line is in (worldpath)/random_messages \n")
io.close(output)
input = io.open(minetest.get_worldpath().."/random_messages","r")
2013-06-19 08:58:07 +02:00
end
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)
if random_messages.messages[message_number] then
2014-03-22 05:02:02 +01:00
minetest.chat_send_all(random_messages.messages[message_number])
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()
random_messages.display_message(math.random(#random_messages.messages))
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
2013-06-19 08:58:07 +02:00
minetest.register_globalstep(function(dtime)
2014-03-22 05:02:02 +01:00
TIMER = TIMER + dtime;
if TIMER > MESSAGE_INTERVAL then
2013-06-19 08:58:07 +02:00
random_messages.show_message()
TIMER = 0
2014-03-22 05:02:02 +01:00
end
end)
2013-06-19 08:58:07 +02:00