API enhancements, player-attribute according naming convention

This commit is contained in:
Alexander Weber 2017-06-19 16:36:53 +02:00
parent 57b815f91f
commit 897ac2c97b
2 changed files with 18 additions and 11 deletions

22
api.lua
View File

@ -1,11 +1,11 @@
-- get current skin -- get current skin
skins.get_player_skin = function(player) function skins.get_player_skin(player)
local skin = player:get_attribute("skin") local skin = player:get_attribute("skinsdb:skin_key")
return skins.get(skin) or skins.get(skins.default) return skins.get(skin) or skins.get(skins.default)
end end
-- Set skin -- Assign skin to player
skins.set_player_skin = function(player, skin) function skins.assign_player_skin(player, skin)
local skin_obj local skin_obj
local skin_key local skin_key
if type(skin) == "string" then if type(skin) == "string" then
@ -13,18 +13,22 @@ skins.set_player_skin = function(player, skin)
else else
skin_obj = skin skin_obj = skin
end end
skin_key = skin:get_meta("_key") 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("skin", skin_key)
skins.update_player_skin(player)
end end
-- update visuals -- update visuals
skins.update_player_skin = function(player) function skins.update_player_skin(player)
local skin = skins.get_player_skin(player) local skin = skins.get_player_skin(player)
skin:set_skin(player) skin:set_skin(player)
end end
-- Assign and update
function skins.set_player_skin(player, skin)
skins.assign_player_skin(player, skin)
skins.update_player_skin(player)
end

View File

@ -26,6 +26,10 @@ end
-- Skin methods -- Skin methods
-- In this implementation it is just access to attrubutes wrapped -- In this implementation it is just access to attrubutes wrapped
-- but this way allow to redefine the functionality for more complex skins provider -- but this way allow to redefine the functionality for more complex skins provider
function skin_class:get_key()
return self._key
end
function skin_class:set_meta(key, value) function skin_class:set_meta(key, value)
self[key] = value self[key] = value
end end
@ -35,10 +39,9 @@ function skin_class:get_meta(key)
end end
function skin_class:get_meta_string(key) function skin_class:get_meta_string(key)
return tostring(self[key] or "") return tostring(self:get_meta(key) or "")
end end
function skin_class:set_texture(value) function skin_class:set_texture(value)
self._texture = value self._texture = value
end end