2015-10-17 01:36:39 +02:00
|
|
|
--
|
|
|
|
-- Player skins mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("player_skins")
|
2019-08-31 00:53:51 +02:00
|
|
|
local NS = function(s) return s end
|
2019-08-28 17:31:41 +02:00
|
|
|
|
2015-10-17 01:36:39 +02:00
|
|
|
player_skins = {}
|
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
-- Array of usable player skins
|
|
|
|
|
2019-08-31 00:53:51 +02:00
|
|
|
player_skins.skin_names = {NS("male"), NS("female")}
|
|
|
|
player_skins.default_skins = {male=true, female=true}
|
2016-02-02 21:53:45 +01:00
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
if minetest.settings:get("player_skins_names") then
|
|
|
|
player_skins.skin_names = util.split(minetest.settings:get("player_skins_names"), ",")
|
2015-10-17 01:36:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
player_skins.old_skins = {}
|
|
|
|
player_skins.skins = {}
|
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
local skins_file = minetest.get_worldpath() .. "/player_skins.dat"
|
2017-05-15 23:41:46 +02:00
|
|
|
local saving = false
|
|
|
|
|
|
|
|
local timer_interval = 1
|
|
|
|
local timer = 10
|
2015-10-17 01:36:39 +02:00
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
local function save_player_skins()
|
2015-10-17 01:36:39 +02:00
|
|
|
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
|
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
local function delayed_save()
|
|
|
|
if not saving then
|
|
|
|
saving = true
|
|
|
|
|
|
|
|
minetest.after(40, save_player_skins)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function load_player_skins()
|
2015-10-17 01:36:39 +02:00
|
|
|
local f = io.open(skins_file, "r")
|
|
|
|
|
|
|
|
if f then
|
|
|
|
repeat
|
|
|
|
local l = f:read("*l")
|
|
|
|
if l == nil then break end
|
|
|
|
|
2019-09-02 20:30:04 +02:00
|
|
|
for name, tex in string.gmatch(l, "(.+) (.+)") do
|
2015-10-17 01:36:39 +02:00
|
|
|
player_skins.skins[name] = tex
|
|
|
|
end
|
|
|
|
until f:read(0) == nil
|
|
|
|
|
|
|
|
io.close(f)
|
|
|
|
else
|
2017-05-15 23:41:46 +02:00
|
|
|
save_player_skins()
|
2015-10-17 01:36:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_valid_skin(tex)
|
|
|
|
for _, n in pairs(player_skins.skin_names) do
|
|
|
|
if n == tex then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function player_skins.get_skin(name)
|
|
|
|
return "player_skins_" .. player_skins.skins[name] .. ".png"
|
|
|
|
end
|
2017-05-13 20:50:15 +02:00
|
|
|
|
2015-10-17 01:36:39 +02:00
|
|
|
function player_skins.set_skin(name, tex)
|
2019-06-23 21:08:51 +02:00
|
|
|
if is_valid_skin(tex) then
|
|
|
|
player_skins.skins[name] = tex
|
|
|
|
save_player_skins()
|
2015-10-17 01:36:39 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(name, S("Invalid skin!"))
|
2015-10-17 01:36:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
local function on_load()
|
|
|
|
load_player_skins()
|
|
|
|
end
|
2015-10-17 01:36:39 +02:00
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
local function on_shutdown()
|
|
|
|
save_player_skins()
|
2015-10-17 01:36:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_joinplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2019-08-31 00:58:56 +02:00
|
|
|
-- New players start with a random skin
|
2015-10-17 01:36:39 +02:00
|
|
|
if player_skins.skins[name] == nil then
|
2019-08-31 00:58:56 +02:00
|
|
|
local skin = math.random(1, #player_skins.skin_names)
|
|
|
|
player_skins.skins[name] = player_skins.skin_names[skin]
|
2015-10-17 01:36:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
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
|
|
|
|
|
2019-08-29 17:44:48 +02:00
|
|
|
minetest.register_on_mods_loaded(on_load)
|
2017-05-15 23:41:46 +02:00
|
|
|
|
|
|
|
minetest.register_on_shutdown(on_shutdown)
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
2015-10-17 01:36:39 +02:00
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
minetest.register_globalstep(on_globalstep)
|
|
|
|
|
2015-10-17 01:36:39 +02:00
|
|
|
local function get_chatparams()
|
|
|
|
local s = "["
|
|
|
|
|
|
|
|
for _, n in pairs(player_skins.skin_names) do
|
|
|
|
if s == "[" then
|
|
|
|
s = s .. n
|
|
|
|
else
|
|
|
|
s = s .. "|" .. n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return s .. "]"
|
|
|
|
end
|
|
|
|
|
2016-02-02 21:53:45 +01:00
|
|
|
function player_skins.get_formspec(playername)
|
2017-05-13 20:50:15 +02:00
|
|
|
local form = default.ui.get_page("default:default")
|
2016-02-02 21:53:45 +01:00
|
|
|
|
|
|
|
form = form .. "image[4,0;0.5,10.05;ui_vertical_divider.png]"
|
|
|
|
|
|
|
|
for i, name in ipairs(player_skins.skin_names) do
|
2019-08-31 16:57:15 +02:00
|
|
|
local x = 0
|
2016-02-07 23:07:43 +01:00
|
|
|
local y = i - 0.5
|
2016-02-02 21:53:45 +01:00
|
|
|
|
|
|
|
if i > 8 then
|
|
|
|
x = 4.5
|
|
|
|
y = y - 8
|
|
|
|
end
|
|
|
|
|
2019-08-31 00:53:51 +02:00
|
|
|
local sname
|
|
|
|
if player_skins.default_skins[name] then
|
|
|
|
sname = S(name)
|
|
|
|
else
|
|
|
|
sname = name
|
|
|
|
end
|
2019-08-31 16:57:15 +02:00
|
|
|
form = form .. default.ui.button(x, y, 2.75, 1, "skin_select_"
|
2019-08-31 00:53:51 +02:00
|
|
|
.. name, sname)
|
2019-08-31 16:57:15 +02:00
|
|
|
form = form .. "image[" .. (x + 2.7) .. "," .. y.. ";1,1;player_skins_icon_"
|
2017-05-15 23:41:46 +02:00
|
|
|
.. name .. ".png]"
|
2016-02-02 21:53:45 +01:00
|
|
|
if player_skins.skins[playername] == name then
|
2019-08-31 16:57:15 +02:00
|
|
|
form = form .. "image[" .. (x + 3.65) .. "," .. (y + 0.25)
|
2017-05-15 23:41:46 +02:00
|
|
|
.. ";0.5,0.5;ui_checkmark.png]"
|
2016-02-02 21:53:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return form
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_player_receive_fields(
|
2016-02-02 21:53:45 +01:00
|
|
|
function(player, form_name, fields)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
for fieldname, val in pairs(fields) do
|
|
|
|
local skinname = string.match(fieldname, "skin_select_(.*)")
|
|
|
|
|
|
|
|
if skinname ~= nil then
|
|
|
|
player_skins.set_skin(name, skinname)
|
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
minetest.show_formspec(name, "player_skins:player_skins",
|
|
|
|
player_skins.get_formspec(name))
|
2016-02-02 21:53:45 +01:00
|
|
|
end
|
|
|
|
end
|
2017-05-15 23:41:46 +02:00
|
|
|
end)
|
2016-02-02 21:53:45 +01:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_chatcommand(
|
2015-10-17 01:36:39 +02:00
|
|
|
"player_skin",
|
|
|
|
{
|
|
|
|
params = get_chatparams(),
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Set your player skin"),
|
2019-06-23 21:08:51 +02:00
|
|
|
privs = {},
|
2015-10-17 01:36:39 +02:00
|
|
|
func = function(name, param)
|
2017-05-15 23:41:46 +02:00
|
|
|
if is_valid_skin(param) then
|
|
|
|
player_skins.set_skin(name, param)
|
2019-08-28 17:31:41 +02:00
|
|
|
return true, S("Skin set to “@1”.", param)
|
2017-05-15 23:41:46 +02:00
|
|
|
elseif param == "" then
|
2019-08-28 17:31:41 +02:00
|
|
|
return true, S("Current player skin: @1", player_skins.skins[name])
|
2017-05-15 23:41:46 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
return false, S("Unknown player skin. Enter “/help player_skin” for help.")
|
2017-05-15 23:41:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
2015-10-17 23:15:34 +02:00
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
default.log("mod:player_skins", "loaded")
|