forked from minetest-mods/skinsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskin_meta_api.lua
153 lines (132 loc) · 3.61 KB
/
skin_meta_api.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
skins.meta = {}
local skin_class = {}
skin_class.__index = skin_class
skins.skin_class = 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] = self
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:get_key()
return self._key
end
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:get_meta(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
function skin_class:apply_skin_to_player(player)
local function concat_texture(base, ext)
if base == "blank.png" then
return ext
elseif ext == "blank.png" then
return base
else
return base .. "^" .. ext
end
end
local playername = player:get_player_name()
local ver = self:get_meta("format") or "1.0"
if minetest.global_exists("player_api") then
-- Minetest-5 compatible
player_api.set_model(player, "skinsdb_3d_armor_character_5.b3d")
else
-- Minetest-0.4 compatible
default.player_set_model(player, "skinsdb_3d_armor_character.b3d")
end
local v10_texture = "blank.png"
local v18_texture = "blank.png"
local armor_texture = "blank.png"
local wielditem_texture = "blank.png"
if ver == "1.8" then
v18_texture = self:get_texture()
else
v10_texture = self:get_texture()
end
-- Support for clothing
if skins.clothing_loaded and clothing.player_textures[playername] then
local cape = clothing.player_textures[playername].cape
local layers = {}
for k, v in pairs(clothing.player_textures[playername]) do
if k ~= "skin" and k ~= "cape" then
table.insert(layers, v)
end
end
local overlay = table.concat(layers, "^")
v10_texture = concat_texture(v10_texture, cape)
v18_texture = concat_texture(v18_texture, overlay)
end
-- Support for armor
if skins.armor_loaded then
local armor_textures = armor.textures[playername]
if armor_textures then
armor_texture = concat_texture(armor_texture, armor_textures.armor)
wielditem_texture = concat_texture(wielditem_texture, armor_textures.wielditem)
end
end
if minetest.global_exists("player_api") then
-- Minetest-5 compatible
player_api.set_textures(player, {
v10_texture,
v18_texture,
armor_texture,
wielditem_texture,
})
else
-- Minetest-0.4 compatible
default.player_set_textures(player, {
v10_texture,
v18_texture,
armor_texture,
wielditem_texture,
})
end
player:set_properties({
visual_size = {
x = self:get_meta("visual_size_x") or 1,
y = self:get_meta("visual_size_y") or 1
}
})
end
function skin_class:set_skin(player)
-- The set_skin is used on skins selection
-- This means the method could be redefined to start an furmslec
-- See character_creator for example
end
function skin_class:is_applicable_for_player(playername)
local assigned_player = self:get_meta("playername")
return assigned_player == nil or assigned_player == true or
(assigned_player:lower() == playername:lower())
end