2019-03-05 12:40:37 +03:00
|
|
|
-- Skins for MineClone 2
|
2019-03-05 02:11:43 +03:00
|
|
|
|
2021-05-29 17:12:33 +03:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
|
2019-03-05 12:36:53 +03:00
|
|
|
mcl_skins = {
|
2019-03-05 16:19:57 +03:00
|
|
|
skins = {}, list = {}, previews = {}, meta = {}, has_preview = {},
|
2021-05-29 17:12:33 +03:00
|
|
|
modpath = minetest.get_modpath(modname),
|
2019-03-05 02:11:43 +03:00
|
|
|
skin_count = 0, -- counter of _custom_ skins (all skins except character.png)
|
|
|
|
}
|
|
|
|
|
2021-05-29 17:12:33 +03:00
|
|
|
local S = minetest.get_translator(modname)
|
2021-02-18 18:07:37 +03:00
|
|
|
local has_mcl_inventory = minetest.get_modpath("mcl_inventory")
|
2019-03-05 02:11:43 +03:00
|
|
|
|
|
|
|
-- load skin list and metadata
|
2019-03-05 15:50:35 +03:00
|
|
|
local id, f, data, skin = 0
|
2019-03-05 13:43:09 +03:00
|
|
|
|
2019-03-05 02:11:43 +03:00
|
|
|
while true do
|
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
if id == 0 then
|
|
|
|
skin = "character"
|
2019-03-05 16:19:57 +03:00
|
|
|
mcl_skins.has_preview[id] = true
|
2019-03-05 15:50:35 +03:00
|
|
|
else
|
2019-03-05 16:26:44 +03:00
|
|
|
skin = "mcl_skins_character_" .. id
|
|
|
|
local preview = "mcl_skins_player_" .. id
|
2019-03-05 02:11:43 +03:00
|
|
|
|
2019-03-05 16:19:57 +03:00
|
|
|
-- Does skin file exist?
|
2019-03-05 15:50:35 +03:00
|
|
|
f = io.open(mcl_skins.modpath .. "/textures/" .. skin .. ".png")
|
2019-03-05 02:11:43 +03:00
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
-- escape loop if not found
|
|
|
|
if not f then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
f:close()
|
2019-03-05 16:19:57 +03:00
|
|
|
|
|
|
|
-- Does skin preview file exist?
|
|
|
|
local file_preview = io.open(mcl_skins.modpath .. "/textures/" .. preview .. ".png")
|
|
|
|
if file_preview == nil then
|
|
|
|
minetest.log("warning", "[mcl_skins] Player skin #"..id.." does not have preview image (player_"..id..".png)")
|
|
|
|
mcl_skins.has_preview[id] = false
|
|
|
|
else
|
|
|
|
mcl_skins.has_preview[id] = true
|
|
|
|
file_preview:close()
|
|
|
|
end
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
mcl_skins.list[id] = skin
|
2019-03-05 02:11:43 +03:00
|
|
|
|
|
|
|
-- does metadata exist for that skin file ?
|
2019-03-05 16:26:44 +03:00
|
|
|
if id == 0 then
|
|
|
|
metafile = "mcl_skins_character.txt"
|
|
|
|
else
|
|
|
|
metafile = "mcl_skins_character_"..id..".txt"
|
|
|
|
end
|
|
|
|
f = io.open(mcl_skins.modpath .. "/meta/" .. metafile)
|
2019-03-05 02:11:43 +03:00
|
|
|
|
2019-03-05 16:33:16 +03:00
|
|
|
data = nil
|
2019-03-05 02:11:43 +03:00
|
|
|
if f then
|
2021-05-29 17:12:33 +03:00
|
|
|
data = minetest.deserialize("return {" .. f:read("*all") .. "}")
|
2019-03-05 02:11:43 +03:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add metadata to list
|
2019-03-05 12:36:53 +03:00
|
|
|
mcl_skins.meta[skin] = {
|
2019-03-05 02:11:43 +03:00
|
|
|
name = data and data.name or "",
|
|
|
|
author = data and data.author or "",
|
2021-02-25 23:27:14 +03:00
|
|
|
gender = data and data.gender or "",
|
2019-03-05 02:11:43 +03:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
if id > 0 then
|
|
|
|
mcl_skins.skin_count = mcl_skins.skin_count + 1
|
|
|
|
end
|
2019-03-05 02:11:43 +03:00
|
|
|
id = id + 1
|
|
|
|
end
|
|
|
|
|
2021-05-25 13:52:25 +03:00
|
|
|
function mcl_skins.cycle_skin(player)
|
2019-03-06 07:45:16 +03:00
|
|
|
local skin_id = tonumber(player:get_meta():get_string("mcl_skins:skin_id"))
|
2019-03-05 15:59:00 +03:00
|
|
|
if not skin_id then
|
|
|
|
skin_id = 0
|
|
|
|
end
|
|
|
|
skin_id = skin_id + 1
|
|
|
|
if skin_id > mcl_skins.skin_count then
|
|
|
|
skin_id = 0
|
|
|
|
end
|
|
|
|
mcl_skins.set_player_skin(player, skin_id)
|
|
|
|
end
|
|
|
|
|
2021-05-25 13:52:25 +03:00
|
|
|
function mcl_skins.set_player_skin(player, skin_id)
|
2019-03-05 02:11:43 +03:00
|
|
|
if not player then
|
2019-03-05 03:50:51 +03:00
|
|
|
return false
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
local playername = player:get_player_name()
|
2021-05-22 20:58:09 +03:00
|
|
|
local skin, preview
|
2019-03-05 12:36:53 +03:00
|
|
|
if skin_id == nil or type(skin_id) ~= "number" or skin_id < 0 or skin_id > mcl_skins.skin_count then
|
2019-03-05 03:50:51 +03:00
|
|
|
return false
|
|
|
|
elseif skin_id == 0 then
|
|
|
|
skin = "character"
|
|
|
|
preview = "player"
|
2021-02-25 23:27:14 +03:00
|
|
|
mcl_player.player_set_model(player, "mcl_armor_character.b3d")
|
2019-03-05 03:50:51 +03:00
|
|
|
else
|
2019-03-05 16:26:44 +03:00
|
|
|
skin = "mcl_skins_character_" .. tostring(skin_id)
|
2021-02-25 23:27:14 +03:00
|
|
|
local meta = mcl_skins.meta[skin]
|
|
|
|
if meta.gender == "female" then
|
|
|
|
mcl_player.player_set_model(player, "mcl_armor_character_female.b3d")
|
|
|
|
else
|
|
|
|
mcl_player.player_set_model(player, "mcl_armor_character.b3d")
|
|
|
|
end
|
2019-03-05 16:19:57 +03:00
|
|
|
if mcl_skins.has_preview[skin_id] then
|
2019-03-05 16:26:44 +03:00
|
|
|
preview = "mcl_skins_player_" .. tostring(skin_id)
|
2019-03-05 16:19:57 +03:00
|
|
|
else
|
|
|
|
-- Fallback preview image if preview image is missing
|
2019-03-05 16:26:44 +03:00
|
|
|
preview = "mcl_skins_player_dummy"
|
2019-03-05 16:19:57 +03:00
|
|
|
end
|
2019-03-05 03:50:51 +03:00
|
|
|
end
|
2021-05-22 20:58:09 +03:00
|
|
|
--local skin_file = skin .. ".png"
|
2019-03-05 12:36:53 +03:00
|
|
|
mcl_skins.skins[playername] = skin
|
|
|
|
mcl_skins.previews[playername] = preview
|
2019-03-06 07:45:16 +03:00
|
|
|
player:get_meta():set_string("mcl_skins:skin_id", tostring(skin_id))
|
2019-03-05 12:36:53 +03:00
|
|
|
mcl_skins.update_player_skin(player)
|
2021-02-18 18:07:37 +03:00
|
|
|
if has_mcl_inventory then
|
2019-03-05 12:29:49 +03:00
|
|
|
mcl_inventory.update_inventory_formspec(player)
|
|
|
|
end
|
2019-03-05 13:43:09 +03:00
|
|
|
for i=1, #mcl_skins.registered_on_set_skins do
|
|
|
|
mcl_skins.registered_on_set_skins[i](player, skin)
|
|
|
|
end
|
2019-03-05 12:57:57 +03:00
|
|
|
minetest.log("action", "[mcl_skins] Player skin for "..playername.." set to skin #"..skin_id)
|
2019-03-05 03:50:51 +03:00
|
|
|
return true
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
|
2021-05-25 13:52:25 +03:00
|
|
|
function mcl_skins.update_player_skin(player)
|
2019-03-05 02:11:43 +03:00
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local playername = player:get_player_name()
|
2021-04-14 16:46:52 +03:00
|
|
|
mcl_player.player_set_skin(player, mcl_skins.skins[playername] .. ".png", mcl_skins.previews[playername] .. ".png")
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- load player skin on join
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
2019-03-06 07:45:16 +03:00
|
|
|
local skin_id = player:get_meta():get_string("mcl_skins:skin_id")
|
2019-03-05 02:11:43 +03:00
|
|
|
local set_skin
|
|
|
|
-- do we already have a skin in player attributes?
|
2021-05-29 17:12:33 +03:00
|
|
|
if skin_id and skin_id ~= "" then
|
2019-03-05 03:50:51 +03:00
|
|
|
set_skin = tonumber(skin_id)
|
2019-03-05 02:11:43 +03:00
|
|
|
-- otherwise use random skin if not set
|
2019-03-06 09:49:14 +03:00
|
|
|
end
|
|
|
|
if not set_skin then
|
2019-03-05 12:36:53 +03:00
|
|
|
set_skin = math.random(0, mcl_skins.skin_count)
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
2019-03-06 09:49:14 +03:00
|
|
|
local ok = mcl_skins.set_player_skin(player, set_skin)
|
|
|
|
if not ok then
|
|
|
|
set_skin = math.random(0, mcl_skins.skin_count)
|
|
|
|
minetest.log("warning", "[mcl_skins] Player skin for "..name.." not found, falling back to skin #"..set_skin)
|
|
|
|
mcl_skins.set_player_skin(player, set_skin)
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-03-05 13:43:09 +03:00
|
|
|
mcl_skins.registered_on_set_skins = {}
|
|
|
|
|
2021-05-25 13:52:25 +03:00
|
|
|
function mcl_skins.register_on_set_skin(func)
|
2019-03-05 13:43:09 +03:00
|
|
|
table.insert(mcl_skins.registered_on_set_skins, func)
|
|
|
|
end
|
|
|
|
|
2019-03-05 02:11:43 +03:00
|
|
|
-- command to set player skin (usually for custom skins)
|
|
|
|
minetest.register_chatcommand("setskin", {
|
2019-03-14 04:32:35 +03:00
|
|
|
params = S("[<player>] [<skin number>]"),
|
2019-03-05 02:11:43 +03:00
|
|
|
description = S("Select player skin of yourself or another player"),
|
|
|
|
privs = {},
|
|
|
|
func = function(name, param)
|
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
if param == "" and name ~= "" then
|
|
|
|
mcl_skins.show_formspec(name)
|
|
|
|
return true
|
|
|
|
end
|
2019-03-05 02:11:43 +03:00
|
|
|
local playername, skin_id = string.match(param, "([^ ]+) (%d+)")
|
|
|
|
if not playername or not skin_id then
|
|
|
|
skin_id = string.match(param, "(%d+)")
|
|
|
|
if not skin_id then
|
|
|
|
return false, S("Insufficient or wrong parameters")
|
|
|
|
end
|
|
|
|
playername = name
|
|
|
|
end
|
|
|
|
skin_id = tonumber(skin_id)
|
|
|
|
|
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
|
|
|
|
if not player then
|
|
|
|
return false, S("Player @1 not online!", playername)
|
|
|
|
end
|
|
|
|
if name ~= playername then
|
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
if not privs.server then
|
|
|
|
return false, S("You need the “server” privilege to change the skin of other players!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-05 12:36:53 +03:00
|
|
|
local ok = mcl_skins.set_player_skin(player, skin_id)
|
2019-03-05 03:50:51 +03:00
|
|
|
if not ok then
|
2019-03-05 12:36:53 +03:00
|
|
|
return false, S("Invalid skin number! Valid numbers: 0 to @1", mcl_skins.skin_count)
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
2019-03-05 15:50:35 +03:00
|
|
|
local skinfile = "#"..skin_id
|
2019-03-05 02:11:43 +03:00
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
local meta = mcl_skins.meta[mcl_skins.skins[playername]]
|
2019-03-05 16:33:16 +03:00
|
|
|
local your_msg
|
|
|
|
if not meta.name or meta.name == "" then
|
|
|
|
your_msg = S("Your skin has been set to: @1", skinfile)
|
|
|
|
else
|
|
|
|
your_msg = S("Your skin has been set to: @1 (@2)", meta.name, skinfile)
|
|
|
|
end
|
2019-03-05 02:11:43 +03:00
|
|
|
if name == playername then
|
|
|
|
return true, your_msg
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(playername, your_msg)
|
2019-03-05 15:50:35 +03:00
|
|
|
return true, S("Skin of @1 set to: @2 (@3)", playername, meta.name, skinfile)
|
2019-03-05 02:11:43 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end,
|
|
|
|
})
|
2019-03-05 12:57:57 +03:00
|
|
|
|
2019-03-05 15:50:35 +03:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if fields.__mcl_skins then
|
2019-03-05 15:59:00 +03:00
|
|
|
if mcl_skins.skin_count <= 6 then
|
|
|
|
-- Change skin immediately if there are not many skins
|
|
|
|
mcl_skins.cycle_skin(player)
|
2021-05-29 17:12:33 +03:00
|
|
|
if player:get_attach() then
|
2021-03-30 20:24:39 +03:00
|
|
|
mcl_player.player_set_animation(player, "sit")
|
|
|
|
end
|
2019-03-05 15:59:00 +03:00
|
|
|
else
|
|
|
|
-- Show skin selection formspec otherwise
|
|
|
|
mcl_skins.show_formspec(player:get_player_name())
|
|
|
|
end
|
2019-03-05 15:50:35 +03:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2021-05-25 13:52:25 +03:00
|
|
|
function mcl_skins.show_formspec(playername)
|
2019-03-05 15:50:35 +03:00
|
|
|
local formspec = "size[7,8.5]"
|
|
|
|
|
2021-04-19 16:16:59 +03:00
|
|
|
formspec = formspec .. "label[2,2;" .. minetest.formspec_escape(minetest.colorize("#383838", S("Select player skin:"))) .. "]"
|
2019-03-05 15:50:35 +03:00
|
|
|
.. "textlist[0,2.5;6.8,6;skins_set;"
|
|
|
|
|
|
|
|
local meta
|
|
|
|
local selected = 1
|
|
|
|
|
|
|
|
for i = 0, mcl_skins.skin_count do
|
|
|
|
|
2019-03-05 16:33:16 +03:00
|
|
|
local label = S("@1 (@2)", mcl_skins.meta[mcl_skins.list[i]].name, "#"..i)
|
2019-03-05 15:50:35 +03:00
|
|
|
|
|
|
|
formspec = formspec .. minetest.formspec_escape(label)
|
|
|
|
|
|
|
|
if mcl_skins.skins[playername] == mcl_skins.list[i] then
|
|
|
|
selected = i + 1
|
|
|
|
meta = mcl_skins.meta[mcl_skins.list[i]]
|
|
|
|
end
|
|
|
|
|
|
|
|
if i < #mcl_skins.list then
|
|
|
|
formspec = formspec ..","
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
formspec = formspec .. ";" .. selected .. ";false]"
|
|
|
|
|
|
|
|
formspec = formspec .. "image[0,0;1.35,2.7;" .. mcl_skins.previews[playername] .. ".png]"
|
|
|
|
|
|
|
|
if meta then
|
2019-03-05 16:33:16 +03:00
|
|
|
if meta.name and meta.name ~= "" then
|
2021-04-19 16:16:59 +03:00
|
|
|
formspec = formspec .. "label[2,0.5;" .. minetest.formspec_escape(minetest.colorize("#383838", S("Name: @1", meta.name))) .. "]"
|
2019-03-05 15:50:35 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.show_formspec(playername, "mcl_skins:skin_select", formspec)
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
|
|
|
|
if formname == "mcl_skins:skin_select" then
|
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
local event = minetest.explode_textlist_event(fields["skins_set"])
|
|
|
|
|
|
|
|
if event.type == "CHG" or event.type == "DCL" then
|
|
|
|
|
|
|
|
local skin_id = math.min(event.index - 1, mcl_skins.skin_count)
|
|
|
|
if not mcl_skins.list[skin_id] then
|
|
|
|
return -- Do not update wrong skin number
|
|
|
|
end
|
|
|
|
|
|
|
|
mcl_skins.set_player_skin(player, skin_id)
|
|
|
|
mcl_skins.show_formspec(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-03-05 12:57:57 +03:00
|
|
|
minetest.log("action", "[mcl_skins] Mod initialized with "..mcl_skins.skin_count.." custom skin(s)")
|