skinsdb/api.lua

95 lines
2.5 KiB
Lua
Raw Normal View History

2017-06-15 14:16:11 +02:00
-- get current skin
2018-06-30 16:15:57 +02:00
local storage = minetest.get_mod_storage()
function skins.get_player_skin(player)
local player_name = player:get_player_name()
2020-07-29 20:27:09 +02:00
local meta = player:get_meta()
if meta:get("skinsdb:skin_key") then
-- Move player data prior July 2018 to mod storage
storage:set_string(player_name, meta:get_string("skinsdb:skin_key"))
2020-07-29 20:27:09 +02:00
meta:set_string("skinsdb:skin_key", "")
2018-06-30 16:35:53 +02:00
end
local skin_name = storage:get_string(player_name)
local skin = skins.get(skin_name)
if #skin_name > 0 and not skin then
-- Migration step to convert `_`-delimited skins to `.` (if possible)
skin = skins.__fuzzy_match_skin_name(player_name, skin_name, true)
if skin then
storage:set_string(player_name, skin:get_key())
else
storage:set_string(player_name, "")
end
end
return skin or skins.get(skins.default)
2014-03-17 06:26:58 +01:00
end
2014-07-23 13:46:42 +02:00
-- Assign skin to player
function skins.assign_player_skin(player, skin)
2017-06-16 23:00:36 +02:00
local skin_obj
if type(skin) == "string" then
2018-02-13 22:02:22 +01:00
skin_obj = skins.get(skin)
2017-06-16 23:00:36 +02:00
else
skin_obj = skin
end
2018-02-13 22:02:22 +01:00
if not skin_obj then
return false
end
if skin_obj:is_applicable_for_player(player:get_player_name()) then
local skin_key = skin_obj:get_key()
if skin_key == skins.default then
skin_key = ""
end
2018-06-30 16:15:57 +02:00
storage:set_string(player:get_player_name(), skin_key)
2018-02-13 22:02:22 +01:00
else
return false
2014-07-23 13:46:42 +02:00
end
return true, skin_obj
2017-06-15 14:16:11 +02:00
end
2014-03-17 06:26:58 +01:00
2017-06-15 14:16:11 +02:00
-- update visuals
function skins.update_player_skin(player)
if skins.armor_loaded then
-- all needed is wrapped and implemented in 3d_armor mod
armor:set_player_armor(player)
else
-- do updates manually without 3d_armor
skins.get_player_skin(player):apply_skin_to_player(player)
if minetest.global_exists("sfinv") and sfinv.enabled then
sfinv.set_player_inventory_formspec(player)
end
end
2014-03-17 06:26:58 +01:00
end
-- Assign and update - should be used on selection externally
function skins.set_player_skin(player, skin)
local success, skin_obj = skins.assign_player_skin(player, skin)
2018-02-13 22:02:22 +01:00
if success then
skins.get_player_skin(player):set_skin(player)
2018-02-13 22:02:22 +01:00
skins.update_player_skin(player)
2022-02-16 10:18:30 +01:00
minetest.log("action", player:get_player_name().." set skin to "..skin_obj:get_key(""))
2018-02-13 22:02:22 +01:00
end
return success
end
2018-04-15 01:09:38 +02:00
-- Check Skin format (code stohlen from stu's multiskin)
function skins.get_skin_format(file)
file:seek("set", 1)
if file:read(3) == "PNG" then
file:seek("set", 16)
local ws = file:read(4)
local hs = file:read(4)
local w = ws:sub(3, 3):byte() * 256 + ws:sub(4, 4):byte()
local h = hs:sub(3, 3):byte() * 256 + hs:sub(4, 4):byte()
if w >= 64 then
if w == h then
return "1.8"
elseif w == h * 2 then
return "1.0"
end
end
end
end