init.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. -- Mod global namespace
  2. if not minetest.global_exists("binoculars") then binoculars = {} end
  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. if have_binocs then
  21. pova.set_modifier(player, "properties", {zoom_fov=new_zoom_fov}, "binoculars")
  22. return true
  23. else
  24. pova.remove_modifier(player, "properties", "binoculars")
  25. end
  26. end
  27. function binoculars.deploy(pname)
  28. if binoculars.update_player_property(pname) then
  29. minetest.after(1, function() binoculars.deploy(pname) end)
  30. end
  31. end
  32. function binoculars.on_use(itemstack, user, pointed_thing)
  33. binoculars.deploy(user:get_player_name())
  34. end
  35. if not binoculars.loaded then
  36. -- Binoculars item.
  37. minetest.register_node("binoculars:binoculars", {
  38. tiles = {"binoculars_binoculars.png"},
  39. wield_image = "binoculars_binoculars.png",
  40. description = "Binoculars\n\nUse (punch) and press the 'zoom' key.\nMust be wielded to continue using.",
  41. inventory_image = "binoculars_binoculars.png",
  42. paramtype = 'light',
  43. paramtype2 = "wallmounted",
  44. drawtype = "nodebox",
  45. sunlight_propagates = true,
  46. walkable = false,
  47. node_box = {
  48. type = "wallmounted",
  49. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  50. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  51. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  52. },
  53. selection_box = {type = "wallmounted"},
  54. stack_max = 1,
  55. groups = utility.dig_groups("bigitem", {flammable = 3, attached_node = 1}),
  56. sounds = default.node_sound_leaves_defaults(),
  57. on_use = function(...)
  58. return binoculars.on_use(...)
  59. end,
  60. })
  61. -- Crafting.
  62. minetest.register_craft({
  63. output = "binoculars:binoculars",
  64. recipe = {
  65. {"default:obsidian_glass", "", "default:obsidian_glass"},
  66. {"default:bronze_ingot", "brass:ingot", "default:bronze_ingot"},
  67. {"default:obsidian_glass", "", "default:obsidian_glass"},
  68. }
  69. })
  70. local c = "binoculars:core"
  71. local f = binoculars.modpath .. "/init.lua"
  72. reload.register_file(c, f, false)
  73. binoculars.loaded = true
  74. end