init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. local S = minetest.get_translator("homedecor_wardrobe")
  2. modpath = minetest.get_modpath("homedecor_wardrobe")
  3. local wd_cbox = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, 1.5, 0.5}}
  4. -- cache set_textures function (fallback to old version)
  5. -- default.player_set_textures is deprecated and will be removed in future
  6. local set_player_textures =
  7. minetest.get_modpath("player_api") and player_api.set_textures
  8. or default.player_set_textures
  9. local armor_mod_path = minetest.get_modpath("3d_armor")
  10. local skinslist = {"male1", "male2", "male3", "male4", "male5"}
  11. local default_skin = "character.png"
  12. local skinsdb_mod_path = minetest.get_modpath("skinsdb")
  13. if skinsdb_mod_path then
  14. for _, shrt in ipairs(skinslist) do
  15. for _, prefix in ipairs({"", "fe"}) do
  16. local skin_name = prefix..shrt
  17. local skin_obj = skins.new("homedecor_clothes_"..skin_name..".png")
  18. skin_obj:set_preview("homedecor_clothes_"..skin_name.."_preview.png")
  19. skin_obj:set_texture("homedecor_clothes_"..skin_name..".png")
  20. skin_obj:set_meta("name", S("Wardrobe").." "..skin_name)
  21. skin_obj:set_meta("author", 'Calinou and Jordach')
  22. skin_obj:set_meta("license", 'CC-by-SA-4.0')
  23. local file = io.open(modpath ..
  24. "/textures/homedecor_clothes_" .. skin_name .. ".png", "r")
  25. skin_obj:set_meta("format", skins.get_skin_format(file))
  26. file:close()
  27. skin_obj:set_meta("in_inventory_list", false)
  28. end
  29. end
  30. end
  31. local function set_player_skin(player, skin, save)
  32. if skinsdb_mod_path then
  33. skins.set_player_skin(player, skin or skins.default)
  34. elseif armor_mod_path then -- if 3D_armor's installed, let it set the skin
  35. armor.textures[player:get_player_name()].skin = skin or default_skin
  36. armor:update_player_visuals(player)
  37. else
  38. set_player_textures(player, { skin or default_skin})
  39. end
  40. if save and not skinsdb_mod_path then
  41. if skin == default_skin then
  42. player:set_attribute("homedecor:player_skin", "")
  43. else
  44. player:set_attribute("homedecor:player_skin", skin)
  45. end
  46. end
  47. end
  48. local def = {
  49. description = S("Wardrobe"),
  50. drawtype = "mesh",
  51. mesh = "homedecor_bedroom_wardrobe.obj",
  52. tiles = {
  53. {name = "homedecor_generic_wood_plain.png", color = 0xffa76820},
  54. "homedecor_wardrobe_drawers.png",
  55. "homedecor_wardrobe_doors.png"
  56. },
  57. inventory_image = "homedecor_wardrobe_inv.png",
  58. paramtype = "light",
  59. paramtype2 = "facedir",
  60. groups = {snappy = 3},
  61. selection_box = wd_cbox,
  62. collision_box = wd_cbox,
  63. sounds = default.node_sound_wood_defaults(),
  64. on_rotate = minetest.get_modpath("screwdriver") and screwdriver.rotate_simple or nil,
  65. on_place = function(itemstack, placer, pointed_thing)
  66. return homedecor.stack_vertically(itemstack, placer, pointed_thing,
  67. itemstack:get_name(), "placeholder")
  68. end,
  69. can_dig = function(pos,player)
  70. local meta = minetest.get_meta(pos)
  71. return meta:get_inventory():is_empty("main")
  72. end,
  73. on_construct = function(pos)
  74. local meta = minetest.get_meta(pos)
  75. meta:set_string("infotext", S("Wardrobe"))
  76. meta:get_inventory():set_size("main", 10)
  77. -- textures made by the Minetest community (mostly Calinou and Jordach)
  78. local clothes_strings = ""
  79. for i = 1, 5 do
  80. clothes_strings = clothes_strings ..
  81. "image_button_exit[" .. (i-1) ..
  82. ".5,0;1.1,2;homedecor_clothes_" .. skinslist[i] ..
  83. "_preview.png;" .. skinslist[i] .. ";]" ..
  84. "image_button_exit[" .. (i-1) ..
  85. ".5,2;1.1,2;homedecor_clothes_fe" .. skinslist[i] ..
  86. "_preview.png;fe" .. skinslist[i] .. ";]"
  87. end
  88. meta:set_string("formspec", "size[5.5,8.5]" ..
  89. default.gui_bg .. default.gui_bg_img .. default.gui_slots ..
  90. "vertlabel[0,0.5;" .. minetest.formspec_escape(S("Clothes")) .. "]" ..
  91. "button_exit[0,3.29;0.6,0.6;default;x]" ..
  92. clothes_strings ..
  93. "vertlabel[0,5.2;" .. minetest.formspec_escape(S("Storage")) .. "]" ..
  94. "list[current_name;main;0.5,4.5;5,2;]" ..
  95. "list[current_player;main;0.5,6.8;5,2;]" ..
  96. "listring[]"
  97. )
  98. end,
  99. on_receive_fields = function(pos, formname, fields, sender)
  100. if fields.default then
  101. set_player_skin(sender, nil, "player")
  102. return
  103. end
  104. for i = 1, 5 do
  105. if fields[skinslist[i]] then
  106. set_player_skin(sender,
  107. "homedecor_clothes_" .. skinslist[i] .. ".png", "player")
  108. break
  109. elseif fields["fe" .. skinslist[i]] then
  110. set_player_skin(sender,
  111. "homedecor_clothes_fe" .. skinslist[i] .. ".png", "player")
  112. break
  113. end
  114. end
  115. end
  116. }
  117. -- register the actual minetest node
  118. minetest.register_node(":homedecor:wardrobe", def)
  119. minetest.register_alias("homedecor:wardrobe_bottom", "homedecor:wardrobe")
  120. minetest.register_alias("homedecor:wardrobe_top", "air")
  121. if not skinsdb_mod_path then -- If not managed by skinsdb
  122. minetest.register_on_joinplayer(function(player)
  123. local skin = player:get_attribute("homedecor:player_skin")
  124. if skin and skin ~= "" then
  125. -- setting player skin on connect has no effect, so delay skin change
  126. minetest.after(1, function(player1, skin1)
  127. set_player_skin(player1, skin1)
  128. end, player, skin)
  129. end
  130. end)
  131. end
  132. minetest.register_craft( {
  133. output = "homedecor:wardrobe",
  134. recipe = {
  135. { "homedecor:drawer_small", "homedecor:kitchen_cabinet" },
  136. { "homedecor:drawer_small", "group:wood" },
  137. { "homedecor:drawer_small", "group:wood" }
  138. },
  139. })