clothing.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. clothing = {
  2. formspec = armor.formspec,
  3. textures = {},
  4. }
  5. clothing.set_player_clothing = function(self, player)
  6. if not player then
  7. return
  8. end
  9. local name = player:get_player_name()
  10. local player_inv = player:get_inventory()
  11. if not name or not player_inv then
  12. return
  13. end
  14. local clothing_texture = "lottarmor_trans.png"
  15. local elements = {}
  16. local textures = {}
  17. local preview = multiskin:get_skin_name(name) or "clothing_preview"
  18. preview = preview..".png"
  19. for i=1, 5 do
  20. local stack = player_inv:get_stack("clothing", i)
  21. local item = stack:get_name()
  22. if stack:get_count() == 1 then
  23. local def = stack:get_definition()
  24. if def.groups["clothes"] == 1 then
  25. local texture = item:gsub("%:", "_")
  26. table.insert(textures, texture..".png")
  27. if not def.groups["no_preview"] then
  28. preview = preview.."^"..texture.."_preview.png"
  29. end
  30. end
  31. end
  32. end
  33. if #textures > 0 then
  34. clothing_texture = table.concat(textures, "^")
  35. end
  36. self.textures[name].clothing = clothing_texture
  37. self.textures[name].preview = preview
  38. multiskin[name].clothing = clothing_texture
  39. multiskin:update_player_visuals(player)
  40. end
  41. clothing.update_inventory = function(self, player)
  42. local name = player:get_player_name()
  43. if unified_inventory then
  44. if unified_inventory.current_page[name] == "clothing" then
  45. unified_inventory.set_inventory_formspec(player, "clothing")
  46. end
  47. else
  48. local formspec = armor.get_armor_formspec(self, name)
  49. if inventory_plus then
  50. local page = player:get_inventory_formspec()
  51. if page:find("detached:"..name.."_clothing") then
  52. inventory_plus.set_inventory_formspec(player, formspec)
  53. end
  54. else
  55. player:set_inventory_formspec(formspec)
  56. end
  57. end
  58. end
  59. minetest.register_on_player_receive_fields(function(player, formname, fields)
  60. local name = player:get_player_name()
  61. if inventory_plus and fields.clothing then
  62. local formspec = clothing:get_clothing_formspec(name)
  63. inventory_plus.set_inventory_formspec(player, formspec)
  64. return
  65. end
  66. end)
  67. minetest.register_on_joinplayer(function(player)
  68. multiskin:init(player)
  69. local name = player:get_player_name()
  70. local player_inv = player:get_inventory()
  71. local clothing_inv = minetest.create_detached_inventory(name.."_clothing",{
  72. on_put = function(inv, listname, index, stack, player)
  73. player:get_inventory():set_stack(listname, index, stack)
  74. clothing:set_player_clothing(player)
  75. clothing:update_inventory(player)
  76. end,
  77. on_take = function(inv, listname, index, stack, player)
  78. player:get_inventory():set_stack(listname, index, nil)
  79. clothing:set_player_clothing(player)
  80. clothing:update_inventory(player)
  81. end,
  82. on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  83. local plaver_inv = player:get_inventory()
  84. local stack = inv:get_stack(to_list, to_index)
  85. player_inv:set_stack(to_list, to_index, stack)
  86. player_inv:set_stack(from_list, from_index, nil)
  87. clothing:set_player_clothing(player)
  88. clothing:update_inventory(player)
  89. end,
  90. allow_put = function(inv, listname, index, stack, player)
  91. if index == 1 then
  92. if stack:get_definition().groups.clothes_head == nil then
  93. return 0
  94. else
  95. return 1
  96. end
  97. elseif index == 2 then
  98. if stack:get_definition().groups.clothes_torso == nil then
  99. return 0
  100. else
  101. return 1
  102. end
  103. elseif index == 3 then
  104. if stack:get_definition().groups.clothes_legs == nil then
  105. return 0
  106. else
  107. return 1
  108. end
  109. elseif index == 4 then
  110. if stack:get_definition().groups.clothes_feet == nil then
  111. return 0
  112. else
  113. return 1
  114. end
  115. elseif index == 5 then
  116. if stack:get_definition().groups.clothes_cloak == nil then
  117. return 0
  118. else
  119. return 1
  120. end
  121. end
  122. end,
  123. allow_take = function(inv, listname, index, stack, player)
  124. return stack:get_count()
  125. end,
  126. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  127. return 0
  128. end,
  129. }, name)
  130. clothing_inv:set_size("clothing", 5)
  131. player_inv:set_size("clothing", 5)
  132. for i=1, 5 do
  133. local stack = player_inv:get_stack("clothing", i)
  134. clothing_inv:set_stack("clothing", i, stack)
  135. end
  136. clothing.textures[name] = {
  137. clthing = "clothing_trans.png",
  138. preview = "clothing_preview.png",
  139. }
  140. for i=1, ARMOR_INIT_TIMES do
  141. minetest.after(ARMOR_INIT_DELAY * i, function(player)
  142. clothing:set_player_clothing(player)
  143. if inventory_plus == nil and unified_inventory == nil then
  144. clothing:update_inventory(player)
  145. end
  146. end, player)
  147. end
  148. end)