init.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -- Set this to how soon after crash/restart the person must log back in by to recieve a gift
  2. local gift_timeout = 300
  3. -- Set this to the name of the gift you wish to give
  4. local gift_name = "default:mese"
  5. -- Set this to how many of the gift you wish to give
  6. local gift_count = 1
  7. local function load_data(filename)
  8. local file = io.open(filename, "r")
  9. if file then
  10. local table = minetest.deserialize(file:read("*all"))
  11. file:close()
  12. if type(table) == "table" then
  13. return table
  14. else
  15. return {}
  16. end
  17. end
  18. end
  19. local function save_data(filename, data)
  20. local file = io.open(filename, "w")
  21. if file then
  22. file:write(minetest.serialize(data))
  23. file:close()
  24. end
  25. end
  26. local db_filename = minetest.get_worldpath().."/kicked_players.txt"
  27. local kicked_players = load_data(db_filename) or {} -- loads file if it exists, or makes empty table
  28. local current_players = {}
  29. local give_gifts = true
  30. minetest.after(gift_timeout, function()
  31. give_gifts = false
  32. end)
  33. local function get_description(name)
  34. if not minetest.registered_items[name] then return name end
  35. return minetest.registered_items[name].description or name
  36. end
  37. minetest.register_on_joinplayer(function(player)
  38. name = player:get_player_name()
  39. if give_gifts == true then
  40. if kicked_players[name] then
  41. player:get_inventory():add_item("main", ItemStack(gift_name.." "..tostring(gift_count)))
  42. minetest.chat_send_player(name, "We're sorry you were kicked, have "..tostring(gift_count).." "..get_description(gift_name).." as a thank you gift for coming back :)")
  43. kicked_players[name] = nil
  44. end
  45. end
  46. current_players[name] = true
  47. save_data(db_filename, current_players)
  48. end)
  49. minetest.register_on_leaveplayer(function(player)
  50. name = player:get_player_name()
  51. current_players[name] = nil
  52. save_data(db_filename, current_players)
  53. end)