2021-06-11 20:47:29 +02:00
|
|
|
-- Localize globals
|
2021-06-18 20:42:32 +02:00
|
|
|
local ipairs, minetest, modlib, table = ipairs, minetest, modlib, table
|
2021-06-11 20:47:29 +02:00
|
|
|
|
2021-06-17 19:45:08 +02:00
|
|
|
-- Set environment
|
|
|
|
local _ENV = {}
|
|
|
|
setfenv(1, _ENV)
|
|
|
|
|
2021-09-16 20:22:52 +02:00
|
|
|
--! deprecated
|
|
|
|
|
2021-01-30 11:50:05 +01:00
|
|
|
forbidden_names = {}
|
|
|
|
|
|
|
|
function register_forbidden_name(name) forbidden_names[name] = true end
|
|
|
|
|
|
|
|
function unregister_forbidden_name(name) forbidden_names[name] = nil end
|
|
|
|
|
|
|
|
minetest.register_on_prejoinplayer(function(name)
|
|
|
|
if forbidden_names[name] then
|
|
|
|
return 'The name "' .. name .. '" is not allowed as a player name'
|
|
|
|
end
|
|
|
|
end)
|
2020-02-09 01:39:54 +01:00
|
|
|
playerdata = {}
|
2020-02-25 20:47:42 +01:00
|
|
|
defaults = {}
|
|
|
|
playerdata_functions = {}
|
2021-01-30 11:50:05 +01:00
|
|
|
|
|
|
|
function delete_player_data(playername) playerdata[playername] = nil end
|
|
|
|
|
|
|
|
function create_player_data(playername) playerdata[playername] = {} end
|
|
|
|
|
2020-02-25 20:47:42 +01:00
|
|
|
function init_player_data(playername)
|
2021-01-30 11:50:05 +01:00
|
|
|
modlib.table.add_all(playerdata[playername], defaults)
|
|
|
|
for _, callback in ipairs(playerdata_functions) do callback(playerdata[playername]) end
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2021-01-30 11:50:05 +01:00
|
|
|
|
|
|
|
function get_player_data(playername) return playerdata[playername] end
|
|
|
|
|
|
|
|
function get_property(playername, propertyname) return get_player_data(playername)[propertyname] end
|
|
|
|
|
|
|
|
function set_property(playername, propertyname, propertyvalue) get_player_data(playername)[propertyname] = propertyvalue end
|
|
|
|
|
|
|
|
function get_property_default(propertyname) return defaults[propertyname] end
|
|
|
|
|
|
|
|
function set_property_default(propertyname, defaultvalue) defaults[propertyname] = defaultvalue end
|
|
|
|
|
|
|
|
function add_playerdata_function(callback) table.insert(playerdata_functions, callback) end
|
|
|
|
|
2020-02-25 20:47:42 +01:00
|
|
|
function get_color(player)
|
2021-01-30 11:50:05 +01:00
|
|
|
local sender_color = player:get_properties().nametag_color
|
|
|
|
if sender_color then
|
|
|
|
sender_color = minetest.rgba(sender_color.r, sender_color.g, sender_color.b)
|
|
|
|
else sender_color = "#FFFFFF" end
|
|
|
|
return sender_color
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2021-01-30 11:50:05 +01:00
|
|
|
|
2020-02-25 20:47:42 +01:00
|
|
|
function get_color_table(player)
|
2021-01-30 11:50:05 +01:00
|
|
|
local sender_color = player:get_properties().nametag_color
|
|
|
|
return sender_color or { r = 255, g = 255, b = 255 }
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2021-01-30 11:50:05 +01:00
|
|
|
|
|
|
|
--! deprecated in favor of modlib.minetest.colorspec
|
2020-02-25 20:47:42 +01:00
|
|
|
function get_color_int(player)
|
2021-01-30 11:50:05 +01:00
|
|
|
local sender_color = player:get_properties().nametag_color
|
|
|
|
if sender_color then
|
|
|
|
sender_color = sender_color.b + sender_color.g * 0x100 + sender_color.r * 0x10000
|
|
|
|
else sender_color = 0xFFFFFF end
|
|
|
|
return sender_color
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
|
|
|
|
2021-01-30 11:50:05 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
create_player_data(playername)
|
|
|
|
init_player_data(playername)
|
|
|
|
end)
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
delete_player_data(playername)
|
|
|
|
end)
|
2020-06-28 22:42:24 +02:00
|
|
|
|
|
|
|
function datatable(table, default)
|
2021-01-30 11:50:05 +01:00
|
|
|
table = table or {}
|
|
|
|
default = default or {}
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
table[name] = table[name] or default
|
|
|
|
end)
|
|
|
|
minetest.register_on_leaveplayer(function(player) table[player:get_player_name()] = nil end)
|
|
|
|
return table
|
2021-06-17 19:45:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Export environment
|
|
|
|
return _ENV
|