2018-04-23 08:45:35 +03:00
-- Minetest Mod: hotbar
2018-05-02 18:19:24 +03:00
-- Version: 0.1.2
2018-04-23 08:45:35 +03:00
-- Licence(s): see the attached license.txt file
-- Author: aristotle, a builder on the Red Cat Creative Server
--
-- This mod allows the player to set his/her own hotbar slots number
-- by adding a new command.
--
-- hotbar [size]
--
-- By itself, hotbar types the hotbar slots number in the chat;
2018-05-02 09:32:06 +03:00
-- when it is followed by a number in the correct range that is now [1,16],
2018-04-23 08:45:35 +03:00
-- the command accordingly sets the new slots number.
--
-- Features:
-- - It permanently stores the user's preference by setting and retrieving
-- the "hotbar_slots" key in the configuration file.
--
-- Changelog:
2018-05-02 18:17:11 +03:00
-- 0.1.2
-- - Some of the existing textures have been modfied to comply with an even
-- size (as recommended)
-- - The missing textures have been added.
-- - Some of the textures have been renamed to better support sorting.
-- - As a consequence, the size range has been extended from [1,16] to the
-- fully supported one [1,23]
2018-05-02 09:32:06 +03:00
-- 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.
2018-04-23 08:45:35 +03:00
--
-- FYI
2018-05-02 09:32:06 +03:00
-- The potential range of the hotbar slots number should be [1,23]: the next
-- update will cover it too. :D
2018-04-23 08:45:35 +03:00
--
-- For now this is all folks: happy builds and explorations! :)
-- aristotle
local hb = { }
2018-05-02 09:32:06 +03:00
hb.min = 1
2018-05-02 18:17:11 +03:00
hb.max = 23
2018-04-23 08:45:35 +03:00
hb.default = 16
hb.setting = " hotbar_slots "
hb.current = minetest.setting_get ( hb.setting ) or hb.default -- The first time
hb.image = { }
2018-05-02 18:17:11 +03:00
hb.image . selected = " hotbar_selected_slot.png "
2018-05-02 09:32:06 +03:00
hb.image . bg = { }
for i = 1 , hb.max do
2018-05-02 18:17:11 +03:00
table.insert ( hb.image . bg , string.format ( " hotbar_slots_bg_%02i.png " , i ) )
2018-04-23 08:45:35 +03:00
end
function hb . resize ( size )
2018-05-02 09:32:06 +03:00
local new_size = tonumber ( size )
return hb.image . bg [ new_size ]
2018-04-23 08:45:35 +03:00
end
function hb . set ( name , slots )
2018-05-02 09:32:06 +03:00
local mask = { err = " [_] Wrong slots number specified: the %s accepted value is %i. " ,
set = " [_] Hotbar slots number set to %i. " }
2018-04-23 08:45:35 +03:00
local player = minetest.get_player_by_name ( name )
if slots < hb.min then
2018-05-02 09:32:06 +03:00
minetest.chat_send_player ( name , mask.err : format ( " minimum " , hb.min ) )
2018-04-23 08:45:35 +03:00
return
end
if slots > hb.max then
2018-05-02 09:32:06 +03:00
minetest.chat_send_player ( name , mask.err : format ( " maximum " , hb.max ) )
2018-04-23 08:45:35 +03:00
return
end
player : hud_set_hotbar_itemcount ( slots )
player : hud_set_hotbar_selected_image ( hb.image . selected )
player : hud_set_hotbar_image ( hb.resize ( slots ) )
minetest.setting_set ( hb.setting , slots ) -- automatically converted into a string
hb.current = slots
2018-05-02 09:32:06 +03:00
minetest.chat_send_player ( name , mask.set : format ( slots ) )
2018-04-23 08:45:35 +03:00
end
function hb . command ( name , slots )
local new_slots = tonumber ( slots )
if not new_slots then
2018-05-02 09:32:06 +03:00
minetest.chat_send_player ( name , " [_] Hotbar slots: " .. hb.current )
2018-04-23 08:45:35 +03:00
return
end
hb.set ( name , new_slots )
end
minetest.register_on_joinplayer ( function ( player )
player : hud_set_hotbar_itemcount ( hb.current )
minetest.after ( 0.5 , function ( )
player : hud_set_hotbar_selected_image ( hb.image . selected )
player : hud_set_hotbar_image ( hb.resize ( hb.current ) )
end )
end )
minetest.register_chatcommand ( " hotbar " , {
params = " [size] " ,
2018-05-02 09:32:06 +03:00
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 ) ,
2018-04-23 08:45:35 +03:00
func = hb.command ,
} )