Rework private skins handling

-add skin:is_applicable_for_player(playername) method
- hide private skins in unified_inventory since not supported
- check player assignment in skins.assign_player_skin() + inherited to skins.set_player_skin()
This commit is contained in:
Alexander Weber 2018-01-07 14:50:22 +01:00
parent 71e05bbd68
commit 7df6363aa0
6 changed files with 38 additions and 11 deletions

6
API.md
View File

@ -72,7 +72,11 @@ The next metadata keys are usually filled
- name - A name for the skin - name - A name for the skin
- author - The skin author - author - The skin author
- license - THe skin texture license - license - THe skin texture license
- assignment - is "player:playername" in case the skin is assigned to be privat for a player - assignment - (obsolete) is "player:playername" in case the skin is assigned to be privat for a player
- playername - Player assignment for private skin
## skin:get_meta_string(key) ## skin:get_meta_string(key)
Same as get_meta() but does return "" instead of nil if the meta key does not exists Same as get_meta() but does return "" instead of nil if the meta key does not exists
## skin:is_applicable_for_player(playername)
Check if a skin is applicable for the player "playername". Ususally the private skins could be applied to the player only

View File

@ -7,19 +7,20 @@ end
-- Assign skin to player -- Assign skin to player
function skins.assign_player_skin(player, skin) function skins.assign_player_skin(player, skin)
local skin_obj local skin_obj
local skin_key
if type(skin) == "string" then if type(skin) == "string" then
skin_obj = skins.get(skin) or skins.get(skins.default) skin_obj = skins.get(skin) or skins.get(skins.default)
else else
skin_obj = skin skin_obj = skin
end end
skin_key = skin_obj:get_key()
if skin_obj:is_applicable_for_player(player:get_player_name()) then
local skin_key = skin_obj:get_key()
if skin_key == skins.default then if skin_key == skins.default then
skin_key = "" skin_key = ""
end end
player:set_attribute("skinsdb:skin_key", skin_key) player:set_attribute("skinsdb:skin_key", skin_key)
end end
end
-- update visuals -- update visuals
function skins.update_player_skin(player) function skins.update_player_skin(player)

View File

@ -84,7 +84,7 @@ sfinv.register_page("skins:overview", {
title = "Skins", title = "Skins",
get = function(self, player, context) get = function(self, player, context)
-- collect skins data -- collect skins data
context.skins_list = skins.get_skinlist("player:"..player:get_player_name(), true) context.skins_list = skins.get_skinlist_for_player(player:get_player_name())
context.total_pages = 1 context.total_pages = 1
for i, skin in ipairs(context.skins_list ) do for i, skin in ipairs(context.skins_list ) do
local page = math.floor((i-1) / 16)+1 local page = math.floor((i-1) / 16)+1

View File

@ -69,3 +69,8 @@ function skin_class:set_skin(player)
textures = {self:get_texture()}, textures = {self:get_texture()},
}) })
end end
function skin_class:is_applicable_for_player(playername)
local assigned_player = self:get_meta("playername")
return not assigned_player or assigned_player == playername
end

View File

@ -4,7 +4,7 @@ local skins_dir_list = minetest.get_dir_list(skins.modpath.."/textures")
for _, fn in pairs(skins_dir_list) do for _, fn in pairs(skins_dir_list) do
local nameparts = string.gsub(fn, "[.]", "_"):split("_") local nameparts = string.gsub(fn, "[.]", "_"):split("_")
local name, sort_id, assignment, is_preview local name, sort_id, assignment, is_preview, playername
if nameparts[1] == "character" then if nameparts[1] == "character" then
if tonumber(nameparts[2]) == nil then --default skin character.png if tonumber(nameparts[2]) == nil then --default skin character.png
sort_id = 5000 sort_id = 5000
@ -16,8 +16,9 @@ for _, fn in pairs(skins_dir_list) do
is_preview = (nameparts[3] == "preview") is_preview = (nameparts[3] == "preview")
end end
elseif nameparts[1] == "player" then elseif nameparts[1] == "player" then
assignment = "player:"..nameparts[2] assignment = "player:"..nameparts[2] --TODO: remove all assignment handling
name = "player_"..nameparts[2] name = "player_"..nameparts[2]
playername = nameparts[2]
if tonumber(nameparts[3]) then if tonumber(nameparts[3]) then
sort_id = tonumber(nameparts[3]) sort_id = tonumber(nameparts[3])
is_preview = (nameparts[4] == "preview") is_preview = (nameparts[4] == "preview")
@ -38,6 +39,9 @@ for _, fn in pairs(skins_dir_list) do
if assignment then if assignment then
skin_obj:set_meta("assignment", assignment) skin_obj:set_meta("assignment", assignment)
end end
if playername then
skin_obj:set_meta("playername", playername)
end
local file = io.open(skins.modpath.."/meta/"..name..".txt", "r") local file = io.open(skins.modpath.."/meta/"..name..".txt", "r")
if file then if file then
local data = string.split(file:read("*all"), "\n", 3) local data = string.split(file:read("*all"), "\n", 3)
@ -52,8 +56,9 @@ for _, fn in pairs(skins_dir_list) do
end end
end end
-- get skinlist. If assignment given ("mod:wardrobe" or "player:bell07") select skins matches the assignment. select_unassigned selects the skins without any assignment too -- (obsolete) get skinlist. If assignment given ("mod:wardrobe" or "player:bell07") select skins matches the assignment. select_unassigned selects the skins without any assignment too
function skins.get_skinlist(assignment, select_unassigned) function skins.get_skinlist(assignment, select_unassigned)
minetest.log("deprecated", "skins.get_skinlist() is deprecated. Use skins.get_skinlist_for_player() instead")
local skinslist = {} local skinslist = {}
for _, skin in pairs(skins.meta) do for _, skin in pairs(skins.meta) do
if not assignment or if not assignment or
@ -65,3 +70,15 @@ function skins.get_skinlist(assignment, select_unassigned)
table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end) table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
return skinslist return skinslist
end end
-- Get skinlist for player. If no player given, public skins only selected
function skins.get_skinlist_for_player(playername)
local skinslist = {}
for _, skin in pairs(skins.meta) do
if skin:is_applicable_for_player(playername) then
table.insert(skinslist, skin)
end
end
table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
return skinslist
end

View File

@ -7,7 +7,7 @@ end
local dropdown_values = {} local dropdown_values = {}
local skins_reftab = {} local skins_reftab = {}
local skins_list = skins.get_skinlist() local skins_list = skins.get_skinlist_for_player() --public only
unified_inventory.register_page("skins", { unified_inventory.register_page("skins", {
get_formspec = function(player) get_formspec = function(player)
local name = player:get_player_name() local name = player:get_player_name()