Some code clean, small bug fix, range extension from [4,16] to [1,16]

master
Aristotle Skotos 2018-05-02 08:32:06 +02:00
parent 35961a69f1
commit 13be1e80ec
5 changed files with 24 additions and 38 deletions

View File

@ -1,5 +1,5 @@
-- Minetest Mod: hotbar
-- Version: 0.1.0
-- Version: 0.1.1
-- Licence(s): see the attached license.txt file
-- Author: aristotle, a builder on the Red Cat Creative Server
--
@ -9,7 +9,7 @@
-- hotbar [size]
--
-- By itself, hotbar types the hotbar slots number in the chat;
-- when it is followed by a number in the correct range that is [4,16],
-- when it is followed by a number in the correct range that is now [1,16],
-- the command accordingly sets the new slots number.
--
-- Features:
@ -17,59 +17,49 @@
-- the "hotbar_slots" key in the configuration file.
--
-- Changelog:
-- - The hotbar is now correctly shown even when there are no items in it.
-- 0.1.1
-- - The code that did not properly show the error message when the
-- received size was out of bounds has been corrected
-- - The accepted range has been extended from [4,16] to [1,16].
-- - Some code optimization to avoid strings repetitions
-- 0.1.0
-- - The hotbar is now correctly shown even when there are no items in it.
--
-- FYI
-- The potential range of the hotbar slots number is [1,16]: the next update
-- will cover it too. :D
-- The potential range of the hotbar slots number should be [1,23]: the next
-- update will cover it too. :D
--
-- For now this is all folks: happy builds and explorations! :)
-- aristotle
local hb = {}
hb.min = 4
hb.min = 1
hb.max = 16
hb.default = 16
hb.setting ="hotbar_slots"
hb.current = minetest.setting_get(hb.setting) or hb.default -- The first time
hb.image = {}
hb.image.selected = "hotbar_slot_selected.png"
hb.image.bg = {nil, nil, nil,
"hotbar_slots_bg_4.png",
"hotbar_slots_bg_5.png",
"hotbar_slots_bg_6.png",
"hotbar_slots_bg_7.png",
"hotbar_slots_bg_8.png",
"hotbar_slots_bg_9.png",
"hotbar_slots_bg_10.png",
"hotbar_slots_bg_11.png",
"hotbar_slots_bg_12.png",
"hotbar_slots_bg_13.png",
"hotbar_slots_bg_14.png",
"hotbar_slots_bg_15.png",
"hotbar_slots_bg_16.png"}
function hb.show_min()
minetest.chat_send_player(name, "[_] The minimum slots number is " .. hb.min .. ".")
end
function hb.show_max()
minetest.chat_send_player(name, "[_] The maximum slots number is " .. hb.max .. ".")
hb.image.bg = {}
for i = 1, hb.max do
table.insert(hb.image.bg, string.format("hotbar_slots_bg_%i.png", i))
end
function hb.resize(size)
local new_size = tonumber(size)
return hb.image.bg[new_size]
local new_size = tonumber(size)
return hb.image.bg[new_size]
end
function hb.set(name, slots)
local mask = {err = "[_] Wrong slots number specified: the %s accepted value is %i.",
set = "[_] Hotbar slots number set to %i."}
local player = minetest.get_player_by_name(name)
if slots < hb.min then
hb.show_min()
minetest.chat_send_player(name, mask.err:format("minimum", hb.min))
return
end
if slots > hb.max then
hb.show_max()
minetest.chat_send_player(name, mask.err:format("maximum", hb.max))
return
end
player:hud_set_hotbar_itemcount(slots)
@ -77,17 +67,13 @@ function hb.set(name, slots)
player:hud_set_hotbar_image(hb.resize(slots))
minetest.setting_set(hb.setting, slots) -- automatically converted into a string
hb.current = slots
minetest.chat_send_player(name, "[_] Hotbar slots number set to " .. slots .. ".")
end
function hb.show(name, slots)
minetest.chat_send_player(name, "[_] Hotbar slots: " .. slots)
minetest.chat_send_player(name, mask.set:format(slots))
end
function hb.command(name, slots)
local new_slots = tonumber(slots)
if not new_slots then
hb.show(name, hb.current)
minetest.chat_send_player(name, "[_] Hotbar slots: " .. hb.current)
return
end
@ -104,7 +90,7 @@ end)
minetest.register_chatcommand("hotbar", {
params = "[size]",
description = "If size is passed then it sets your hotbar slots number in the range [4,16], else it displays the current slots number.",
description = string.format("If size is passed then it sets your hotbar slots number in the range [%i,%i], else it displays the current slots number.", hb.min, hb.max),
func = hb.command,
})

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 467 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 605 B