minetest-mm/mods/currency/income.lua

57 lines
2 KiB
Lua
Raw Normal View History

2020-10-26 17:38:53 +01:00
local players_income = {}
local income_enabled = minetest.settings:get_bool("currency.income_enabled", true)
local creative_income_enabled = minetest.settings:get_bool("currency.creative_income_enabled", true)
local income_item = minetest.settings:get("currency.income_item") or "currency:minegeld_10"
local income_count = tonumber(minetest.settings:get("currency.income_count")) or 1
local income_period = tonumber(minetest.settings:get("currency.income_period")) or 720
if income_enabled then
local timer = 0
if creative_income_enabled then
minetest.register_globalstep(function(dtime)
timer = timer + dtime;
if timer >= income_period then
timer = 0
for _, player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
players_income[name] = income_count
minetest.log("info", "[Currency] basic income for "..name)
end
end
end)
else
minetest.register_globalstep(function(dtime)
timer = timer + dtime;
if timer >= income_period then
timer = 0
for _, player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local privs = minetest.get_player_privs(name)
if not (privs.creative or privs.give) then
players_income[name] = income_count
minetest.log("info", "[Currency] basic income for "..name)
end
end
end
end)
end
local function earn_income(player)
if not player or player.is_fake_player then return end
local name = player:get_player_name()
2024-12-19 12:55:40 +01:00
local ic = players_income[name]
if ic and ic > 0 then
2020-10-26 17:38:53 +01:00
local inv = player:get_inventory()
2024-12-19 12:55:40 +01:00
inv:add_item("main", {name=income_item, count=ic})
2020-10-26 17:38:53 +01:00
players_income[name] = nil
minetest.log("info", "[Currency] added basic income for "..name.." to inventory")
end
end
minetest.register_on_dignode(function(pos, oldnode, digger) earn_income(digger) end)
minetest.register_on_placenode(function(pos, node, placer) earn_income(placer) end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) earn_income(player) end)
end