Refactor player_skins a little

This commit is contained in:
Wuzzy 2021-08-07 20:27:06 +02:00
parent c6f553bb82
commit 88a5bf1006

View File

@ -13,52 +13,11 @@ player_skins = {}
player_skins.skin_names = {NS("male"), NS("female")}
player_skins.default_skins = {male=true, female=true}
player_skins.old_skins = {}
player_skins.skins = {}
local skins_file = minetest.get_worldpath() .. "/player_skins.dat"
local saving = false
local timer_interval = 1
local timer = 10
local function save_player_skins()
local f = io.open(skins_file, "w")
for name, tex in pairs(player_skins.skins) do
f:write(name .. " " .. tex .. "\n")
end
io.close(f)
end
local function delayed_save()
if not saving then
saving = true
minetest.after(40, save_player_skins)
end
end
local function load_player_skins()
local f = io.open(skins_file, "r")
if f then
repeat
local l = f:read("*l")
if l == nil then break end
for name, tex in string.gmatch(l, "(.+) (.+)") do
player_skins.skins[name] = tex
end
until f:read(0) == nil
io.close(f)
else
save_player_skins()
end
end
local function is_valid_skin(tex)
for _, n in pairs(player_skins.skin_names) do
if n == tex then
@ -70,69 +29,24 @@ local function is_valid_skin(tex)
end
function player_skins.get_skin(name)
return "player_skins_" .. player_skins.skins[name] .. ".png"
return "player_skins_male.png"
end
function player_skins.set_skin(name, tex)
if is_valid_skin(tex) then
player_skins.skins[name] = tex
save_player_skins()
else
minetest.chat_send_player(name, S("Invalid skin!"))
end
end
local function on_load()
load_player_skins()
end
local function on_shutdown()
save_player_skins()
end
local function on_joinplayer(player)
local name = player:get_player_name()
-- New players start with a random skin
if player_skins.skins[name] == nil then
local skin = math.random(1, #player_skins.skin_names)
player_skins.skins[name] = player_skins.skin_names[skin]
end
player_skins.set_random_skin(name)
end
local function on_globalstep(dtime)
timer = timer + dtime
if timer < timer_interval then
return
end
timer = 0
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if player_skins.skins[name] ~= player_skins.old_skins[name] then
default.player_set_textures(
player, {
"player_skins_" .. player_skins.skins[name] .. ".png"
})
player_skins.old_skins[name] = player_skins.skins[name]
delayed_save()
end
end
end
minetest.register_on_mods_loaded(on_load)
minetest.register_on_shutdown(on_shutdown)
minetest.register_on_joinplayer(on_joinplayer)
minetest.register_globalstep(on_globalstep)
local function get_chatparams()
local s = "["
@ -228,6 +142,27 @@ local band_colors = { "red", "redviolet", "magenta", "purple", "blue", "skyblue"
local hairs = { "beard", "short" }
local eye_colors = { "green", "blue", "brown" }
function player_skins.set_random_skin(name)
local player = minetest.get_player_by_name(name)
if not player then
return false
end
-- TODO: Pick a random skin color (0-9)
local scol = 1
local ccol = cloth_colors[math.random(1, #cloth_colors)]
local bcol = band_colors[math.random(1, #band_colors)]
local hair = hairs[math.random(1, #hairs)]
local ecol = eye_colors[math.random(1, #eye_colors)]
default.player_set_textures(
player, {
"player_skins_skin_"..scol..".png" .. "^" ..
"player_skins_eyes_"..ecol..".png" .. "^" ..
"player_skins_hair_"..hair..".png" .. "^" ..
"player_skins_clothes_"..ccol..".png" .. "^" ..
"player_skins_bands_"..bcol..".png"
})
end
minetest.register_chatcommand(
"random_skin",
{
@ -235,23 +170,7 @@ minetest.register_chatcommand(
description = S("Set random player skin"),
privs = {},
func = function(name, param)
local player = minetest.get_player_by_name(name)
if not player then
return false
end
local scol = math.random(0,9)
local ccol = cloth_colors[math.random(1, #cloth_colors)]
local bcol = band_colors[math.random(1, #band_colors)]
local hair = hairs[math.random(1, #hairs)]
local ecol = eye_colors[math.random(1, #eye_colors)]
default.player_set_textures(
player, {
"player_skins_skin_"..scol..".png" .. "^" ..
"player_skins_eyes_"..ecol..".png" .. "^" ..
"player_skins_hair_"..hair..".png" .. "^" ..
"player_skins_clothes_"..ccol..".png" .. "^" ..
"player_skins_bands_"..bcol..".png"
})
player_skins.set_random_skin(name)
return true
end
})