From 1b7ea87932f6dd7461f884e1f0c909462d9e00de Mon Sep 17 00:00:00 2001 From: xisd Date: Tue, 4 Oct 2016 01:43:32 +0200 Subject: [PATCH] Support for a default messages file Hi ! I use your mod on a local server, it's great for displaying tips and news ! But since I'm too lazy to copy-paste the random_messages file from one world to an other, I made this change so if the random_messages file is not in the world folder, it look for a template in the mod folder before the 'blame the admin' last resort. Anyway I thought it could be useful for others --- init.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index a7c7656..35d2fb8 100644 --- a/init.lua +++ b/init.lua @@ -5,6 +5,8 @@ arsdragonfly@gmail.com --]] --Time between two subsequent messages. local MESSAGE_INTERVAL = 0 +-- Added default messages file +local default_messages_file = "default_random_messages" math.randomseed(os.time()) @@ -47,14 +49,27 @@ end function random_messages.read_messages() local line_number = 1 + -- define input local input = io.open(minetest.get_worldpath().."/random_messages","r") + -- no input file found if not input then + -- look for default file + local default_input = io.open(minetest.get_modpath("random_messages").."/"..default_messages_file,"r") 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") + if not default_input then + -- blame the admin if not found + 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") + else + -- or write default_input content in worldpath message file + local content = default_input:read("*all") + output:write(content) + end io.close(output) + io.close(default_input) input = io.open(minetest.get_worldpath().."/random_messages","r") end + -- we should have input by now, so lets read it for line in input:lines() do random_messages.messages[line_number] = line line_number = line_number + 1