mirror of
https://github.com/arsdragonfly/random_messages.git
synced 2025-01-09 09:20:26 +01:00
fix indentation & fix crash on startup
This commit is contained in:
parent
703626648c
commit
7884e6b424
52
init.lua
52
init.lua
@ -8,54 +8,56 @@ local MESSAGE_INTERVAL = 0
|
|||||||
|
|
||||||
math.randomseed(os.time())
|
math.randomseed(os.time())
|
||||||
|
|
||||||
random_messages = {}
|
random_messages = {}
|
||||||
random_messages.messages = {} --This table contains all messages.
|
random_messages.messages = {} --This table contains all messages.
|
||||||
|
|
||||||
function random_messages.initialize() --Set the interval in minetest.conf.
|
function random_messages.initialize() --Set the interval in minetest.conf.
|
||||||
minetest.setting_set("random_messages_interval",120)
|
minetest.setting_set("random_messages_interval",120)
|
||||||
minetest.setting_save();
|
minetest.setting_save();
|
||||||
return 120
|
return 120
|
||||||
end
|
end
|
||||||
|
|
||||||
function random_messages.set_interval() --Read the interval from minetest.conf(set it if it doesn'st exist)
|
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()
|
MESSAGE_INTERVAL = tonumber(minetest.setting_get("random_messages_interval")) or random_messages.initialize()
|
||||||
end
|
end
|
||||||
|
|
||||||
function random_messages.read_messages()
|
function random_messages.read_messages()
|
||||||
local line_number = 1
|
local line_number = 1
|
||||||
local input = io.open(minetest.get_worldpath().."/random_messages","r")
|
local input = io.open(minetest.get_worldpath().."/random_messages","r")
|
||||||
if not input then
|
if not input then
|
||||||
local output = io.open(minetest.get_worldpath().."/random_messages","w")
|
local output = io.open(minetest.get_worldpath().."/random_messages","w")
|
||||||
io.close(output)
|
output:write("Blame the server admin! He/She has probably not edited the random messages yet.\n")
|
||||||
input = io.open(minetest.get_worldpath().."/random_messages","r")
|
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")
|
||||||
end
|
end
|
||||||
for line in input:lines() do
|
for line in input:lines() do
|
||||||
random_messages.messages[line_number] = line
|
random_messages.messages[line_number] = line
|
||||||
line_number = line_number + 1
|
line_number = line_number + 1
|
||||||
end
|
|
||||||
io.close(input)
|
|
||||||
end
|
end
|
||||||
|
io.close(input)
|
||||||
|
end
|
||||||
|
|
||||||
function random_messages.display_message(message_number)
|
function random_messages.display_message(message_number)
|
||||||
if random_messages.messages[message_number] then
|
if random_messages.messages[message_number] then
|
||||||
minetest.chat_send_all(random_messages.messages[message_number])
|
minetest.chat_send_all(random_messages.messages[message_number])
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function random_messages.show_message()
|
function random_messages.show_message()
|
||||||
random_messages.display_message(math.random(#random_messages.messages))
|
random_messages.display_message(math.random(#random_messages.messages))
|
||||||
end
|
end
|
||||||
|
|
||||||
--When server starts:
|
--When server starts:
|
||||||
random_messages.set_interval()
|
random_messages.set_interval()
|
||||||
random_messages.read_messages()
|
random_messages.read_messages()
|
||||||
|
|
||||||
local TIMER = 0
|
local TIMER = 0
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
TIMER = TIMER + dtime;
|
TIMER = TIMER + dtime;
|
||||||
if TIMER > MESSAGE_INTERVAL then
|
if TIMER > MESSAGE_INTERVAL then
|
||||||
random_messages.show_message()
|
random_messages.show_message()
|
||||||
TIMER = 0
|
TIMER = 0
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user