mirror of
https://github.com/mt-mods/dreambuilder_hotbar.git
synced 2025-01-12 06:17:34 +01:00
code improvements
* create all hotbar images on load * round hotbar size before clamping * move migration function to bottom of file * check if player is online in chat command * return message from chat command instead of sending chat message
This commit is contained in:
parent
ed6db2e32d
commit
837b886c7b
85
init.lua
85
init.lua
@ -1,52 +1,39 @@
|
||||
local storage = minetest.get_mod_storage()
|
||||
local themename = ""
|
||||
|
||||
local storage = minetest.get_mod_storage()
|
||||
|
||||
local themename = ""
|
||||
if minetest.global_exists("dreambuilder_theme") then
|
||||
themename = dreambuilder_theme.name.."_"
|
||||
end
|
||||
|
||||
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_selected_image = themename.."gui_hotbar_selected.png"
|
||||
local hotbar_image = {}
|
||||
do
|
||||
local hotbar_slot = themename.."gui_hb_bg_1.png"
|
||||
local str = ""
|
||||
for i = 0, 31 do
|
||||
str = str..string.format(":%i,0=%s", i*64, hotbar_slot)
|
||||
hotbar_image[i+1] = string.format("[combine:%ix64%s", (i+1)*64, str)
|
||||
end
|
||||
end
|
||||
|
||||
local hotbar_size_default = 16
|
||||
|
||||
local img = {}
|
||||
for i = 0, 31 do
|
||||
img[i+1] = string.format(":%04i,0=%s", i*64, base_img)
|
||||
end
|
||||
local hb_img = table.concat(img)
|
||||
|
||||
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)))
|
||||
local size = tonumber(s) or hotbar_size_default
|
||||
return math.max(1, math.min(math.floor(size + 0.5), 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
|
||||
if size == 0 then
|
||||
-- Migrate from modstorage if present
|
||||
size = storage:get_int(name)
|
||||
if size ~= 0 then -- migrate
|
||||
if size ~= 0 then
|
||||
storage:set_string(name, "")
|
||||
meta:set_int("hotbar_size", size)
|
||||
end
|
||||
@ -56,8 +43,8 @@ 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))
|
||||
player:hud_set_hotbar_selected_image(hotbar_selected_image)
|
||||
player:hud_set_hotbar_image(hotbar_image[hotbar_size])
|
||||
end
|
||||
|
||||
local function set_hotbar_size(player, size)
|
||||
@ -81,11 +68,33 @@ end)
|
||||
|
||||
minetest.register_chatcommand("hotbar", {
|
||||
params = "<size>",
|
||||
description = "Sets the size of your hotbar, from 1 to 32 slots, default " .. hotbar_size_default,
|
||||
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)
|
||||
minetest.chat_send_player(name, "[_] Hotbar size set to " ..size.. ".")
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if not player then
|
||||
return false, "Player is not online."
|
||||
end
|
||||
local size = set_hotbar_size(player, slots)
|
||||
return true, "Hotbar size set to "..size
|
||||
end
|
||||
})
|
||||
|
||||
migrate_file2modstorage()
|
||||
-- Migrate hotbar settings from file to modstorage
|
||||
local function migrate_storage()
|
||||
local path = minetest.get_worldpath()..DIR_DELIM.."hotbar_settings"
|
||||
local file = io.open(path, "r")
|
||||
if file then
|
||||
local hotbar_sizes = minetest.deserialize(file:read("*all"))
|
||||
file:close()
|
||||
local count = 0
|
||||
for name, size in pairs(hotbar_sizes) do
|
||||
if size ~= hotbar_size_default then
|
||||
storage:set_int(name, tonumber(size))
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
os.remove(path)
|
||||
minetest.log("action", "[dreambuilder_hotbar] Migrated "..count.." player hotbars to modstorage")
|
||||
end
|
||||
end
|
||||
migrate_storage()
|
||||
|
Loading…
Reference in New Issue
Block a user