You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4KB

  1. --
  2. -- money3 income
  3. -- Inspired by https://gitlab.com/VanessaE/currency/blob/master/income.lua
  4. --
  5. -- Copyright © 2019 by luk3yx
  6. --
  7. -- This library is free software; you can redistribute it and/or
  8. -- modify it under the terms of the GNU Lesser General Public
  9. -- License as published by the Free Software Foundation; either
  10. -- version 2.1 of the License, or (at your option) any later version.
  11. --
  12. -- This program is distributed in the hope that it will be useful,
  13. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. -- GNU General Public License for more details.
  16. --
  17. -- You should have received a copy of the GNU Lesser General Public
  18. -- License along with this library; if not, write to the Free Software
  19. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. --
  21. local income = {}
  22. -- Earn income
  23. function money3.earn_income(name)
  24. if type(name) ~= "string" then
  25. if not name or name.is_fake_player then return end
  26. name = name:get_player_name()
  27. end
  28. if income[name] then
  29. income[name] = nil
  30. local amount = money3.income_amount
  31. money3.add(name, amount)
  32. -- Tell the player
  33. local msg = "[money3] You have earned " .. money3.format(amount) ..
  34. ". Your balance is now " .. money3.format(money3.get(name)) .. "."
  35. if minetest.colorize then msg = minetest.colorize("#CCCCCC", msg) end
  36. minetest.chat_send_player(name, msg)
  37. minetest.log("action", "[money3] Given " .. name .. " income.")
  38. end
  39. end
  40. -- The daemon
  41. local time = 0
  42. minetest.register_globalstep(function(dtime)
  43. time = time + dtime
  44. if time >= 720 then
  45. -- Reset everything
  46. time = 0
  47. for k, v in pairs(income) do income[k] = nil end
  48. -- Add money
  49. for _, player in ipairs(minetest.get_connected_players()) do
  50. income[player:get_player_name()] = true
  51. end
  52. end
  53. end)
  54. function money3.debug_step() time = 710 end
  55. minetest.register_on_dignode(function(pos, oldnode, digger)
  56. money3.earn_income(digger)
  57. end)
  58. minetest.register_on_placenode(function(pos, newnode, placer)
  59. money3.earn_income(placer)
  60. end)
  61. -- If an outdated version of the currency mod exists, use a hack to disable its
  62. -- income system.
  63. if minetest.get_modpath("currency") and
  64. minetest.global_exists("players_income") and
  65. not minetest.get_modpath("players_income") then
  66. setmetatable(players_income, {
  67. __index = function(table, key) return 0 end,
  68. __newindex = function(table, key, value) end,
  69. })
  70. end