-
Notifications
You must be signed in to change notification settings - Fork 6
/
skinMethods.lua
104 lines (84 loc) · 2.79 KB
/
skinMethods.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
-- Returns:
-- initSkin(player)
-- changeSkin(playerName, skin)
-- updateSkin(player)
local wardrobe = wardrobe or {};
--- Methods for initializing/changing/updating skin. Valid values are keys
-- from the SKIN_CHANGE_METHODS table (below). nil means use the default
-- method.
local SKIN_CHANGE_METHOD = '3d_armor';
local playerMesh = "character.b3d";
do -- autodetect version of player mesh used by default
if default and default.registered_player_models then
local haveCharName = false; -- 'character.*' has priority
local name = nil;
local nNames = 0;
for k in pairs(default.registered_player_models) do
if string.find(k, "^character\\.[^\\.]+$") then
if haveCharName then nNames = 2; break; end;
name = k;
nNames = 1;
haveCharName = true;
elseif not haveCharName then
name = k;
nNames = nNames + 1;
end;
end;
if nNames == 1 then playerMesh = name; end;
end;
end;
local function changeWardrobeSkin(playerName, skin)
local player = minetest.get_player_by_name(playerName);
if not player then
error("unknown player '"..playerName.."'");
end;
if skin and not wardrobe.skinNames[skin] then
error("unknown skin '"..skin.."'");
end;
wardrobe.playerSkins[playerName] = skin;
wardrobe.storage.savePlayerSkins();
end;
local function defaultUpdateSkin(player)
local playerName = player:get_player_name();
if not playerName or playerName == "" then return; end;
local skin = wardrobe.playerSkins[playerName];
if not skin or not wardrobe.skinNames[skin] then return; end;
player:set_properties(
{
visual = "mesh",
visual_size = { x = 1, y = 1 },
mesh = playerMesh,
textures = { skin }
});
end;
--- Method for updating the player skin, IF the dependent mod is enabled.
local SKIN_CHANGE_METHODS =
{
default =
{
required_mods = {},
initSkin = defaultUpdateSkin,
changeSkin = changeWardrobeSkin,
updateSkin = defaultUpdateSkin
},
["3d_armor"] =
{
required_mods = { '3d_armor' },
initSkin = nil,
changeSkin = function(playerName, skin)
changeWardrobeSkin(playerName, skin);
armor.textures[playerName].skin = skin;
end,
updateSkin = function(player)
armor:update_player_visuals(player);
end,
},
};
local methods = SKIN_CHANGE_METHODS[SKIN_CHANGE_METHOD];
if methods then
for _, mod in ipairs(methods.required_mods) do
if not minetest.get_modpath(mod) then methods = nil; break; end;
end;
end;
if not methods then methods = SKIN_CHANGE_METHODS.default; end;
return methods.initSkin, methods.changeSkin, methods.updateSkin;