init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static_save = false,
  44. on_activate = function(self, staticdata)
  45. if staticdata == "expired" then
  46. self.object:remove()
  47. end
  48. end,
  49. on_punch = function(self)
  50. self.object:remove()
  51. end,
  52. get_staticdata = function(self)
  53. return "expired"
  54. end,
  55. })
  56. -- Shall be called whenever someone teleports.
  57. -- This mod requires to be able to reset the wield entities.
  58. function wield3d.on_teleport()
  59. -- Disabled, let's see if the devs finally fixed the entity attachment problems.
  60. -- 2024/6/1: re-enabled because sometimes wields disappear and don't reappear.
  61. ---[[
  62. local players = minetest.get_connected_players()
  63. -- Just clear all the wield entities. They will be restored shortly.
  64. for k, v in ipairs(players) do
  65. local name = v:get_player_name()
  66. local wield = player_wielding[name]
  67. if wield and wield.object then
  68. wield.object:set_detach()
  69. wield.object:remove()
  70. end
  71. player_wielding[name] = nil
  72. end
  73. gauges.on_teleport()
  74. city_block.on_teleport()
  75. --]]
  76. end
  77. minetest.register_globalstep(function(dtime)
  78. timer = timer + dtime
  79. if timer < update_time then
  80. return
  81. end
  82. periodic_reset = periodic_reset + update_time
  83. if periodic_reset > 20 then
  84. wield3d.on_teleport()
  85. periodic_reset = 0
  86. end
  87. gauges.on_global_step()
  88. local active_players = {}
  89. for _, player in pairs(minetest.get_connected_players()) do
  90. local name = player:get_player_name()
  91. local wield = player_wielding[name]
  92. if wield and wield.object then
  93. local stack = player:get_wielded_item()
  94. local item = stack:get_name() or ""
  95. if item ~= wield.item then
  96. wield.item = item
  97. if item == "" then
  98. item = "wield3d:hand"
  99. end
  100. local loc = wield3d.location[item] or location
  101. if loc[1] ~= wield.location[1] or
  102. not vector.equals(loc[2], wield.location[2]) or
  103. not vector.equals(loc[3], wield.location[3]) then
  104. --wield.object:set_detach()
  105. wield.object:set_attach(player, loc[1], loc[2], loc[3])
  106. wield.location = {loc[1], loc[2], loc[3]}
  107. end
  108. wield.object:set_properties({
  109. textures = {item},
  110. visual_size = loc[4],
  111. })
  112. end
  113. else
  114. add_wield_entity(player)
  115. end
  116. active_players[name] = true
  117. end
  118. for name, wield in pairs(player_wielding) do
  119. if not active_players[name] then
  120. if wield.object then
  121. wield.object:remove()
  122. end
  123. player_wielding[name] = nil
  124. end
  125. end
  126. timer = 0
  127. end)
  128. minetest.register_on_joinplayer(function(player)
  129. minetest.after(2, function() wield3d.on_teleport() end)
  130. end)
  131. minetest.register_on_leaveplayer(function(player)
  132. gauges.on_leaveplayer(player)
  133. local name = player:get_player_name()
  134. if name then
  135. local wield = player_wielding[name] or {}
  136. if wield.object then
  137. wield.object:remove()
  138. end
  139. player_wielding[name] = nil
  140. end
  141. end)