init.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. wield3d = {}
  2. wield3d.modpath = minetest.get_modpath("wield3d")
  3. dofile(wield3d.modpath .. "/location.lua")
  4. local update_time_conf = minetest.setting_get("wield3d_update_time") or 1
  5. local update_time = tonumber(update_time_conf) or 1
  6. local timer = 0
  7. local periodic_reset = 0
  8. local player_wielding = {}
  9. local location = {
  10. "Arm_Right", -- default bone
  11. {x=0, y=5.5, z=3}, -- default position
  12. {x=-90, y=225, z=90}, -- default rotation
  13. {x=0.25, y=0.25}, -- default scale
  14. }
  15. local function add_wield_entity(player)
  16. local name = player:get_player_name()
  17. local pos = player:getpos()
  18. if name and pos then
  19. pos.y = pos.y + 0.5
  20. local object = minetest.add_entity(pos, "wield3d:wield_entity")
  21. if object then
  22. object:set_attach(player, location[1], location[2], location[3])
  23. object:set_properties({
  24. textures = {"wield3d:hand"},
  25. visual_size = location[4],
  26. })
  27. player_wielding[name] = {}
  28. player_wielding[name].item = ""
  29. player_wielding[name].object = object
  30. player_wielding[name].location = location
  31. end
  32. end
  33. end
  34. minetest.register_item("wield3d:hand", {
  35. type = "none",
  36. wield_image = "blank.png",
  37. })
  38. minetest.register_entity("wield3d:wield_entity", {
  39. physical = false,
  40. collisionbox = {-0.125,-0.125,-0.125, 0.125,0.125,0.125},
  41. visual = "wielditem",
  42. pointable = false,
  43. on_activate = function(self, staticdata)
  44. if staticdata == "expired" then
  45. self.object:remove()
  46. end
  47. end,
  48. on_punch = function(self)
  49. self.object:remove()
  50. end,
  51. get_staticdata = function(self)
  52. return "expired"
  53. end,
  54. })
  55. -- Shall be called whenever someone teleports.
  56. -- This mod requires to be able to reset the wield entities.
  57. function wield3d.on_teleport()
  58. local players = minetest.get_connected_players()
  59. -- Just clear all the wield entities. They will be restored shortly.
  60. for k, v in ipairs(players) do
  61. local name = v:get_player_name()
  62. local wield = player_wielding[name]
  63. if wield and wield.object then
  64. wield.object:set_detach()
  65. wield.object:remove()
  66. end
  67. player_wielding[name] = nil
  68. end
  69. gauges.on_teleport()
  70. end
  71. minetest.register_globalstep(function(dtime)
  72. timer = timer + dtime
  73. if timer < update_time then
  74. return
  75. end
  76. periodic_reset = periodic_reset + update_time
  77. if periodic_reset > 20 then
  78. wield3d.on_teleport()
  79. periodic_reset = 0
  80. end
  81. gauges.on_global_step()
  82. local active_players = {}
  83. for _, player in pairs(minetest.get_connected_players()) do
  84. local name = player:get_player_name()
  85. local wield = player_wielding[name]
  86. if wield and wield.object then
  87. local stack = player:get_wielded_item()
  88. local item = stack:get_name() or ""
  89. if item ~= wield.item then
  90. wield.item = item
  91. if item == "" then
  92. item = "wield3d:hand"
  93. end
  94. local loc = wield3d.location[item] or location
  95. if loc[1] ~= wield.location[1] or
  96. not vector.equals(loc[2], wield.location[2]) or
  97. not vector.equals(loc[3], wield.location[3]) then
  98. --wield.object:set_detach()
  99. wield.object:set_attach(player, loc[1], loc[2], loc[3])
  100. wield.location = {loc[1], loc[2], loc[3]}
  101. end
  102. wield.object:set_properties({
  103. textures = {item},
  104. visual_size = loc[4],
  105. })
  106. end
  107. else
  108. add_wield_entity(player)
  109. end
  110. active_players[name] = true
  111. end
  112. for name, wield in pairs(player_wielding) do
  113. if not active_players[name] then
  114. if wield.object then
  115. wield.object:remove()
  116. end
  117. player_wielding[name] = nil
  118. end
  119. end
  120. timer = 0
  121. end)
  122. minetest.register_on_joinplayer(function(player)
  123. minetest.after(0.5, function() wield3d.on_teleport() end)
  124. end)
  125. minetest.register_on_leaveplayer(function(player)
  126. gauges.on_leaveplayer(player)
  127. local name = player:get_player_name()
  128. if name then
  129. local wield = player_wielding[name] or {}
  130. if wield.object then
  131. wield.object:remove()
  132. end
  133. player_wielding[name] = nil
  134. end
  135. end)