Deprecate player.get_color_int, beautification

This commit is contained in:
Lars Mueller 2021-01-30 11:50:05 +01:00
parent 1d76fee9db
commit a71c115647

@ -1,88 +1,70 @@
forbidden_names = {"me"} forbidden_names = {}
function register_forbidden_name(name)
forbidden_names[name] = true function register_forbidden_name(name) forbidden_names[name] = true end
end
function unregister_forbidden_name(name) function unregister_forbidden_name(name) forbidden_names[name] = nil end
forbidden_names[name] = nil
minetest.register_on_prejoinplayer(function(name)
if forbidden_names[name] then
return 'The name "' .. name .. '" is not allowed as a player name'
end end
end)
playerdata = {} playerdata = {}
defaults = {} defaults = {}
playerdata_functions = {} playerdata_functions = {}
function delete_player_data(playername)
playerdata[playername] = nil function delete_player_data(playername) playerdata[playername] = nil end
end
function create_player_data(playername) function create_player_data(playername) playerdata[playername] = {} end
playerdata[playername] = {}
end
function init_player_data(playername) function init_player_data(playername)
modlib.table.add_all(playerdata[playername], defaults) modlib.table.add_all(playerdata[playername], defaults)
for _, callback in ipairs(playerdata_functions) do for _, callback in ipairs(playerdata_functions) do callback(playerdata[playername]) end
callback(playerdata[playername])
end
end
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 end
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
function get_color(player) function get_color(player)
local sender_color = player:get_properties().nametag_color local sender_color = player:get_properties().nametag_color
if sender_color then if sender_color then
sender_color = minetest.rgba(sender_color.r, sender_color.g, sender_color.b) sender_color = minetest.rgba(sender_color.r, sender_color.g, sender_color.b)
else else sender_color = "#FFFFFF" end
sender_color = "#FFFFFF"
end
return sender_color return sender_color
end end
function get_color_table(player) function get_color_table(player)
local sender_color = player:get_properties().nametag_color local sender_color = player:get_properties().nametag_color
return sender_color or { r = 255, g = 255, b = 255 } return sender_color or { r = 255, g = 255, b = 255 }
end end
--! deprecated in favor of modlib.minetest.colorspec
function get_color_int(player) function get_color_int(player)
local sender_color = player:get_properties().nametag_color local sender_color = player:get_properties().nametag_color
if sender_color then if sender_color then
sender_color = sender_color.b + (sender_color.g * 256) + (sender_color.r * 256 * 256) sender_color = sender_color.b + sender_color.g * 0x100 + sender_color.r * 0x10000
else else sender_color = 0xFFFFFF end
sender_color = 0xFFFFFF
end
return sender_color return sender_color
end end
minetest.register_on_prejoinplayer( minetest.register_on_joinplayer(function(player)
function(name, ip)
if forbidden_names[name] then
return 'The name "' .. name .. '" is not allowed as a player name'
end
end
)
minetest.register_on_joinplayer(
function(player)
local playername = player:get_player_name() local playername = player:get_player_name()
create_player_data(playername) create_player_data(playername)
init_player_data(playername) init_player_data(playername)
end end)
) minetest.register_on_leaveplayer(function(player)
minetest.register_on_leaveplayer(
function(player)
local playername = player:get_player_name() local playername = player:get_player_name()
delete_player_data(playername) delete_player_data(playername)
end end)
)
function datatable(table, default) function datatable(table, default)
table = table or {} table = table or {}