init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local time = 0
  2. local update_time = tonumber(minetest.settings:get("wieldview_update_time"))
  3. if not update_time then
  4. update_time = 2
  5. minetest.settings:set("wieldview_update_time", tostring(update_time))
  6. end
  7. local node_tiles = minetest.settings:get_bool("wieldview_node_tiles")
  8. if not node_tiles then
  9. node_tiles = false
  10. minetest.settings:set("wieldview_node_tiles", "false")
  11. end
  12. wieldview = {
  13. wielded_item = {},
  14. transform = {},
  15. }
  16. dofile(minetest.get_modpath(minetest.get_current_modname()).."/transform.lua")
  17. wieldview.get_item_texture = function(self, item)
  18. local texture = "3d_armor_trans.png"
  19. if item ~= "" then
  20. if minetest.registered_items[item] then
  21. if minetest.registered_items[item].inventory_image ~= "" then
  22. texture = minetest.registered_items[item].inventory_image
  23. elseif node_tiles == true and minetest.registered_items[item].tiles
  24. and type(minetest.registered_items[item].tiles[1]) == "string"
  25. and minetest.registered_items[item].tiles[1] ~= "" then
  26. texture = minetest.inventorycube(minetest.registered_items[item].tiles[1])
  27. end
  28. end
  29. -- Get item image transformation, first from group, then from transform.lua
  30. local transform = minetest.get_item_group(item, "wieldview_transform")
  31. if transform == 0 then
  32. transform = wieldview.transform[item]
  33. end
  34. if transform then
  35. -- This actually works with groups ratings because transform1, transform2, etc.
  36. -- have meaning and transform0 is used for identidy, so it can be ignored
  37. texture = texture.."^[transform"..tostring(transform)
  38. end
  39. end
  40. return texture
  41. end
  42. wieldview.update_wielded_item = function(self, player)
  43. if not player then
  44. return
  45. end
  46. local name = player:get_player_name()
  47. local stack = player:get_wielded_item()
  48. local item = stack:get_name()
  49. if not item then
  50. return
  51. end
  52. if self.wielded_item[name] then
  53. if self.wielded_item[name] == item then
  54. return
  55. end
  56. armor.textures[name].wielditem = self:get_item_texture(item)
  57. armor:update_player_visuals(player)
  58. end
  59. self.wielded_item[name] = item
  60. end
  61. minetest.register_on_joinplayer(function(player)
  62. local name = player:get_player_name()
  63. wieldview.wielded_item[name] = ""
  64. minetest.after(0, function(player)
  65. wieldview:update_wielded_item(player)
  66. end, player)
  67. end)
  68. minetest.register_globalstep(function(dtime)
  69. time = time + dtime
  70. if time > update_time then
  71. for _,player in ipairs(minetest.get_connected_players()) do
  72. wieldview:update_wielded_item(player)
  73. end
  74. time = 0
  75. end
  76. end)