rolling-6

This commit is contained in:
Lars Mueller 2020-02-25 20:47:42 +01:00
parent 0ba259425e
commit b0b68c39bd
2 changed files with 50 additions and 44 deletions

@ -1,4 +1,4 @@
# modlib # Modding Library (`modlib`)
Multipurpose Minetest Modding Library Multipurpose Minetest Modding Library

@ -1,44 +1,44 @@
forbidden_names = {"me"} forbidden_names = {"me"}
register_forbidden_name=function(name) function register_forbidden_name(name)
forbidden_names[name] = true forbidden_names[name] = true
end end
unregister_forbidden_name=function(name) function unregister_forbidden_name(name)
forbidden_names[name] = nil forbidden_names[name] = nil
end end
playerdata = {} playerdata = {}
defaults = {} defaults = {}
playerdata_functions = {} playerdata_functions = {}
delete_player_data=function(playername) function delete_player_data(playername)
playerdata[playername] = nil playerdata[playername] = nil
end end
create_player_data=function(playername) function create_player_data(playername)
playerdata[playername] = {} playerdata[playername] = {}
end end
init_player_data=function(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]) callback(playerdata[playername])
end end
end end
get_player_data=function(playername) function get_player_data(playername)
return playerdata[playername] return playerdata[playername]
end end
get_property =function(playername, propertyname) function get_property(playername, propertyname)
return get_player_data(playername)[propertyname] return get_player_data(playername)[propertyname]
end end
set_property =function(playername, propertyname, propertyvalue) function set_property(playername, propertyname, propertyvalue)
get_player_data(playername)[propertyname] = propertyvalue get_player_data(playername)[propertyname] = propertyvalue
end end
get_property_default =function(propertyname) function get_property_default(propertyname)
return defaults[propertyname] return defaults[propertyname]
end end
set_property_default =function(propertyname, defaultvalue) function set_property_default(propertyname, defaultvalue)
defaults[propertyname] = defaultvalue defaults[propertyname] = defaultvalue
end end
add_playerdata_function=function(callback) function add_playerdata_function(callback)
table.insert(playerdata_functions, callback) table.insert(playerdata_functions, callback)
end end
get_color=function(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)
@ -47,11 +47,11 @@ get_color=function(player)
end end
return sender_color return sender_color
end end
get_color_table=function(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
get_color_int=function(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 * 256) + (sender_color.r * 256 * 256)
@ -61,19 +61,25 @@ get_color_int=function(player)
return sender_color return sender_color
end end
minetest.register_on_prejoinplayer(function(name, ip) minetest.register_on_prejoinplayer(
function(name, ip)
if forbidden_names[name] then if forbidden_names[name] then
return "The name '"..name.."' is already used in the game and thus not allowed as playername." return 'The name "' .. name .. '" is not allowed as a player name'
end end
end ) end
)
minetest.register_on_joinplayer(function(player) 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
)