mirror of
https://github.com/mt-mods/dreambuilder_hotbar.git
synced 2024-11-20 05:33:45 +01:00
b1cbaa24fc
* If user tries to set it to some length < 1 or > 32, it is capped at 1 or 32, respectively. * If user set it to something that doesn't evaluate to a number, it resets to 16.
73 lines
2.2 KiB
Lua
73 lines
2.2 KiB
Lua
local mtver = minetest.get_version()
|
|
local themename = dreambuilder_theme and dreambuilder_theme.name.."_" or ""
|
|
|
|
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 hb_img = ""
|
|
for i = 0, 31 do
|
|
hb_img = string.format("%s:%04i,0="..base_img, hb_img, i*64)
|
|
end
|
|
|
|
local function validate_size(s)
|
|
local size = s and tonumber(s) or 16
|
|
if size < 1 then
|
|
size = 1
|
|
elseif size > 32 then
|
|
size = 32
|
|
end
|
|
return size, ("[combine:"..(size*64).."x64"..string.sub(hb_img, 1, size*imgref_len))
|
|
end
|
|
|
|
local hotbar_size_default = validate_size(minetest.settings:get("hotbar_size"))
|
|
|
|
local player_hotbar_settings = {}
|
|
|
|
local function load_hotbar_settings()
|
|
local f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","r")
|
|
if not f then return end
|
|
local d = f:read("*all")
|
|
f:close()
|
|
player_hotbar_settings = minetest.deserialize(d)
|
|
end
|
|
|
|
local function save_hotbar_settings()
|
|
local f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","w")
|
|
if not f then
|
|
minetest.log("error","Failed to save hotbar settings")
|
|
return
|
|
end
|
|
local d = minetest.serialize(player_hotbar_settings)
|
|
f:write(d)
|
|
f:close()
|
|
end
|
|
|
|
local function get_hotbar_setting(name)
|
|
return tonumber(player_hotbar_settings[name]) or hotbar_size_default
|
|
end
|
|
|
|
load_hotbar_settings()
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
local hotbar_size, img = validate_size(get_hotbar_setting(player:get_player_name()))
|
|
player:hud_set_hotbar_itemcount(hotbar_size)
|
|
minetest.after(0.5,function(hotbar_size)
|
|
player:hud_set_hotbar_selected_image(themename.."gui_hotbar_selected.png")
|
|
player:hud_set_hotbar_image(img)
|
|
end,hotbar_size)
|
|
end)
|
|
|
|
minetest.register_chatcommand("hotbar", {
|
|
params = "[size]",
|
|
description = "Sets the size of your hotbar",
|
|
func = function(name, slots)
|
|
local hotbar_size, img = validate_size(tonumber(slots))
|
|
player_hotbar_settings[name] = hotbar_size
|
|
local player = minetest.get_player_by_name(name)
|
|
player:hud_set_hotbar_itemcount(hotbar_size)
|
|
minetest.chat_send_player(name, "[_] Hotbar size set to " ..hotbar_size.. ".")
|
|
player:hud_set_hotbar_image(img)
|
|
save_hotbar_settings()
|
|
end,
|
|
})
|