init.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. -- Mod global namespace
  2. binoculars = {}
  3. -- Detect creative mod
  4. local creative_mod = minetest.get_modpath("creative")
  5. -- Cache creative mode setting as fallback if creative mod not present
  6. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  7. -- Update player property
  8. -- Global to allow overriding
  9. function binoculars.update_player_property(player)
  10. -- local creative_enabled =
  11. -- (creative_mod and creative.is_enabled_for(player:get_player_name())) or
  12. -- creative_mode_cache
  13. local new_zoom_fov = 0
  14. if player:get_inventory():contains_item(
  15. "main", "binoculars:binoculars") then
  16. new_zoom_fov = 10
  17. -- elseif creative_enabled then
  18. -- new_zoom_fov = 15
  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. end
  25. -- Set player property 'on joinplayer'
  26. minetest.register_on_joinplayer(function(player)
  27. binoculars.update_player_property(player)
  28. end)
  29. -- Cyclic update of player property
  30. local function cyclic_update()
  31. for _, player in ipairs(minetest.get_connected_players()) do
  32. binoculars.update_player_property(player)
  33. end
  34. minetest.after(4.7, cyclic_update)
  35. end
  36. minetest.after(4.7, cyclic_update)
  37. -- Binoculars item
  38. minetest.register_craftitem("binoculars:binoculars", {
  39. description = "Binoculars\nUse with 'Zoom' key",
  40. inventory_image = "binoculars_binoculars.png",
  41. stack_max = 1,
  42. on_use = function(itemstack, user, pointed_thing)
  43. binoculars.update_player_property(user)
  44. end,
  45. })
  46. -- Crafting
  47. minetest.register_craft({
  48. output = "binoculars:binoculars",
  49. recipe = {
  50. {"default:glass", "", "default:glass"},
  51. {"default:bronze_ingot", "default:bronze_ingot", "default:bronze_ingot"},
  52. {"default:glass", "", "default:glass"},
  53. }
  54. })