From 3ef7cf9a936de24dcb7bdc1f0308d3540e592712 Mon Sep 17 00:00:00 2001 From: Niklp <89982526+Niklp09@users.noreply.github.com> Date: Thu, 20 Jul 2023 07:35:03 +0200 Subject: [PATCH] New storage backend (#6) Co-authored-by: OgelGames --- .luacheckrc | 8 ++--- init.lua | 88 +++++++++++++++++++++++++++++++----------------- settingtypes.txt | 2 ++ 3 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 settingtypes.txt diff --git a/.luacheckrc b/.luacheckrc index 3a2c3c0..3840b4f 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,14 +1,10 @@ - read_globals = { -- Stdlib - string = {fields = {"split"}}, - table = {fields = {"copy", "getn"}}, - "VoxelManip", + "string", + "table", -- Minetest "minetest", "DIR_DELIM", - "vector", "ItemStack", - "dump", "VoxelArea", -- Deps "dreambuilder_theme" diff --git a/init.lua b/init.lua index 517bd07..3eb2423 100644 --- a/init.lua +++ b/init.lua @@ -1,25 +1,13 @@ +local storage = minetest.get_mod_storage() local themename = "" if minetest.global_exists("dreambuilder_theme") then themename = dreambuilder_theme.name.."_" end -local player_hotbar_settings = {} -local f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","r") -if f then - player_hotbar_settings = minetest.deserialize(f:read("*all")) - f:close() -end - -local function validate_size(s) - local size = s and tonumber(s) or 16 - return math.floor(0.5 + math.max(1, math.min(size, 32))) -end - -local hotbar_size_default = validate_size(minetest.settings:get("hotbar_size")) - local base_img = themename.."gui_hb_bg_1.png" local imgref_len = string.len(base_img) + 8 -- accounts for the stuff in the string.format() below. +local hotbar_size_default = 16 local img = {} for i = 0, 31 do @@ -27,34 +15,74 @@ for i = 0, 31 do end local hb_img = table.concat(img) -local function set_hotbar_size(player, s) - local hotbar_size = validate_size(s) +local function validate_size(s) + local size = s and tonumber(s) or hotbar_size_default + return math.floor(0.5 + math.max(1, math.min(size, 32))) +end + +hotbar_size_default = validate_size(minetest.settings:get("hotbar_size")) + +local function migrate_file2modstorage() + local path = minetest.get_worldpath()..DIR_DELIM.."hotbar_settings" + local f = io.open(path, "r") + if f then + local hotbar_sizes = minetest.deserialize(f:read("*all")) + f:close() + local counter = 0 + for name, size in pairs(hotbar_sizes) do + if size ~= hotbar_size_default then + storage:set_int(name, tonumber(size)) + counter = counter + 1 + end + end + os.remove(path) + minetest.log("action", "[dreambuilder_hotbar] Migrated " .. counter .. " player hotbars to modstorage") + end +end + +local function get_hotbar_size(player) + local name = player:get_player_name() + local meta = player:get_meta() + local size = meta:get_int("hotbar_size") + if size == 0 then -- not present + size = storage:get_int(name) + if size ~= 0 then -- migrate + storage:set_string(name, "") + meta:set_int("hotbar_size", size) + end + end + return size > 0 and size or hotbar_size_default +end + +local function update_hotbar(player, hotbar_size) player:hud_set_hotbar_itemcount(hotbar_size) player:hud_set_hotbar_selected_image(themename.."gui_hotbar_selected.png") player:hud_set_hotbar_image("[combine:"..(hotbar_size*64).."x64"..string.sub(hb_img, 1, hotbar_size*imgref_len)) +end + +local function set_hotbar_size(player, size) + local meta = player:get_meta() + local hotbar_size = validate_size(size) + if hotbar_size ~= hotbar_size_default then + meta:set_int("hotbar_size", hotbar_size) + end + update_hotbar(player, hotbar_size) return hotbar_size end minetest.register_on_joinplayer(function(player) - minetest.after(0.5,function() - set_hotbar_size(player, tonumber(player_hotbar_settings[player:get_player_name()]) or hotbar_size_default) + minetest.after(0.5, function() + update_hotbar(player, get_hotbar_size(player)) end) end) minetest.register_chatcommand("hotbar", { - params = "[size]", - description = "Sets the size of your hotbar, from 1 to 32 slots, default 16", + params = "", + description = "Sets the size of your hotbar, from 1 to 32 slots, default " .. hotbar_size_default, func = function(name, slots) local size = set_hotbar_size(minetest.get_player_by_name(name), slots) - player_hotbar_settings[name] = size minetest.chat_send_player(name, "[_] Hotbar size set to " ..size.. ".") - - f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","w") - if not f then - minetest.log("error","Failed to save hotbar settings") - else - f:write(minetest.serialize(player_hotbar_settings)) - f:close() - end - end, + end }) + +migrate_file2modstorage() \ No newline at end of file diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..bc09775 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,2 @@ +# default hotbar size +hotbar_size (Default hotbar size) int 16 1 32 \ No newline at end of file