2023-07-20 07:35:03 +02:00
|
|
|
local storage = minetest.get_mod_storage()
|
2021-11-29 19:18:58 +01:00
|
|
|
local themename = ""
|
|
|
|
|
|
|
|
if minetest.global_exists("dreambuilder_theme") then
|
|
|
|
themename = dreambuilder_theme.name.."_"
|
|
|
|
end
|
2018-12-03 00:27:45 +01:00
|
|
|
|
2023-07-20 07:35:03 +02:00
|
|
|
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
|
|
|
|
img[i+1] = string.format(":%04i,0=%s", i*64, base_img)
|
2021-03-25 02:57:59 +01:00
|
|
|
end
|
2023-07-20 07:35:03 +02:00
|
|
|
local hb_img = table.concat(img)
|
2021-03-25 02:57:59 +01:00
|
|
|
|
2018-12-03 00:27:45 +01:00
|
|
|
local function validate_size(s)
|
2023-07-20 07:35:03 +02:00
|
|
|
local size = s and tonumber(s) or hotbar_size_default
|
2021-03-25 06:07:11 +01:00
|
|
|
return math.floor(0.5 + math.max(1, math.min(size, 32)))
|
2018-12-03 00:27:45 +01:00
|
|
|
end
|
|
|
|
|
2023-07-20 07:35:03 +02:00
|
|
|
hotbar_size_default = validate_size(minetest.settings:get("hotbar_size"))
|
2018-11-11 05:23:16 +01:00
|
|
|
|
2023-07-20 07:35:03 +02:00
|
|
|
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
|
2018-11-11 05:26:59 +01:00
|
|
|
|
2023-07-20 07:35:03 +02:00
|
|
|
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
|
2018-11-11 05:26:59 +01:00
|
|
|
end
|
|
|
|
|
2023-07-20 07:35:03 +02:00
|
|
|
local function update_hotbar(player, hotbar_size)
|
2021-03-25 03:59:36 +01:00
|
|
|
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))
|
2023-07-20 07:35:03 +02:00
|
|
|
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)
|
2021-03-25 03:59:36 +01:00
|
|
|
return hotbar_size
|
2018-11-11 05:26:59 +01:00
|
|
|
end
|
|
|
|
|
2018-11-11 05:23:16 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2023-07-20 07:35:03 +02:00
|
|
|
minetest.after(0.5, function()
|
|
|
|
update_hotbar(player, get_hotbar_size(player))
|
2021-11-29 19:18:58 +01:00
|
|
|
end)
|
2018-11-11 05:23:16 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_chatcommand("hotbar", {
|
2023-07-20 07:35:03 +02:00
|
|
|
params = "<size>",
|
|
|
|
description = "Sets the size of your hotbar, from 1 to 32 slots, default " .. hotbar_size_default,
|
2018-11-11 05:23:16 +01:00
|
|
|
func = function(name, slots)
|
2021-03-25 03:59:36 +01:00
|
|
|
local size = set_hotbar_size(minetest.get_player_by_name(name), slots)
|
|
|
|
minetest.chat_send_player(name, "[_] Hotbar size set to " ..size.. ".")
|
2023-07-20 07:35:03 +02:00
|
|
|
end
|
2018-11-11 05:23:16 +01:00
|
|
|
})
|
2023-07-20 07:35:03 +02:00
|
|
|
|
|
|
|
migrate_file2modstorage()
|