realtest_mt5/mods/mf_skins/init.lua

119 lines
3.2 KiB
Lua
Raw Normal View History

2020-03-30 22:41:58 +02:00
-- simple male/female player textures mod
-- based on player_textures by PilzAdam
-- License: WTFPL
local worldpath = minetest.get_worldpath()
local textures_config = worldpath.."/player_skins_db.txt"
if io.open(textures_config, "r") ~= nil then
io.input(textures_config)
skins_cfg = io.read("*all")
2020-03-30 23:09:30 +02:00
minetest.log("verbose", dump(skins_cfg))
2020-03-30 22:41:58 +02:00
mf_skins_table = minetest.deserialize(skins_cfg)
io.close()
end
2020-03-30 22:59:44 +02:00
if minetest.global_exists("mf_skins_table") then
mf_skins_table = mf_skins_table
else
mf_skins_table = {}
end
2020-03-30 22:41:58 +02:00
local save_skins = function()
2020-03-30 23:09:30 +02:00
minetest.log("verbose", dump(mf_skins_table))
2020-03-30 22:41:58 +02:00
local file = io.open(textures_config, "w")
file:write(minetest.serialize(mf_skins_table))
io.close()
end
minetest.register_on_joinplayer(
function(player)
local pn = player:get_player_name()
local skin_name = "skin_"..pn
2020-04-17 20:19:32 +02:00
local skin_gender
if mf_skins_table[skin_name] == "m" then
skin_gender = { "player_male.png" }
elseif mf_skins_table[skin_name] == "f" then
2020-03-30 22:41:58 +02:00
skin_gender = { "player_female.png" }
2020-04-17 20:19:32 +02:00
elseif mf_skins_table[skin_name] == "nyan" then
2020-03-30 22:41:58 +02:00
skin_gender = { "player_nyan.png" }
end
2020-04-17 20:19:32 +02:00
if not skin_gender then
local r = math.random(1,2)
if r == 1 then
skin_gender = { "player_male.png" }
mf_skins_table[skin_name] = "m"
else
skin_gender = { "player_female.png" }
mf_skins_table[skin_name] = "f"
end
2020-04-17 20:24:10 +02:00
save_skins()
2020-04-17 20:19:32 +02:00
end
2020-03-30 22:41:58 +02:00
player:set_properties({
visual = "mesh",
visual_size = {x=1, y=1},
textures = skin_gender
})
2020-04-17 20:19:32 +02:00
minetest.log("action", "Skin for "..pn.." was set to "..dump(mf_skins_table[skin_name]))
2020-03-30 22:41:58 +02:00
end
)
-- commands
minetest.register_chatcommand("skin", {
2020-04-17 19:49:40 +02:00
params = "<name> <gender>",
description = "Set a player's skin to either male (m) or female (f).",
func = function(name, param)
2020-03-30 22:41:58 +02:00
-- this line borrowed from worldedit
local _,_, username, gender = param:find("^([^%s]+)%s+(.+)$")
2020-04-17 19:49:40 +02:00
if not username or not gender then
return false, "Invalid syntax."
end
2020-03-30 22:41:58 +02:00
if minetest.get_player_privs(name).basic_privs or name==username then
2020-04-17 19:49:40 +02:00
if username and minetest.player_exists(username) then
if gender == "nyan" then
-- Nyan skin only for privileged players to prevent abuse
local privs = minetest.get_player_privs(username)
if not privs.server then
gender = nil
end
end
2020-04-17 20:19:32 +02:00
if gender ~= "f" and gender ~= "m" and gender ~= "nyan" then
-- Pick random skin if invalid
local r = math.random(1,2)
if r == 1 then
gender = "m"
else
gender = "f"
end
end
2020-03-30 22:41:58 +02:00
mf_skins_table["skin_"..username] = gender
minetest.chat_send_player(name, "Set skin for "..username.." to "..gender..".")
save_skins()
local skin_gender = { "player_male.png" }
if gender == "f" then
skin_gender = { "player_female.png" }
elseif gender == "nyan" then
skin_gender = { "player_nyan.png" }
end
local player = minetest.get_player_by_name(username)
player:set_properties({
visual = "mesh",
visual_size = {x=1, y=1},
textures = skin_gender
})
else
2020-04-17 19:49:40 +02:00
return false, "That player does not exist."
2020-03-30 22:41:58 +02:00
end
else
return false, "You are not authorized to run this command (required privilege: 'basic_privs')."
2020-03-30 22:41:58 +02:00
end
2020-04-17 19:49:40 +02:00
return true
end
2020-03-30 22:41:58 +02:00
})