1
0
mirror of https://github.com/minetest/minetest_game.git synced 2025-06-30 05:40:24 +02:00

Add binoculars mod with binoculars item to allow zoom

Uses the new player object property 'can zoom'.
In survival mode, use of zoom requires the binoculars item in
inventory.
Zoom is automatically allowed in creative mode and for players with
the 'creative' privilege.
The binoculars.update_player_property() function is global so can be
redefined by a mod for alternative behaviour.
This commit is contained in:
paramat
2017-08-30 02:37:43 +01:00
committed by paramat
parent eb5a5b56e1
commit cc371ac726
5 changed files with 165 additions and 0 deletions

69
mods/binoculars/init.lua Normal file
View File

@ -0,0 +1,69 @@
-- Mod global namespace
binoculars = {}
-- Cache creative mode setting
local creative_mode_cache = minetest.settings:get_bool("creative_mode")
-- Update player property
-- Global to allow overriding
function binoculars.update_player_property(player)
local creative_enabled =
(creative and creative.is_enabled_for(player:get_player_name())) or
creative_mode_cache
if creative_enabled or
player:get_inventory():contains_item("main", "binoculars:binoculars") then
player:set_properties({can_zoom = true})
else
player:set_properties({can_zoom = false})
end
end
-- Set player property 'on joinplayer'
minetest.register_on_joinplayer(function(player)
binoculars.update_player_property(player)
end)
-- Cyclic update of player property
local function cyclic_update()
for _, player in ipairs(minetest.get_connected_players()) do
binoculars.update_player_property(player)
end
minetest.after(4.7, cyclic_update)
end
minetest.after(4.7, cyclic_update)
-- Binoculars item
minetest.register_craftitem("binoculars:binoculars", {
description = "Binoculars",
inventory_image = "binoculars_binoculars.png",
stack_max = 1,
on_use = function(itemstack, user, pointed_thing)
binoculars.update_player_property(user)
end,
})
-- Crafting
minetest.register_craft({
output = "binoculars:binoculars",
recipe = {
{"default:obsidian_glass", "", "default:obsidian_glass"},
{"default:bronze_ingot", "default:bronze_ingot", "default:bronze_ingot"},
{"default:obsidian_glass", "", "default:obsidian_glass"},
}
})