init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. wield3d = {}
  2. wield3d.modpath = minetest.get_modpath("wield3d")
  3. dofile(wield3d.modpath .. "/location.lua")
  4. local update_time_conf = minetest.settings: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:get_pos()
  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. -- Disabled, let's see if the devs finally fixed the entity attachment problems.
  59. --[[
  60. local players = minetest.get_connected_players()
  61. -- Just clear all the wield entities. They will be restored shortly.
  62. for k, v in ipairs(players) do
  63. local name = v:get_player_name()
  64. local wield = player_wielding[name]
  65. if wield and wield.object then
  66. wield.object:set_detach()
  67. wield.object:remove()
  68. end
  69. player_wielding[name] = nil
  70. end
  71. gauges.on_teleport()
  72. --]]
  73. end
  74. minetest.register_globalstep(function(dtime)
  75. timer = timer + dtime
  76. if timer < update_time then
  77. return
  78. end
  79. periodic_reset = periodic_reset + update_time
  80. if periodic_reset > 20 then
  81. wield3d.on_teleport()
  82. periodic_reset = 0
  83. end
  84. gauges.on_global_step()
  85. local active_players = {}
  86. for _, player in pairs(minetest.get_connected_players()) do
  87. local name = player:get_player_name()
  88. local wield = player_wielding[name]
  89. if wield and wield.object then
  90. local stack = player:get_wielded_item()
  91. local item = stack:get_name() or ""
  92. if item ~= wield.item then
  93. wield.item = item
  94. if item == "" then
  95. item = "wield3d:hand"
  96. end
  97. local loc = wield3d.location[item] or location
  98. if loc[1] ~= wield.location[1] or
  99. not vector.equals(loc[2], wield.location[2]) or
  100. not vector.equals(loc[3], wield.location[3]) then
  101. --wield.object:set_detach()
  102. wield.object:set_attach(player, loc[1], loc[2], loc[3])
  103. wield.location = {loc[1], loc[2], loc[3]}
  104. end
  105. wield.object:set_properties({
  106. textures = {item},
  107. visual_size = loc[4],
  108. })
  109. end
  110. else
  111. add_wield_entity(player)
  112. end
  113. active_players[name] = true
  114. end
  115. for name, wield in pairs(player_wielding) do
  116. if not active_players[name] then
  117. if wield.object then
  118. wield.object:remove()
  119. end
  120. player_wielding[name] = nil
  121. end
  122. end
  123. timer = 0
  124. end)
  125. minetest.register_on_joinplayer(function(player)
  126. minetest.after(0.5, function() wield3d.on_teleport() end)
  127. end)
  128. minetest.register_on_leaveplayer(function(player)
  129. gauges.on_leaveplayer(player)
  130. local name = player:get_player_name()
  131. if name then
  132. local wield = player_wielding[name] or {}
  133. if wield.object then
  134. wield.object:remove()
  135. end
  136. player_wielding[name] = nil
  137. end
  138. end)