Merge pull request #2 from bell07/pr_private_skins

Rework private skins handling
This commit is contained in:
bell07 2018-01-07 20:38:34 +01:00 committed by GitHub
commit c6ee5c2d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 16 deletions

20
API.md
View File

@ -16,12 +16,14 @@ skins.update_player_skin(player)
``` ```
## skins.get_skinlist(assignment, select_unassigned) ## 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: ## skins.get_skinlist_for_player(playername)
- "player:"..playername - Skins directly assigned to a player 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) ## skins.new(key, object)
@ -68,11 +70,17 @@ Add a meta information to the skin object
Note: the information is not stored, therefore should be filled each time during skins registration Note: the information is not stored, therefore should be filled each time during skins registration
## skin:get_meta(key) ## 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 - 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. 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) ## 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)
Returns whether this skin is applicable for player "playername" or not, like private skins

11
api.lua
View File

@ -7,18 +7,19 @@ 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_key == skins.default then if skin_obj:is_applicable_for_player(player:get_player_name()) then
skin_key = "" 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 end
player:set_attribute("skinsdb:skin_key", skin_key)
end end
-- update visuals -- update visuals

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,10 @@ 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 assigned_player == nil or
assigned_player == playername or
assigned_player == true
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,28 @@ 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) and skin:get_meta("in_inventory_list") ~= false 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
-- 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

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()