init.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. -- Mod global namespace
  2. binoculars = binoculars or {}
  3. binoculars.modpath = minetest.get_modpath("binoculars")
  4. -- Update player property
  5. -- Global to allow overriding
  6. -- May be called with player object or player name.
  7. function binoculars.update_player_property(player)
  8. if type(player) == "string" then
  9. player = minetest.get_player_by_name(player)
  10. end
  11. if not player or not player:is_player() then
  12. return
  13. end
  14. local new_zoom_fov = 0
  15. local have_binocs = false
  16. if player:get_wielded_item():get_name() == "binoculars:binoculars" then
  17. new_zoom_fov = 10
  18. have_binocs = true
  19. end
  20. -- Only set property if necessary to avoid player mesh reload
  21. if player:get_properties().zoom_fov ~= new_zoom_fov then
  22. player:set_properties({zoom_fov = new_zoom_fov})
  23. end
  24. if have_binocs then
  25. return true
  26. end
  27. end
  28. function binoculars.deploy(pname)
  29. if binoculars.update_player_property(pname) then
  30. minetest.after(1, function() binoculars.deploy(pname) end)
  31. end
  32. end
  33. function binoculars.on_use(itemstack, user, pointed_thing)
  34. binoculars.deploy(user:get_player_name())
  35. end
  36. if not binoculars.loaded then
  37. -- Binoculars item.
  38. minetest.register_node("binoculars:binoculars", {
  39. tiles = {"binoculars_binoculars.png"},
  40. wield_image = "binoculars_binoculars.png",
  41. description = "Binoculars\n\nUse (punch) and press the 'zoom' key.\nMust be wielded to continue using.",
  42. inventory_image = "binoculars_binoculars.png",
  43. paramtype = 'light',
  44. paramtype2 = "wallmounted",
  45. drawtype = "nodebox",
  46. sunlight_propagates = true,
  47. walkable = false,
  48. node_box = {
  49. type = "wallmounted",
  50. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  51. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  52. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  53. },
  54. selection_box = {type = "wallmounted"},
  55. stack_max = 1,
  56. groups = utility.dig_groups("bigitem", {flammable = 3, attached_node = 1}),
  57. sounds = default.node_sound_leaves_defaults(),
  58. on_use = function(...)
  59. return binoculars.on_use(...)
  60. end,
  61. })
  62. -- Crafting.
  63. minetest.register_craft({
  64. output = "binoculars:binoculars",
  65. recipe = {
  66. {"default:obsidian_glass", "", "default:obsidian_glass"},
  67. {"default:bronze_ingot", "brass:ingot", "default:bronze_ingot"},
  68. {"default:obsidian_glass", "", "default:obsidian_glass"},
  69. }
  70. })
  71. local c = "binoculars:core"
  72. local f = binoculars.modpath .. "/init.lua"
  73. reload.register_file(c, f, false)
  74. binoculars.loaded = true
  75. end