init.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -- binoculars/init.lua
  2. -- Mod global namespace
  3. binoculars = {}
  4. -- Load support for MT game translation.
  5. local S = minetest.get_translator("binoculars")
  6. -- Detect creative mod
  7. local creative_mod = minetest.get_modpath("creative")
  8. -- Cache creative mode setting as fallback if creative mod not present
  9. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  10. -- Update player property
  11. -- Global to allow overriding
  12. function binoculars.update_player_property(player)
  13. local creative_enabled =
  14. (creative_mod and creative.is_enabled_for(player:get_player_name())) or
  15. creative_mode_cache
  16. local new_zoom_fov = 0
  17. if player:get_inventory():contains_item(
  18. "main", "binoculars:binoculars") then
  19. new_zoom_fov = 10
  20. elseif creative_enabled then
  21. new_zoom_fov = 15
  22. end
  23. -- Only set property if necessary to avoid player mesh reload
  24. if player:get_properties().zoom_fov ~= new_zoom_fov then
  25. player:set_properties({zoom_fov = new_zoom_fov})
  26. end
  27. end
  28. -- Set player property 'on joinplayer'
  29. minetest.register_on_joinplayer(function(player)
  30. binoculars.update_player_property(player)
  31. end)
  32. -- Cyclic update of player property
  33. local function cyclic_update()
  34. for _, player in ipairs(minetest.get_connected_players()) do
  35. binoculars.update_player_property(player)
  36. end
  37. minetest.after(4.7, cyclic_update)
  38. end
  39. minetest.after(4.7, cyclic_update)
  40. -- Binoculars item
  41. minetest.register_craftitem("binoculars:binoculars", {
  42. description = S("Binoculars") .. "\n" .. S("Use with 'Zoom' key"),
  43. inventory_image = "binoculars_binoculars.png",
  44. stack_max = 1,
  45. on_use = function(itemstack, user, pointed_thing)
  46. binoculars.update_player_property(user)
  47. end,
  48. })
  49. -- Crafting
  50. minetest.register_craft({
  51. output = "binoculars:binoculars",
  52. recipe = {
  53. {"default:obsidian_glass", "", "default:obsidian_glass"},
  54. {"default:bronze_ingot", "default:bronze_ingot", "default:bronze_ingot"},
  55. {"default:obsidian_glass", "", "default:obsidian_glass"},
  56. }
  57. })