Add news mod

master
Brandon 2016-09-23 22:43:50 -05:00
parent 209cfc4e16
commit 1330192497
2 changed files with 131 additions and 0 deletions

26
mods/news/README.txt Normal file
View File

@ -0,0 +1,26 @@
Minetest News
-------------
News that pops up in a formspec when a player joins the server.
Create a text file called news.txt in the world folder.
This text file can be updated any time and will always be current in game without a server reboot
To add extra articles just create a text file for each article in the world folder in the format
news_[article name].txt
(i.e. news_crafting.txt would show an article for the command /news crafting)
This version shows pops up if news.txt was changed since last log on.
This modification is still bit rough as it needs cronjob which will be generating md5 hash of news.txt
into file called news.txt.md5 . Without that file, script will crash.
example cron line:
* * * * * user md5sum /path/to/news.txt > /path/to/news.txt.md5
As I don't use windows, I will not provide information how to do so on windows.

105
mods/news/init.lua Normal file
View File

@ -0,0 +1,105 @@
local news = {}
local news_player_stamps = {}
local file = io.open(minetest.get_worldpath().."/player_stamps.txt", "r")
if file then
news_player_stamps = minetest.deserialize(file:read("*all"))
file:close()
if not news_player_stamps then
news_player_stamps = {}
end
end
local function update_hash()
local file = io.open(minetest.get_worldpath().."/news.txt.md5", "r")
local news_h
if file then
local news_hfile_content = file:read("*all")
news_h = string.match(news_hfile_content, "(%w+)")
file:close()
end
return news_h
end
local news_hash = update_hash()
local function compare_hashes(data, hash)
if data ~= nil then
if data == hash then
return false
else
return true
end
else
return nil
end
end
local function save_file()
local file = io.open(minetest.get_worldpath().."/player_stamps.txt", "w")
if file then
file:write(minetest.serialize(news_player_stamps))
file:close()
end
end
news.path = minetest.get_worldpath()
function news.formspec(player,article)
if ( article == "" or article == nil ) then
article = "news.txt"
else
article = "news_"..article..".txt"
end
local newsfile = io.open(news.path.."/"..article,"r")
local formspec = "size[12,10]"
if newsfile ~= nil then
local newscontent = newsfile:read("*a")
formspec = formspec.."textarea[.25,.25;12,10;news;;"..newscontent.."]"
else
formspec = formspec.."label[.25,.25;Article does not exist]"
end
formspec = formspec.."button_exit[.25,9;2,1;exit;Close"
if ( newsfile ~= nil ) then
newsfile:close()
end
return formspec
end
function news.show_formspec(player)
local name = player:get_player_name()
minetest.show_formspec(name,"news",news.formspec(player))
minetest.log('action','Showing formspec to '..name)
end
minetest.register_chatcommand("news",{
params = "<article>",
description="Shows the server news",
func = function (name,params)
local player = minetest.get_player_by_name(name)
minetest.show_formspec(name,"news",news.formspec(player,params))
end,
})
minetest.register_on_joinplayer(function (player)
local name = player:get_player_name()
if news_player_stamps[name] == nil then
news_hash = update_hash()
news_player_stamps[name] = news_hash
minetest.after(5,news.show_formspec,player)
save_file()
else
news_hash = update_hash()
if compare_hashes(news_player_stamps[name], news_hash) ~= nil and compare_hashes(news_player_stamps[name], news_hash) then
news_player_stamps[name] = news_hash
minetest.after(5,news.show_formspec,player)
save_file()
end
end
end)