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

This commit is contained in:
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 -- Minetest Mod: hotbar
-- Version: 0.1.0 -- Version: 0.1.1
-- Licence(s): see the attached license.txt file -- Licence(s): see the attached license.txt file
-- Author: aristotle, a builder on the Red Cat Creative Server -- Author: aristotle, a builder on the Red Cat Creative Server
-- --
@ -9,7 +9,7 @@
-- hotbar [size] -- hotbar [size]
-- --
-- By itself, hotbar types the hotbar slots number in the chat; -- 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. -- the command accordingly sets the new slots number.
-- --
-- Features: -- Features:
@ -17,59 +17,49 @@
-- the "hotbar_slots" key in the configuration file. -- the "hotbar_slots" key in the configuration file.
-- --
-- Changelog: -- 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 -- FYI
-- The potential range of the hotbar slots number is [1,16]: the next update -- The potential range of the hotbar slots number should be [1,23]: the next
-- will cover it too. :D -- update will cover it too. :D
-- --
-- For now this is all folks: happy builds and explorations! :) -- For now this is all folks: happy builds and explorations! :)
-- aristotle -- aristotle
local hb = {} local hb = {}
hb.min = 4 hb.min = 1
hb.max = 16 hb.max = 16
hb.default = 16 hb.default = 16
hb.setting ="hotbar_slots" hb.setting ="hotbar_slots"
hb.current = minetest.setting_get(hb.setting) or hb.default -- The first time hb.current = minetest.setting_get(hb.setting) or hb.default -- The first time
hb.image = {} hb.image = {}
hb.image.selected = "hotbar_slot_selected.png" hb.image.selected = "hotbar_slot_selected.png"
hb.image.bg = {nil, nil, nil, hb.image.bg = {}
"hotbar_slots_bg_4.png", for i = 1, hb.max do
"hotbar_slots_bg_5.png", table.insert(hb.image.bg, string.format("hotbar_slots_bg_%i.png", i))
"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 .. ".")
end end
function hb.resize(size) function hb.resize(size)
local new_size = tonumber(size) local new_size = tonumber(size)
return hb.image.bg[new_size] return hb.image.bg[new_size]
end end
function hb.set(name, slots) 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) local player = minetest.get_player_by_name(name)
if slots < hb.min then if slots < hb.min then
hb.show_min() minetest.chat_send_player(name, mask.err:format("minimum", hb.min))
return return
end end
if slots > hb.max then if slots > hb.max then
hb.show_max() minetest.chat_send_player(name, mask.err:format("maximum", hb.max))
return return
end end
player:hud_set_hotbar_itemcount(slots) player:hud_set_hotbar_itemcount(slots)
@ -77,17 +67,13 @@ function hb.set(name, slots)
player:hud_set_hotbar_image(hb.resize(slots)) player:hud_set_hotbar_image(hb.resize(slots))
minetest.setting_set(hb.setting, slots) -- automatically converted into a string minetest.setting_set(hb.setting, slots) -- automatically converted into a string
hb.current = slots hb.current = slots
minetest.chat_send_player(name, "[_] Hotbar slots number set to " .. slots .. ".") minetest.chat_send_player(name, mask.set:format(slots))
end
function hb.show(name, slots)
minetest.chat_send_player(name, "[_] Hotbar slots: " .. slots)
end end
function hb.command(name, slots) function hb.command(name, slots)
local new_slots = tonumber(slots) local new_slots = tonumber(slots)
if not new_slots then if not new_slots then
hb.show(name, hb.current) minetest.chat_send_player(name, "[_] Hotbar slots: " .. hb.current)
return return
end end
@ -104,7 +90,7 @@ end)
minetest.register_chatcommand("hotbar", { minetest.register_chatcommand("hotbar", {
params = "[size]", 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, 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