init.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. local update_time = 2 -- number of seconds between wielditem updates
  2. local bone = "Armature_Wield_Item"
  3. dofile(minetest.get_modpath(minetest.get_current_modname()).."/rotation.lua")
  4. minetest.register_item("wield3d:hand", {
  5. type = "none",
  6. wield_image = "wield3d_trans.png",
  7. })
  8. minetest.register_entity("wield3d:wield_entity", {
  9. initial_properties = {
  10. physical = false,
  11. collisionbox = {x=0, y=0, z=0},
  12. visual = "wielditem",
  13. visual_size = {x=0.25, y=0.25},
  14. },
  15. wield_item = nil,
  16. player = nil,
  17. timer = 0,
  18. rotation = 0,
  19. on_step = function(self, dtime)
  20. local player = self.player
  21. if player == nil then
  22. self.object:remove()
  23. return
  24. end
  25. self.timer = self.timer + dtime
  26. if self.timer < update_time then
  27. return
  28. end
  29. self.timer = 0
  30. if minetest.env:get_player_by_name(player:get_player_name()) == nil then
  31. self.object:remove()
  32. return
  33. end
  34. local stack = player:get_wielded_item()
  35. local item = stack:get_name()
  36. if item == self.wield_item then
  37. return
  38. end
  39. self.wield_item = item
  40. if item == "" then
  41. item = "wield3d:hand"
  42. end
  43. local rotation = wield3d_rotation[item] or 0
  44. if rotation ~= self.rotation then
  45. self.object:setpos(player:getpos())
  46. self.object:set_detach()
  47. self.object:set_attach(player, bone, {x=0, y=0, z=0}, {x=0, y=0, z=rotation})
  48. self.rotation = rotation
  49. end
  50. self.object:set_properties({textures={item}})
  51. end,
  52. })
  53. minetest.register_on_joinplayer(function(player)
  54. minetest.after(1, function(player)
  55. player:set_properties({
  56. visual = "mesh",
  57. mesh = "wield3d_character.x",
  58. visual_size = {x=1, y=1},
  59. })
  60. local pos = player:getpos()
  61. local entity = minetest.env:add_entity(pos, "wield3d:wield_entity")
  62. if entity ~= nil then
  63. entity:set_attach(player, bone, {x=0, y=0, z=0}, {x=0, y=0, z=0})
  64. entity = entity:get_luaentity()
  65. entity.player = player
  66. end
  67. end, player)
  68. end)