Binoculars: Only set property if necessary to reduce mesh reloading
authorparamat <paramat@users.noreply.github.com>
Mon, 30 Oct 2017 20:06:05 +0000 (20:06 +0000)
committerparamat <mat.gregory@virginmedia.com>
Fri, 3 Nov 2017 16:26:00 +0000 (16:26 +0000)
mods/binoculars/init.lua

index cd07ef9b7e0a7adbcc51ae98733747920d1db502..153044ae112de76cd6735473dbe3ee657df14abe 100644 (file)
@@ -3,7 +3,12 @@
 binoculars = {}
 
 
--- Cache creative mode setting
+-- Detect creative mod
+
+local creative_mod = minetest.get_modpath("creative")
+
+
+-- Cache creative mode setting as fallback if creative mod not present
 
 local creative_mode_cache = minetest.settings:get_bool("creative_mode")
 
@@ -13,14 +18,13 @@ local creative_mode_cache = minetest.settings:get_bool("creative_mode")
 
 function binoculars.update_player_property(player)
        local creative_enabled =
-               (creative and creative.is_enabled_for(player:get_player_name())) or
+               (creative_mod 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})
+       -- Only set property if necessary to avoid player mesh reload
+       local new_can_zoom = creative_enabled or player:get_inventory():contains_item(
+                       "main", "binoculars:binoculars")
+       if player:get_properties().can_zoom ~= new_can_zoom then
+               player:set_properties({can_zoom = new_can_zoom})
        end
 end