init.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. -- Update player property
  7. -- Global to allow overriding
  8. function binoculars.update_player_property(player)
  9. local new_zoom_fov = 0
  10. if player:get_inventory():contains_item(
  11. "main", "binoculars:binoculars") then
  12. new_zoom_fov = 10
  13. elseif minetest.is_creative_enabled(player:get_player_name()) then
  14. new_zoom_fov = 15
  15. end
  16. -- Only set property if necessary to avoid player mesh reload
  17. if player:get_properties().zoom_fov ~= new_zoom_fov then
  18. player:set_properties({zoom_fov = new_zoom_fov})
  19. end
  20. end
  21. -- Set player property 'on joinplayer'
  22. minetest.register_on_joinplayer(function(player)
  23. binoculars.update_player_property(player)
  24. end)
  25. -- Cyclic update of player property
  26. local function cyclic_update()
  27. for _, player in ipairs(minetest.get_connected_players()) do
  28. binoculars.update_player_property(player)
  29. end
  30. minetest.after(4.7, cyclic_update)
  31. end
  32. minetest.after(4.7, cyclic_update)
  33. -- Binoculars item
  34. minetest.register_craftitem("binoculars:binoculars", {
  35. description = S("Binoculars") .. "\n" .. S("Use with 'Zoom' key"),
  36. inventory_image = "binoculars_binoculars.png",
  37. stack_max = 1,
  38. groups = {tool = 1},
  39. on_use = function(itemstack, user, pointed_thing)
  40. binoculars.update_player_property(user)
  41. end,
  42. })
  43. -- Crafting
  44. minetest.register_craft({
  45. output = "binoculars:binoculars",
  46. recipe = {
  47. {"default:obsidian_glass", "", "default:obsidian_glass"},
  48. {"default:bronze_ingot", "default:bronze_ingot", "default:bronze_ingot"},
  49. {"default:obsidian_glass", "", "default:obsidian_glass"},
  50. }
  51. })