From 16afb9eddc5efa951c6a848b79bb1ec6412113ea Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 24 May 2023 14:44:38 -0700 Subject: [PATCH] Create setting_handler.lua --- .../utils/setting_handler.lua | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 worldeditadditions_core/utils/setting_handler.lua diff --git a/worldeditadditions_core/utils/setting_handler.lua b/worldeditadditions_core/utils/setting_handler.lua new file mode 100644 index 0000000..ab3fb45 --- /dev/null +++ b/worldeditadditions_core/utils/setting_handler.lua @@ -0,0 +1,42 @@ +--- A wrapper to simultaniously handle global and world settings. + +-- Initialize settings container +local weac = worldeditadditions_core +weac.settings = {} + +-- Initialize wea world folder if not already existing +local path = minetest.get_worldpath() .. "/worldeditadditions" +minetest.mkdir(path) + +-- @class +local setting_handler = {} + +--- Reads world settings into WEA core settings object +setting_handler.read = function() + local file, err = io.open(path .. "/settings.conf", "rb") + if err then return false end + -- Split by newline + local settings = wea_c.split(file.read(),"[\n\r]+") +end + +--- Write setting to world settings +setting_handler.write = function(setting, state) + local writer, err = io.open(path .. "/settings.conf", "ab") + if not writer then + return false + elseif setting == "" and not state then + writer:write("") + else + writer:write("worldeditadditions_" .. setting .. " = " .. state .. "\n") + end + writer:flush() + writer:close() + return true +end + +-- Test for world settings and generate file if none +if not setting_handler.read() then + setting_handler.write("") +end + +return setting_handler \ No newline at end of file