money3/init.lua

118 lines
3.2 KiB
Lua
Raw Normal View History

2014-02-15 05:46:44 +01:00
--
2019-05-23 06:47:11 +02:00
-- money3
2014-02-15 05:46:44 +01:00
--
2019-05-23 06:47:11 +02:00
-- Copyright © 2012 Bad_Command
-- Copyright © 2012 kotolegokot
-- Copyright © 2019 by luk3yx
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2014-02-15 05:46:44 +01:00
--
2019-05-23 06:47:11 +02:00
money3 = {}
money3.version = 2.5
2014-02-15 05:46:44 +01:00
2019-05-23 06:47:11 +02:00
local modpath = assert(minetest.get_modpath("money3",
"Please call this mod money3."))
dofile(modpath .. "/config.lua")
2014-02-15 05:46:44 +01:00
2019-05-23 06:47:11 +02:00
assert(not minetest.get_modpath("money2"), "money3 and money2 do not mix.")
2014-02-15 05:46:44 +01:00
2019-05-23 06:47:11 +02:00
local storage = minetest.get_mod_storage()
loadfile(modpath .. "/core.lua")(storage)
loadfile(modpath .. "/migration.lua")(storage)
2014-02-15 05:46:44 +01:00
2019-05-23 06:47:11 +02:00
-- Only load convertval.lua if required.
if next(money3.convert_items) then
loadfile(modpath .. "/convertval.lua")(storage)
2014-02-15 05:46:44 +01:00
end
2019-05-23 06:47:11 +02:00
-- Load income
if money3.enable_income then
dofile(modpath .. "/income.lua")
2014-02-15 05:46:44 +01:00
end
local function set_if_exists(name, balance)
if money3.user_exists(name) and balance == balance then
money3.set(name, balance)
return true
end
return false
end
-- Register money3 as a backend for unified_money
if minetest.get_modpath("um_core") then
local function make_canonical_name_cache()
if minetest.global_exists("canonical_name") then
return canonical_name.get
end
local names = {}
for name in minetest.get_auth_handler().iterate() do
names[name:lower()] = name
end
return function(name)
return names[name:lower()]
end
end
unified_money.register_backend({
get_balance = money3.get,
set_balance = set_if_exists,
create_account = function(name, default_balance)
2023-08-08 06:37:30 +02:00
if not name:find(":", 1, true) and
not minetest.get_player_privs(name).money then
return false
end
money3.set(name, default_balance or 0)
return true
end,
2023-08-08 12:11:13 +02:00
delete_account = money3.delete,
account_exists = money3.user_exists,
list_accounts = function()
local get_canonical_name = make_canonical_name_cache()
local accounts = {}
for _, key in ipairs(storage:get_keys()) do
if key:sub(1, 8) == "balance-" then
local name = key:sub(9)
accounts[#accounts + 1] = get_canonical_name(name) or name
end
end
return accounts
end,
})
end
2019-05-23 06:47:11 +02:00
-- Make sure the lurkcoin mod knows that money3 exists
if minetest.get_modpath("lurkcoin") then
lurkcoin.change_bank({
user_exists = money3.user_exists,
getbal = money3.get,
setbal = set_if_exists,
2019-05-23 06:47:11 +02:00
pay = function(from, to, amount)
local err = money.transfer(from, to, amount)
return not err, err
2014-02-15 05:46:44 +01:00
end
2019-05-23 06:47:11 +02:00
})
2014-02-15 05:46:44 +01:00
end
2019-05-23 06:47:11 +02:00
-- Backwards compatibility
rawset(_G, "money", money3)
2014-02-15 05:46:44 +01:00
2019-05-23 06:47:11 +02:00
-- I couldn't be bothered to update lockedsign.lua
if minetest.get_modpath("locked_sign") then
dofile(modpath .. "/lockedsign.lua")
2014-02-15 05:46:44 +01:00
end