homedecor_modpack/homedecor/wardrobe.lua

146 lines
4.7 KiB
Lua
Raw Normal View History

2015-05-01 14:51:52 +02:00
2017-01-31 06:34:41 +01:00
local S = homedecor_i18n.gettext
2015-05-01 14:51:52 +02:00
local wd_cbox = {
type = "fixed",
fixed = { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 }
}
-- cache set_textures function (fallback to old version)
-- default.player_set_textures is deprecated and will be removed in future
local set_player_textures =
2018-04-27 22:39:23 +02:00
minetest.get_modpath("player_api") and player_api.set_textures
or default.player_set_textures
2018-04-27 22:39:23 +02:00
local armor_mod_path = minetest.get_modpath("3d_armor")
local skinslist = {"male1", "male2", "male3", "male4", "male5"}
2018-04-28 03:40:02 +02:00
local default_skin = "character.png"
2018-04-27 22:39:23 +02:00
local skinsdb_mod_path = minetest.get_modpath("skinsdb")
if skinsdb_mod_path then
for _, shrt in ipairs(skinslist) do
for _, prefix in ipairs({"", "fe"}) do
local skin_name = prefix..shrt
local skin_obj = skins.new("homedecor_clothes_"..skin_name..".png") -- Texture PNG file as key to be compatible in set_player_skin
skin_obj:set_preview("homedecor_clothes_"..skin_name.."_preview.png")
skin_obj:set_texture("homedecor_clothes_"..skin_name..".png")
skin_obj:set_meta("name", "Wardrobe "..skin_name)
skin_obj:set_meta("author", 'Calinou and Jordach')
skin_obj:set_meta("license", 'CC-by-SA-4.0')
local file = io.open(homedecor.modpath.."/textures/homedecor_clothes_"..skin_name..".png", "r")
skin_obj:set_meta("format", skins.get_skin_format(file))
file:close()
skin_obj:set_meta("in_inventory_list", false)
end
end
end
2018-04-28 03:40:02 +02:00
function homedecor.get_player_skin(player)
local skin = player:get_attribute("homedecor:player_skin")
if not skin or skin == "" then
return default_skin, true
end
return skin, false
end
function homedecor.set_player_skin(player, skin, save)
skin = skin or default_skin
if skinsdb_mod_path then
skins.set_player_skin(player, skin)
elseif armor_mod_path then -- if 3D_armor's installed, let it set the skin
2018-04-27 22:39:23 +02:00
armor.textures[player:get_player_name()].skin = skin
armor:update_player_visuals(player)
else
set_player_textures(player, { skin })
end
2018-04-28 03:40:02 +02:00
if save then
if skin == default_skin then
skin = "default"
player:set_attribute("homedecor:player_skin", "")
else
player:set_attribute("homedecor:player_skin", skin)
end
if save == "player" then -- if player action
minetest.log("verbose",
S("player @1 sets skin to @2", player:get_player_name(), skin) ..
(armor_mod_path and ' [3d_armor]' or '')
)
end
end
end
function homedecor.unset_player_skin(player)
homedecor.set_player_skin(player, nil, true)
2018-04-27 22:39:23 +02:00
end
2015-05-01 14:51:52 +02:00
homedecor.register("wardrobe", {
mesh = "homedecor_bedroom_wardrobe.obj",
tiles = {
homedecor.plain_wood,
2015-05-01 14:51:52 +02:00
"homedecor_wardrobe_drawers.png",
"homedecor_wardrobe_doors.png"
},
inventory_image = "homedecor_wardrobe_inv.png",
2017-01-31 06:34:41 +01:00
description = S("Wardrobe"),
2015-05-01 14:51:52 +02:00
groups = {snappy=3},
selection_box = wd_cbox,
collision_box = wd_cbox,
sounds = default.node_sound_wood_defaults(),
expand = { top="placeholder" },
on_rotate = screwdriver.rotate_simple,
2017-01-31 06:34:41 +01:00
infotext = S("Wardrobe"),
inventory = {
size = 10
},
2015-05-01 14:51:52 +02:00
on_construct = function(pos)
local meta = minetest.get_meta(pos)
-- textures made by the Minetest community (mostly Calinou and Jordach)
local clothes_strings = ""
for i = 1,5 do
clothes_strings = clothes_strings..
"image_button_exit["..(i-1)..".5,0;1.1,2;homedecor_clothes_"..skinslist[i].."_preview.png;"..skinslist[i]..";]"..
"image_button_exit["..(i-1)..".5,2;1.1,2;homedecor_clothes_fe"..skinslist[i].."_preview.png;fe"..skinslist[i]..";]"
end
2015-05-01 14:51:52 +02:00
meta:set_string("formspec", "size[5.5,8.5]"..default.gui_bg..default.gui_bg_img..default.gui_slots..
2017-01-31 06:34:41 +01:00
"vertlabel[0,0.5;"..minetest.formspec_escape(S("Clothes")).."]"..
2018-04-28 03:40:02 +02:00
"button_exit[0,3.29;0.6,0.6;default;x]"..
clothes_strings..
2017-01-31 06:34:41 +01:00
"vertlabel[0,5.2;"..minetest.formspec_escape(S("Storage")).."]"..
2015-05-01 14:51:52 +02:00
"list[current_name;main;0.5,4.5;5,2;]"..
"list[current_player;main;0.5,6.8;5,2;]" ..
"listring[]")
2015-05-01 14:51:52 +02:00
end,
on_receive_fields = function(pos, formname, fields, sender)
2018-04-28 03:40:02 +02:00
if fields.default then
homedecor.set_player_skin(sender, nil, "player")
return
end
for i = 1,5 do
if fields[skinslist[i]] then
homedecor.set_player_skin(sender, "homedecor_clothes_"..skinslist[i]..".png", "player")
break
elseif fields["fe"..skinslist[i]] then
homedecor.set_player_skin(sender, "homedecor_clothes_fe"..skinslist[i]..".png", "player")
break
end
2015-05-01 14:51:52 +02:00
end
end
})
minetest.register_alias("homedecor:wardrobe_bottom", "homedecor:wardrobe")
minetest.register_alias("homedecor:wardrobe_top", "air")
minetest.register_on_joinplayer(function(player)
local skin = player:get_attribute("homedecor:player_skin")
2018-04-28 03:40:02 +02:00
if skin and skin ~= "" then
-- setting player skin on connect has no effect, so delay skin change
minetest.after(1, function(player, skin)
homedecor.set_player_skin(player, skin)
end, player, skin)
end
end)