From a71489327aa6b906de31720309cc61b8a3a50667 Mon Sep 17 00:00:00 2001 From: Till Affeldt Date: Fri, 24 Mar 2023 20:47:18 +0100 Subject: [PATCH] Refactor compatibility patches --- compatibility/enable_shadows.lua | 44 ++++++++++++++++++++++++++++++++ compatibility/weather.lua | 14 ++++++++++ init.lua | 35 ++++++++++--------------- mod.conf | 2 +- 4 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 compatibility/enable_shadows.lua create mode 100644 compatibility/weather.lua diff --git a/compatibility/enable_shadows.lua b/compatibility/enable_shadows.lua new file mode 100644 index 0000000..218a03c --- /dev/null +++ b/compatibility/enable_shadows.lua @@ -0,0 +1,44 @@ +--[[ + This code is mostly taken from the original enable_shadows mod + and tweaked to work with the new monoids. + enable_shadows is licensed under MIT, respective Copyright (c) 2022 ROllerozxa +]] + +local S = minetest.get_translator("enable_shadows") +local storage = minetest.get_mod_storage() + +local default_intensity = tonumber(minetest.settings:get("enable_shadows_default_intensity") or 0.33) +local intensity = tonumber(storage:get("intensity") or default_intensity) + +minetest.register_on_joinplayer(function(player) + lighting_monoids.shadows:add_change(player, intensity, "enable_shadows:base_value") +end) + +minetest.override_chatcommand("shadow_intensity", { + func = function(name, param) + local new_intensity + if param ~= "" then + new_intensity = tonumber(param) or nil + else + new_intensity = tonumber(default_intensity) or nil + end + + if new_intensity < 0 or new_intensity > 1 or new_intensity == nil then + minetest.chat_send_player(name, minetest.colorize("#ff0000", S("Invalid intensity."))) + return true + end + + if new_intensity ~= default_intensity then + minetest.chat_send_player(name, S("Set intensity to @1.", new_intensity)) + storage:set_float("intensity", new_intensity) + else + minetest.chat_send_player(name, S("Set intensity to default value (@1).", default_intensity)) + storage:set_string("intensity", "") + end + + intensity = new_intensity + for _,player in pairs(minetest.get_connected_players()) do + lighting_monoids.shadows:add_change(player, new_intensity, "enable_shadows:base_value") + end + end +}) \ No newline at end of file diff --git a/compatibility/weather.lua b/compatibility/weather.lua new file mode 100644 index 0000000..440c9ce --- /dev/null +++ b/compatibility/weather.lua @@ -0,0 +1,14 @@ +-- Hook into MTG weather mod for compatibility (requires PR #3020) +if weather ~= nil and weather.on_update ~= nil then + weather.on_update = function(player, overrides) + if overrides == nil then + return + end + if overrides.shadows and overrides.shadows.intensity then + local intensity = overrides.shadows.intensity + lighting_monoids.shadows:add_change(player, intensity, "weather:cloud_shadows") + end + overrides.lighting = nil + return overrides + end +end \ No newline at end of file diff --git a/init.lua b/init.lua index 37055ae..4ec3f9c 100644 --- a/init.lua +++ b/init.lua @@ -3,6 +3,8 @@ lighting_monoids = {} local SET_BASE_SHADOW = minetest.settings:get_bool("lighting_monoids.set_base_shadow", true) local BASE_SHADOW_INTENSITY = tonumber(minetest.settings:get("lighting_monoids.base_shadow_intensity") or 0.33) +local MODPATH = minetest.get_modpath(minetest.get_current_modname()) + local function multiply(a, b) if a == nil then a = 1 end if b == nil then b = 1 end @@ -28,7 +30,9 @@ lighting_monoids.shadows = player_monoids.make_monoid({ local lighting = player:get_lighting() lighting.shadows = lighting.shadows or {} lighting.shadows.intensity = multiplier - player:set_lighting(lighting) + if player.set_lighting ~= nil then + player:set_lighting(lighting) + end end }) @@ -40,31 +44,18 @@ lighting_monoids.saturation = player_monoids.make_monoid({ apply = function(multiplier, player) local lighting = player:get_lighting() lighting.saturation = multiplier - player:set_lighting(lighting) + if player.set_lighting ~= nil then + player:set_lighting(lighting) + end end }) --- Hook into MTG weather mod for compatibility (requires PR #3020) -if minetest.get_modpath("weather") and weather and weather.on_update then - weather.on_update = function(player, overrides) - if overrides == nil then - return - end - if overrides.shadows and overrides.shadows.intensity then - local intensity = overrides.shadows.intensity - lighting_monoids.shadows:add_change(player, intensity, "weather:cloud_shadows") - end - overrides.lighting = nil - return overrides - end +if minetest.get_modpath("weather") then + dofile(MODPATH .. DIR_DELIM .. "compatibility" .. DIR_DELIM .. "weather.lua") +end --- set base shadow intensity according to mod config --- only basic integration, doesn't update when command is used -elseif minetest.get_modpath("enable_shadows") then - local intensity = tonumber(minetest.settings:get("enable_shadows_default_intensity") or 0.33) - minetest.register_on_joinplayer(function(player) - lighting_monoids.shadows:add_change(player, intensity, "enable_shadows:base_value") - end) +if minetest.get_modpath("enable_shadows") then + dofile(MODPATH .. DIR_DELIM .. "compatibility" .. DIR_DELIM .. "enable_shadows.lua") -- set base shadow elseif SET_BASE_SHADOW then diff --git a/mod.conf b/mod.conf index 1fedbbc..3f2b0d2 100644 --- a/mod.conf +++ b/mod.conf @@ -2,7 +2,7 @@ name = lighting_monoids title = Monoids for Player Lighting author = TestificateMods depends = player_monoids -optional_depends = enable_shadows,weather +optional_depends = enable_shadows, weather description = """ """ \ No newline at end of file