From 7df6363aa0247068205a09815721164813849b19 Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Sun, 7 Jan 2018 14:50:22 +0100 Subject: [PATCH 1/3] 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() --- API.md | 6 +++++- api.lua | 11 ++++++----- sfinv_page.lua | 2 +- skin_meta_api.lua | 5 +++++ skinlist.lua | 23 ++++++++++++++++++++--- unified_inventory_page.lua | 2 +- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/API.md b/API.md index 0e6b293..ad0248b 100644 --- a/API.md +++ b/API.md @@ -72,7 +72,11 @@ The next metadata keys are usually filled - name - A name for the skin - author - The skin author - 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) 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 diff --git a/api.lua b/api.lua index 549e79e..d822005 100644 --- a/api.lua +++ b/api.lua @@ -7,18 +7,19 @@ end -- Assign skin to player function skins.assign_player_skin(player, skin) local skin_obj - local skin_key if type(skin) == "string" then skin_obj = skins.get(skin) or skins.get(skins.default) else skin_obj = skin end - skin_key = skin_obj:get_key() - if skin_key == skins.default then - skin_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 + skin_key = "" + end + player:set_attribute("skinsdb:skin_key", skin_key) end - player:set_attribute("skinsdb:skin_key", skin_key) end -- update visuals diff --git a/sfinv_page.lua b/sfinv_page.lua index c62e7e7..1d63163 100644 --- a/sfinv_page.lua +++ b/sfinv_page.lua @@ -84,7 +84,7 @@ sfinv.register_page("skins:overview", { title = "Skins", get = function(self, player, context) -- 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 for i, skin in ipairs(context.skins_list ) do local page = math.floor((i-1) / 16)+1 diff --git a/skin_meta_api.lua b/skin_meta_api.lua index 5219e1d..1e3bd99 100644 --- a/skin_meta_api.lua +++ b/skin_meta_api.lua @@ -69,3 +69,8 @@ function skin_class:set_skin(player) textures = {self:get_texture()}, }) 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 diff --git a/skinlist.lua b/skinlist.lua index 5d29fe4..889b531 100644 --- a/skinlist.lua +++ b/skinlist.lua @@ -4,7 +4,7 @@ local skins_dir_list = minetest.get_dir_list(skins.modpath.."/textures") for _, fn in pairs(skins_dir_list) do 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 tonumber(nameparts[2]) == nil then --default skin character.png sort_id = 5000 @@ -16,8 +16,9 @@ for _, fn in pairs(skins_dir_list) do is_preview = (nameparts[3] == "preview") end elseif nameparts[1] == "player" then - assignment = "player:"..nameparts[2] + assignment = "player:"..nameparts[2] --TODO: remove all assignment handling name = "player_"..nameparts[2] + playername = nameparts[2] if tonumber(nameparts[3]) then sort_id = tonumber(nameparts[3]) is_preview = (nameparts[4] == "preview") @@ -38,6 +39,9 @@ for _, fn in pairs(skins_dir_list) do if assignment then skin_obj:set_meta("assignment", assignment) end + if playername then + skin_obj:set_meta("playername", playername) + end local file = io.open(skins.modpath.."/meta/"..name..".txt", "r") if file then local data = string.split(file:read("*all"), "\n", 3) @@ -52,8 +56,9 @@ for _, fn in pairs(skins_dir_list) do 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) + minetest.log("deprecated", "skins.get_skinlist() is deprecated. Use skins.get_skinlist_for_player() instead") local skinslist = {} for _, skin in pairs(skins.meta) do 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) return skinslist 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 diff --git a/unified_inventory_page.lua b/unified_inventory_page.lua index 58e8ae6..ebac196 100644 --- a/unified_inventory_page.lua +++ b/unified_inventory_page.lua @@ -7,7 +7,7 @@ end local dropdown_values = {} local skins_reftab = {} -local skins_list = skins.get_skinlist() +local skins_list = skins.get_skinlist_for_player() --public only unified_inventory.register_page("skins", { get_formspec = function(player) local name = player:get_player_name() From 429874698ea5176e15a3b5703ac734b5b123d842 Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Sun, 7 Jan 2018 19:09:52 +0100 Subject: [PATCH 2/3] Add skins.get_skinlist_with_meta(key, value) and small adjustments --- API.md | 14 ++++++++------ skin_meta_api.lua | 4 +++- skinlist.lua | 13 +++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index ad0248b..f94213b 100644 --- a/API.md +++ b/API.md @@ -16,12 +16,14 @@ skins.update_player_skin(player) ``` ## skins.get_skinlist(assignment, select_unassigned) -Get a list of skin objects matching to the assignment. +Obsolete - use get_skinlist_for_player() or get_skinlist_with_meta() instead -Supported assignments: - - "player:"..playername - Skins directly assigned to a player +## skins.get_skinlist_for_player(playername) +Get all allowed skins for player. All public and all player's private skins. If playername not given only public skins returned -select_unassigned - Select all skins without assignment too (usually the "character_*" skins) +## skins.get_skinlist_with_meta(key, value) +Get all skins with metadata key is set to value. Example: +skins.get_skinlist_with_meta("playername", playername) - Get all private skins (w.o. public) for playername ## skins.new(key, object) @@ -73,10 +75,10 @@ The next metadata keys are usually filled - author - The skin author - license - THe skin texture license - assignment - (obsolete) is "player:playername" in case the skin is assigned to be privat for a player - - playername - Player assignment for private skin + - playername - Player assignment for private skin. Set false for skins not usable by all players (like NPC-Skins), true or nothing for all player skins ## skin:get_meta_string(key) 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 +Returns whether this skin is applicable for player "playername" or not, like private skins diff --git a/skin_meta_api.lua b/skin_meta_api.lua index 1e3bd99..5ff17f1 100644 --- a/skin_meta_api.lua +++ b/skin_meta_api.lua @@ -72,5 +72,7 @@ end function skin_class:is_applicable_for_player(playername) local assigned_player = self:get_meta("playername") - return not assigned_player or assigned_player == playername + return assigned_player == nil or + assigned_player == playername or + assigned_player == true end diff --git a/skinlist.lua b/skinlist.lua index 889b531..0a06f5d 100644 --- a/skinlist.lua +++ b/skinlist.lua @@ -82,3 +82,16 @@ function skins.get_skinlist_for_player(playername) table.sort(skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end) return skinslist end + +-- Get skinlist selected by metadata +function skins.get_skinlist_with_meta(key, value) + assert(key, "key parameter for skins.get_skinlist_with_meta() missed") + local skinslist = {} + for _, skin in pairs(skins.meta) do + if skin:get_meta(key) == value 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 From 6a09746b903d63302d9dd339877bf450adb3a52c Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Sun, 7 Jan 2018 20:30:27 +0100 Subject: [PATCH 3/3] Support for in_inventory_list attribute to hide usable skins in inventory API-documentation update --- API.md | 4 +++- skinlist.lua | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index f94213b..193f824 100644 --- a/API.md +++ b/API.md @@ -70,12 +70,14 @@ Add a meta information to the skin object Note: the information is not stored, therefore should be filled each time during skins registration ## skin:get_meta(key) -The next metadata keys are usually filled +The next metadata keys are filled or/and used interally in skinsdb framework - name - A name for the skin - author - The skin author - license - THe skin texture license - assignment - (obsolete) is "player:playername" in case the skin is assigned to be privat for a player - playername - Player assignment for private skin. Set false for skins not usable by all players (like NPC-Skins), true or nothing for all player skins + - in_inventory_list - If set to false the skin is not visible in inventory skins selection but can be still applied to the player + - _sort_id - Thi skins lists are sorted by this field for output (internal key) ## skin:get_meta_string(key) Same as get_meta() but does return "" instead of nil if the meta key does not exists diff --git a/skinlist.lua b/skinlist.lua index 0a06f5d..5ed5e12 100644 --- a/skinlist.lua +++ b/skinlist.lua @@ -75,7 +75,7 @@ end function skins.get_skinlist_for_player(playername) local skinslist = {} for _, skin in pairs(skins.meta) do - if skin:is_applicable_for_player(playername) then + if skin:is_applicable_for_player(playername) and skin:get_meta("in_inventory_list") ~= false then table.insert(skinslist, skin) end end