local prefs = {} local path = minetest.get_worldpath()..DIR_DELIM.."daynight.txt" local file = io.open(path,"r") if file then local data = file:read("*all") prefs = minetest.deserialize(data) file:close() end local function save() local file = io.open(path,"w") local data = minetest.serialize(prefs) file:write(data) file:close() end local form = "size[6,2.25]".. "label[2,-0.25;Select a day/night mode:]".. "image_button_exit[0,0.25;2,2;"..minetest.formspec_escape("[combine:128x128:16,16=ui_moon_icon.png:-16,-16=ui_sun_icon.png")..";normal;]".. "image_button_exit[2,0.25;2,2;ui_sun_icon.png;day;]".. "image_button_exit[4,0.25;2,2;ui_moon_icon.png;night;]".. "label[0.5,2;Automatic]".. "label[2.5,2;Always Day]".. "label[4.5,2;Always Night]" minetest.register_chatcommand("dayratio", { params = "[ratio]", description = "Sets your day/night ratio to the given value, or opens a GUI if a value is not given", func = function(name,param) if param ~= "" and param ~= "nil" then if not tonumber(param) then return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-1 or nil." elseif tonumber(param) > 1 then return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Value must be nil or <= 1." elseif tonumber(param) < 0 then return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Only positive values are allowed." end end local player = minetest.get_player_by_name(name) if param == "nil" then prefs[name] = nil player:override_day_night_ratio(nil) return true,minetest.colorize("#00FFFF","Success")..": Day/night ratio override cleared." elseif tonumber(param) then prefs[name] = tonumber(param) player:override_day_night_ratio(tonumber(param)) return true,minetest.colorize("#00FFFF","Success")..": Day/night ratio set to "..param else minetest.show_formspec(name,"daynight:daynight",form) end save() end } ) minetest.register_on_player_receive_fields( function(player,formname,fields) local name = player:get_player_name() if formname ~= "daynight:daynight" then return false end if fields.normal then prefs[name] = nil player:override_day_night_ratio(nil) elseif fields.day then prefs[name] = 1 player:override_day_night_ratio(1) elseif fields.night then prefs[name] = 0 player:override_day_night_ratio(0) end save() end ) minetest.register_on_joinplayer( function(player) local name = player:get_player_name() player:override_day_night_ratio(prefs[name]) end ) unified_inventory.register_button("daynight", { action = function(player) minetest.show_formspec(player:get_player_name(),"daynight:daynight",form) end, tooltip = "Day/Night Options", type = "image", image = "[combine:128x128:16,16=ui_moon_icon.png:-16,-16=ui_sun_icon.png" } )