summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api.lua33
-rw-r--r--init.lua13
-rw-r--r--sfinv_page.lua55
-rw-r--r--skin_meta_api.lua56
-rw-r--r--skinlist.lua22
5 files changed, 119 insertions, 60 deletions
diff --git a/api.lua b/api.lua
index 6037d75..b77c7c4 100644
--- a/api.lua
+++ b/api.lua
@@ -1,18 +1,25 @@
-- get current skin
skins.get_player_skin = function(player)
local skin = player:get_attribute("skin")
- if not skins.textures[skin] then
- skin = skins.default
- end
- return skin
+ return skins.get(skin) or skins.get(skins.default)
end
-- Set skin
skins.set_player_skin = function(player, skin)
- if skin == skins.default then
- 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:get_meta("_key")
+
+ if skin_key == skins.default then
+ skin_key = ""
end
- player:set_attribute("skin", skin)
+
+ player:set_attribute("skin", skin_key)
skins.update_player_skin(player)
end
@@ -20,7 +27,7 @@ end
skins.update_player_skin = function(player)
local skin = skins.get_player_skin(player)
player:set_properties({
- textures = {skins.textures[skin]},
+ textures = {skin:get_texture()},
})
end
@@ -28,13 +35,3 @@ end
minetest.register_on_joinplayer(function(player)
skins.update_player_skin(player)
end)
-
--- 3d_armor compatibility
-if minetest.global_exists("armor") then
- armor.get_player_skin = function(self, name)
- return skins.get_player_skin(minetest.get_player_by_name(name))
- end
- armor.get_preview = function(self, name)
- return skins.preview[skins.get_player_skin(minetest.get_player_by_name(name))]
- end
-end
diff --git a/init.lua b/init.lua
index 953e1b6..93a16ca 100644
--- a/init.lua
+++ b/init.lua
@@ -9,6 +9,7 @@ skins = {}
skins.modpath = minetest.get_modpath(minetest.get_current_modname())
skins.default = "character_1"
+dofile(skins.modpath.."skin_meta_api.lua")
dofile(skins.modpath.."/api.lua")
dofile(skins.modpath.."/skinlist.lua")
@@ -20,3 +21,15 @@ end
if minetest.get_modpath("sfinv") then
dofile(skins.modpath.."/sfinv_page.lua")
end
+
+-- 3d_armor compatibility
+if minetest.global_exists("armor") then
+ armor.get_player_skin = function(self, name)
+ local skin = skins.get_player_skin(minetest.get_player_by_name(name))
+ return skin:get_meta("_key") --3d_armor adds a ".png" but it should be compatible in most cases
+ end
+ armor.get_preview = function(self, name)
+ local skin = skins.get_player_skin(minetest.get_player_by_name(name))
+ return skin:get_meta("preview")
+ end
+end
diff --git a/sfinv_page.lua b/sfinv_page.lua
index 8b40d99..1155a71 100644
--- a/sfinv_page.lua
+++ b/sfinv_page.lua
@@ -6,16 +6,13 @@ else
end
local dropdown_values = {}
-local skins_reftab = {}
-local skins_reftab_byskin = {}
+--local skins_reftab = {} --skins.list
-- collect skins data
local total_pages = 1
for i, skin in ipairs(skins.list) do
- local page = math.floor((i-1) / 16)+1
- local index_p = (i-1)%16+1
- skins_reftab[i] = { index = i, page = page, index_p = index_p, skin = skin }
- skins_reftab_byskin[skin] = skins_reftab[i]
+ skin:set_meta("inv_page", math.floor((i-1) / 16)+1)
+ skin:set_meta("inv_page_index", (i-1)%16+1)
total_pages = page
end
@@ -25,37 +22,37 @@ local function get_formspec(player, context)
local skin = skins.get_player_skin(player)
-- overview page
- local formspec = "image[0,.75;1,2;"..skins.preview[skin].."]"
+ local formspec = "image[0,.75;1,2;"..skin:get_preview().."]"
.."label[6,.5;"..S("Raw texture")..":]"
- .."image[6,1;2,1;"..skins.textures[skin].."]"
+ .."image[6,1;2,1;"..skin:get_texture().."]"
- local meta = skins.meta[skin]
- if meta then
- if meta.name ~= "" then
- formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(meta.name).."]"
- end
- if meta.author ~= "" then
- formspec = formspec.."label[2,1;"..S("Author")..": "..minetest.formspec_escape(meta.author).."]"
- end
- if meta.license ~= "" then
- formspec = formspec.."label[2,1.5;"..S("License")..": "..minetest.formspec_escape(meta.license).."]"
- end
- if meta.description ~= "" then --what's that??
- formspec = formspec.."label[2,2;"..S("Description")..": "..minetest.formspec_escape(meta.description).."]"
- end
+ local m_name = skin:get_meta_string("name")
+ local m_author = skin:get_meta_string("author")
+ local m_license = skin:get_meta_string("license")
+ if m_name ~= "" then
+ formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
end
+ if m_author ~= "" then
+ formspec = formspec.."label[2,1;"..S("Author")..": "..minetest.formspec_escape(m_author).."]"
+ end
+ if m_license ~= "" then
+ formspec = formspec.."label[2,1.5;"..S("License")..": "..minetest.formspec_escape(m_license).."]"
+ end
+
local page = 1
if context.skins_page then
page = context.skins_page
- elseif skins_reftab_byskin[skin] then
- page = skins_reftab_byskin[skin].page
+ else
+ page = skin:get_meta("inv_page") or 1
end
for i = (page-1)*16+1, page*16 do
- if not skins_reftab[i] then
+ local skin = skins.list[i]
+ if not skin then
break
end
- local index_p = skins_reftab[i].index_p
+
+ local index_p = skin:get_meta("inv_page_index")
local x = (index_p-1) % 8
local y
if index_p > 8 then
@@ -64,8 +61,8 @@ local function get_formspec(player, context)
y = 3.2
end
formspec = formspec.."image_button["..x..","..y..";1,2;"..
- skins.preview[skins_reftab[i].skin]..";skins_set$"..i..";]"..
- "tooltip[skins_set$"..i..";"..minetest.formspec_escape(skins.meta[skins_reftab[i].skin].name).."]"
+ skin:get_preview()..";skins_set$"..i..";]"..
+ "tooltip[skins_set$"..i..";"..minetest.formspec_escape(skin:get_meta_string("name")).."]"
end
local page_prev = page - 1
local page_next = page + 1
@@ -100,7 +97,7 @@ sfinv.register_page("skins:overview", {
for field, _ in pairs(fields) do
local current = string.split(field, "$", 2)
if current[1] == "skins_set" then
- skins.set_player_skin(player, skins_reftab[tonumber(current[2])].skin)
+ skins.set_player_skin(player, skins.list[tonumber(current[2])])
sfinv.set_player_inventory_formspec(player)
return
elseif current[1] == "skins_page" then
diff --git a/skin_meta_api.lua b/skin_meta_api.lua
new file mode 100644
index 0000000..9e681c0
--- /dev/null
+++ b/skin_meta_api.lua
@@ -0,0 +1,56 @@
+skins.meta = {}
+
+local skin_class = {}
+skin_class.__index = skin_class
+-----------------------
+-- Class methods
+-----------------------
+-- constructor
+function skins.new(key, object)
+ assert(key, 'Unique skins key required, like "character_1"')
+ local self = object or {}
+ setmetatable(self, skin_class)
+ self.__index = skin_class
+
+ self._key, key)
+ self._sort_id = 0
+ skins.meta[key]
+ return self
+end
+
+-- getter
+function skins.get(key)
+ return skins.meta[key]
+end
+
+-- Skin methods
+-- In this implementation it is just access to attrubutes wrapped
+-- but this way allow to redefine the functionality for more complex skins provider
+function skin_class:set_meta(key, value)
+ self[key] = value
+end
+
+function skin_class:get_meta(key)
+ return self[key]
+end
+
+function skin_class:get_meta_string(key)
+ return tostring(self[key] or "")
+end
+
+
+function skin_class:set_texture(value)
+ self._texture = value
+end
+
+function skin_class:get_texture()
+ return self._texture
+end
+
+function skin_class:set_preview(value)
+ self._preview = value
+end
+
+function skin_class:get_preview()
+ return self._preview or "player.png"
+end
diff --git a/skinlist.lua b/skinlist.lua
index e050497..da59a47 100644
--- a/skinlist.lua
+++ b/skinlist.lua
@@ -1,7 +1,4 @@
skins.list = {}
-skins.textures = {}
-skins.meta = {}
-skins.preview = {}
local skins_dir_list = minetest.get_dir_list(skins.modpath.."/textures")
local unsorted_skinslist = {}
@@ -10,26 +7,25 @@ for _, fn in pairs(skins_dir_list) do
nameparts = string.gsub(fn, "[.]", "_"):split("_")
local id = nameparts[2]
local name = "character_"..id
+ local skin_obj = skins.get(name) or skins.new(new)
if nameparts[3] == "preview" then
- skins.preview[name] = fn
+ skin_obj:set_preview(fn)
else
local file = io.open(skins.modpath.."/meta/"..name..".txt", "r")
if file then
local data = string.split(file:read("*all"), "\n", 3)
file:close()
- table.insert(unsorted_skinslist, {id = tonumber(id) or id, name = name})
- skins.textures[name] = fn
- skins.meta[name] = {}
- skins.meta[name].name = data[1]
- skins.meta[name].author = data[2]
- skins.meta[name].license = data[3]
- skins.meta[name].description = "" --what's that??
+ skin_obj:set_texture(fn)
+ skin_obj:set_meta("_sort_id", tonumber(id))
+ skin_obj:set_meta("name", data[1])
+ skin_obj:set_meta("author", data[2])
+ skin_obj:set_meta("license", data[3])
end
end
end
end
-table.sort(unsorted_skinslist, function(a,b) return a.id < b.id end)
+table.sort(unsorted_skinslist, function(a,b) return a:get_meta("_sort_id") < b:get_meta("_sort_id") end)
for _,v in ipairs(unsorted_skinslist) do
- table.insert(skins.list, v.name)
+ table.insert(skins.list, v)
end