New storage backend (#6)

Co-authored-by: OgelGames <olliverdc28@gmail.com>
This commit is contained in:
Niklp 2023-07-20 07:35:03 +02:00 committed by GitHub
parent 25090d31fc
commit 3ef7cf9a93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 36 deletions

@ -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"

@ -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 = "<size>",
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()

2
settingtypes.txt Normal file

@ -0,0 +1,2 @@
# default hotbar size
hotbar_size (Default hotbar size) int 16 1 32