2018-04-23 08:45:35 +03:00
|
|
|
-- Minetest Mod: hotbar
|
2018-08-23 15:46:09 +03:00
|
|
|
-- Version: 0.1.4
|
2018-04-23 08:45:35 +03:00
|
|
|
-- Licence(s): see the attached license.txt file
|
2018-08-23 16:16:14 +03:00
|
|
|
-- Author: aristotle, a builder on Red Cat Creative
|
2018-04-23 08:45:35 +03:00
|
|
|
--
|
|
|
|
-- 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-07-22 22:18:57 +03:00
|
|
|
-- when it is followed by a number in the correct range that is now [1,23],
|
2018-04-23 08:45:35 +03:00
|
|
|
-- the command accordingly sets the new slots number.
|
|
|
|
--
|
|
|
|
-- Features:
|
2018-08-23 21:13:18 +03:00
|
|
|
-- - It may permanently store the user's preferences by setting & retrieving
|
|
|
|
-- the "hotbar_slots" & "hotbar_mode" keys in the configuration file.
|
2018-08-23 15:46:09 +03:00
|
|
|
-- - For those of us who are running MT 0.4.16+, the hotbar size may be
|
|
|
|
-- different in each world / map.
|
2018-04-23 08:45:35 +03:00
|
|
|
--
|
|
|
|
-- Changelog:
|
2018-08-23 15:46:09 +03:00
|
|
|
-- 0.1.4
|
|
|
|
-- - A new command - /hotbar_mode - has been added to take advantage of
|
|
|
|
-- the 0.4.16+ mod_storage API.
|
|
|
|
-- In singleplayer, the suggested default mode for the most recent
|
|
|
|
-- clients (0.4.16+) should now be WORLD, because when this mode is
|
|
|
|
-- active a different size can be stored in each world.
|
|
|
|
-- The original global behavior / mode is now called LEGACY, and
|
|
|
|
-- should be the default and only one for any MT < 0.4.16
|
|
|
|
-- because of the lack of the mod_storage API (not tested).
|
|
|
|
-- No further effort will be put to provide an alternative storage
|
|
|
|
-- system for any MT < 0.4.16.
|
|
|
|
-- A third mode - SESSION - is the only mode available when the mod
|
|
|
|
-- is running on a server. The player can change the slots number,
|
|
|
|
-- but every new session its value is restored to the default.
|
|
|
|
-- The SESSION mode is available in singleplayer too.
|
|
|
|
-- - The slots range has been extended to include the 0: because of this
|
|
|
|
-- it is now possible to hide the hotbar, the wielded item (and,
|
|
|
|
-- consequently, the hand).
|
|
|
|
-- - The deprecated minetest.setting_{get,set}() calls have been removed:
|
|
|
|
-- this avoids some warnings, but might at the same time limit the
|
|
|
|
-- compatibility with MT versions < 0.4.16 (not tested too).
|
|
|
|
-- - The code has been refactoried to better support future comprehension
|
|
|
|
-- and extensibility (eg for a clientmod version): most of it should
|
|
|
|
-- now be hopefully self explainatory to me! and even to the occasional
|
|
|
|
-- modder. :D
|
|
|
|
-- - FIXES
|
|
|
|
-- - The slots number is now checked not to be a float (ty GreenDimond).
|
2018-07-22 22:18:57 +03:00
|
|
|
-- 0.1.3
|
|
|
|
-- - New update to assure the full API support because the version 0.1.2
|
2018-08-23 15:46:09 +03:00
|
|
|
-- update seems to have gone wrong for some reason. (SORRY. Ty _Xenon)
|
2018-07-22 22:18:57 +03:00
|
|
|
-- The accepted slots number should now be in the range [1,23]!
|
2018-05-02 18:17:11 +03:00
|
|
|
-- 0.1.2
|
2018-07-22 22:18:57 +03:00
|
|
|
-- - Some of the existing textures have been modified to comply with an even
|
2018-05-02 18:17:11 +03:00
|
|
|
-- 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
|
|
|
--
|
|
|
|
-- For now this is all folks: happy builds and explorations! :)
|
|
|
|
-- aristotle
|
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
local VERSION = "0.1.4"
|
|
|
|
local MODES = {legacy = "legacy", world = "world", session = "session"} -- this redundancy simplifies later checks
|
2018-08-23 16:28:09 +03:00
|
|
|
local DEFAULT = {mode = MODES.world, slots = {legacy = 16, world = 10, session = 12}}
|
2018-08-23 15:46:09 +03:00
|
|
|
local MOD_STORAGE = {}
|
2018-08-24 10:45:27 +03:00
|
|
|
if not core.get_mod_storage then
|
2018-08-23 15:46:09 +03:00
|
|
|
-- MT < 0.4.16
|
|
|
|
MOD_STORAGE.present = false
|
|
|
|
MOD_STORAGE.settings = false
|
|
|
|
else
|
|
|
|
-- MT 0.4.16+
|
|
|
|
MOD_STORAGE.present = true
|
2018-08-24 10:45:27 +03:00
|
|
|
MOD_STORAGE.settings = core.get_mod_storage()
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local stringified_table_keys = function(what, sep)
|
|
|
|
-- what: table
|
|
|
|
-- sep: keys separator (a string)
|
|
|
|
local rc = ""
|
|
|
|
for k, v in pairs(what) do
|
|
|
|
rc = rc .. k .. sep
|
|
|
|
end
|
|
|
|
return string.sub(rc, 1, -#sep - 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
local new_masked_array = function(mask, max)
|
|
|
|
local rc = {}
|
|
|
|
for i = 1, max do
|
|
|
|
table.insert(rc, string.format(mask, i))
|
|
|
|
end
|
|
|
|
return rc
|
|
|
|
end
|
|
|
|
|
|
|
|
local read_mode = function(key, default_value)
|
2018-08-24 10:45:27 +03:00
|
|
|
if not core.is_singleplayer() then
|
2018-08-24 10:09:33 +03:00
|
|
|
return MODES.session
|
|
|
|
end
|
2018-08-24 10:45:27 +03:00
|
|
|
local value = core.settings:get(key)
|
2018-08-24 10:09:33 +03:00
|
|
|
if type(value) ~= "string" or #value == 0 then
|
|
|
|
return default_value
|
|
|
|
end
|
|
|
|
value = string.lower(value)
|
|
|
|
if not MODES[value] then
|
|
|
|
value = default_value
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
|
|
|
local get_mode = function(storage, key, default_value)
|
2018-08-24 10:45:27 +03:00
|
|
|
if not core.is_singleplayer() then
|
2018-08-24 10:09:33 +03:00
|
|
|
return MODES.session
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
local value = read_mode(key, default_value)
|
|
|
|
local wrong = false
|
|
|
|
if value == MODES.world then
|
|
|
|
if not storage.present then
|
|
|
|
value = MODES.legacy
|
|
|
|
wrong = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if wrong then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.settings:set(key, value)
|
|
|
|
core.log("error",
|
|
|
|
"[MOD] hotbar v" .. VERSION ..
|
|
|
|
" automatically changed and saved the mode. " ..
|
|
|
|
"The mode has now been set to " ..
|
|
|
|
string.upper(value) .. ".")
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
return value
|
2018-04-23 08:45:35 +03:00
|
|
|
end
|
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
local get_and_set_initial_slots = function(storage, mode_value, key, default_value)
|
|
|
|
local current
|
2018-08-24 10:45:27 +03:00
|
|
|
if not core.is_singleplayer() then
|
2018-08-23 15:46:09 +03:00
|
|
|
mode_value = MODES.session
|
|
|
|
end
|
|
|
|
|
|
|
|
if mode_value == MODES.legacy then
|
2018-08-24 10:45:27 +03:00
|
|
|
local result = tonumber(core.settings:get(key))
|
2018-08-23 15:46:09 +03:00
|
|
|
current = result or default_value -- The first time
|
|
|
|
if not result then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.settings:set(key, current)
|
2018-08-23 15:46:09 +03:00
|
|
|
else
|
|
|
|
result = math.floor(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif mode_value == MODES.world then
|
2018-08-24 10:45:27 +03:00
|
|
|
local result = core.deserialize(storage.settings:get_string(key))
|
2018-08-23 15:46:09 +03:00
|
|
|
if type(result) == "number" then
|
|
|
|
current = result
|
|
|
|
else
|
|
|
|
current = default_value -- The first time
|
2018-08-24 10:45:27 +03:00
|
|
|
storage.settings:set_string(key, core.serialize(current))
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
elseif mode_value == MODES.session then
|
|
|
|
current = default_value -- Session initial value
|
|
|
|
|
|
|
|
else
|
|
|
|
current = default_value -- Unplanned case
|
2018-08-24 10:45:27 +03:00
|
|
|
core.log("error",
|
|
|
|
"[MOD] hotbar v" .. VERSION ..
|
|
|
|
": the specified mode - " .. string.upper(mode_value) ..
|
|
|
|
" - is unmanaged and has been overridden and set to " ..
|
|
|
|
string.upper(default_value) .. ".")
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
return current
|
2018-04-23 08:45:35 +03:00
|
|
|
end
|
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
local adjust_hotbar = function(name, slots, selected_image, bg_image)
|
2018-08-24 10:45:27 +03:00
|
|
|
local player = core.get_player_by_name(name)
|
2018-08-23 15:46:09 +03:00
|
|
|
if slots == 0 then
|
|
|
|
player:hud_set_flags({hotbar = false, wielditem = false})
|
|
|
|
else
|
|
|
|
player:hud_set_flags({hotbar = true, wielditem = true})
|
|
|
|
player:hud_set_hotbar_itemcount(slots)
|
|
|
|
player:hud_set_hotbar_selected_image(selected_image)
|
|
|
|
player:hud_set_hotbar_image(bg_image)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local hb = {}
|
|
|
|
|
|
|
|
hb.adjust = adjust_hotbar
|
|
|
|
|
|
|
|
hb.mode = { key = "hotbar_mode" }
|
|
|
|
hb.slots = { key = "hotbar_slots", min = 0, max = 23 }
|
|
|
|
hb.image = { selected = "hotbar_selected_slot.png", bg = {} }
|
|
|
|
|
|
|
|
hb.mode.current = get_mode(MOD_STORAGE, hb.mode.key, DEFAULT.mode)
|
|
|
|
hb.slots.current = get_and_set_initial_slots(MOD_STORAGE, hb.mode.current, hb.slots.key, DEFAULT.slots[hb.mode.current])
|
|
|
|
hb.image.bg.array = new_masked_array("hotbar_slots_bg_%02i.png", hb.slots.max)
|
|
|
|
|
|
|
|
hb.image.bg.get = function(slots)
|
|
|
|
return hb.image.bg.array[tonumber(slots)]
|
|
|
|
end
|
|
|
|
|
|
|
|
hb.slots.set = function(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-08-23 15:46:09 +03:00
|
|
|
if slots < hb.slots.min then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.chat_send_player(name, mask.err:format("minimum", hb.slots.min))
|
|
|
|
return
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
if slots > hb.slots.max then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.chat_send_player(name, mask.err:format("maximum", hb.slots.max))
|
|
|
|
return
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
slots = math.floor(slots) -- to avoid fractions
|
|
|
|
hb.adjust(name, slots, hb.image.selected, hb.image.bg.get(slots))
|
|
|
|
|
|
|
|
if hb.mode.current == MODES.legacy then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.settings:set(hb.slots.key, slots)
|
2018-08-23 15:46:09 +03:00
|
|
|
elseif hb.mode.current == MODES.world then
|
2018-08-24 10:45:27 +03:00
|
|
|
MOD_STORAGE.settings:set_string(hb.slots.key, core.serialize(slots))
|
|
|
|
core.log("warning",
|
|
|
|
"[MOD] hotbar v" .. VERSION ..
|
|
|
|
" operating in " .. hb.mode.current ..
|
|
|
|
" mode: " .. name ..
|
|
|
|
" has changed the slots number to " .. slots .. ".")
|
2018-08-23 15:46:09 +03:00
|
|
|
elseif hb.mode.current == MODES.session then
|
2018-08-24 10:45:27 +03:00
|
|
|
if core.is_singleplayer() then
|
|
|
|
-- This is an ephemeral / transient storage that is to survive while in a map
|
2018-08-23 15:46:09 +03:00
|
|
|
-- and trying different hotbar modes.
|
|
|
|
-- As a commodity, singleplayer can override the default value to get it back
|
|
|
|
-- if he/she switched back from another mode during the same session.
|
|
|
|
-- This does not have to happen on a server to avoid that other players
|
|
|
|
-- overrided it or that their default / current value might be overridden by
|
|
|
|
-- others.
|
|
|
|
DEFAULT.slots[hb.mode.current] = slots
|
|
|
|
end
|
|
|
|
else
|
2018-08-24 10:45:27 +03:00
|
|
|
core.log("error",
|
|
|
|
"[MOD] hotbar v" .. VERSION ..
|
|
|
|
": it is still not possible to set the slots number in " ..
|
|
|
|
string.upper(hb.mode.current) .. " mode.")
|
|
|
|
core.chat_send_player(name, string.upper(hb.mode.current) .. " mode is not managed yet!")
|
2018-08-23 15:46:09 +03:00
|
|
|
return
|
|
|
|
end
|
2018-08-23 22:02:00 +03:00
|
|
|
if hb.mode.current ~= MODES.session then
|
|
|
|
hb.slots.current = slots
|
|
|
|
end
|
2018-08-24 10:45:27 +03:00
|
|
|
core.chat_send_player(name, mask.set:format(slots))
|
2018-04-23 08:45:35 +03:00
|
|
|
end
|
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
hb.slots.command = function(name, slots)
|
|
|
|
local new_slots = tonumber(slots)
|
|
|
|
if not new_slots then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.chat_send_player(name, "[_] Hotbar slots: " .. hb.slots.current)
|
2018-08-23 15:46:09 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
hb.slots.set(name, new_slots)
|
|
|
|
end
|
|
|
|
|
|
|
|
hb.mode.command = function(name, mode)
|
|
|
|
local message
|
|
|
|
|
2018-08-24 10:45:27 +03:00
|
|
|
if not core.is_singleplayer() or #mode == 0 then
|
2018-08-23 15:46:09 +03:00
|
|
|
-- display current settings
|
2018-08-24 10:45:27 +03:00
|
|
|
local player = core.get_player_by_name(name)
|
|
|
|
core.chat_send_player(name, "[_] Hotbar mode: " .. string.upper(hb.mode.current))
|
|
|
|
core.chat_send_player(name, "[_] Hotbar slots: " .. player:hud_get_hotbar_itemcount())
|
|
|
|
core.chat_send_player(name, "[_] Hotbar version: " .. VERSION)
|
2018-08-23 15:46:09 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
mode = string.lower(mode)
|
|
|
|
if MODES[mode] then
|
|
|
|
if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then
|
|
|
|
message = "hotbar mode changed to " .. string.upper(mode)
|
|
|
|
else
|
|
|
|
-- This is wrong and must be logged for further investigation
|
|
|
|
message = "Your request to change the hotbar mode to " ..
|
|
|
|
string.upper(mode) .. " has been declined because it is unmanaged."
|
|
|
|
|
2018-08-24 10:45:27 +03:00
|
|
|
core.log("error", "[MOD] hotbar v" .. VERSION .. ": " .. message)
|
|
|
|
core.chat_send_player(name, "[_] " .. message)
|
2018-08-23 15:46:09 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- this might just be a mispelled mode, nothing to worry about:
|
|
|
|
-- no log is required.
|
|
|
|
message = "[_] Wrong hotbar mode - " ..
|
|
|
|
string.upper(mode) .. " - specified."
|
2018-08-24 10:45:27 +03:00
|
|
|
core.chat_send_player(name, message)
|
2018-08-23 15:46:09 +03:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then
|
2018-08-24 10:45:27 +03:00
|
|
|
core.settings:set(hb.mode.key, mode)
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
|
|
|
hb.mode.current = mode
|
|
|
|
hb.slots.current = get_and_set_initial_slots(MOD_STORAGE, hb.mode.current, hb.slots.key, DEFAULT.slots[hb.mode.current])
|
|
|
|
hb.slots.set(name, hb.slots.current)
|
2018-08-24 10:45:27 +03:00
|
|
|
core.log("warning", "[MOD] hotbar v" .. VERSION .. ": [" .. name .. "] " .. message)
|
|
|
|
core.chat_send_player(name, "[_] " .. message)
|
2018-08-23 15:46:09 +03:00
|
|
|
end
|
2018-04-23 08:45:35 +03:00
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
hb.on_joinplayer = function(player)
|
|
|
|
hb.adjust(player:get_player_name(), hb.slots.current, hb.image.selected, hb.image.bg.get(hb.slots.current))
|
2018-04-23 08:45:35 +03:00
|
|
|
end
|
|
|
|
|
2018-08-23 15:46:09 +03:00
|
|
|
minetest.register_on_joinplayer(hb.on_joinplayer)
|
2018-04-23 08:45:35 +03:00
|
|
|
|
|
|
|
minetest.register_chatcommand("hotbar", {
|
2018-08-24 10:45:27 +03:00
|
|
|
params = "[slots]",
|
|
|
|
description = string.format("If slots is not passed then it displays" ..
|
|
|
|
" the current slots number, else if it is" ..
|
|
|
|
" set to 0 then the hotbar gets hidden," ..
|
|
|
|
" while any other value in the range " ..
|
|
|
|
" [%i,%i] will show and accordingly " ..
|
|
|
|
" resize the hotbar.",
|
|
|
|
hb.slots.min + 1,
|
|
|
|
hb.slots.max),
|
|
|
|
func = hb.slots.command,
|
|
|
|
privs = {interact = true},
|
2018-08-23 15:46:09 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("hotbar_mode", {
|
2018-08-24 10:45:27 +03:00
|
|
|
params = "[mode]",
|
|
|
|
description = "If mode is not passed then it shows the current mode, " ..
|
|
|
|
"else it will change the mode to one of the supported " ..
|
|
|
|
"ones: " .. stringified_table_keys(MODES, ", ") .. ".",
|
|
|
|
func = hb.mode.command,
|
|
|
|
privs = {interact = true},
|
2018-04-23 08:45:35 +03:00
|
|
|
})
|
|
|
|
|
2018-08-24 10:45:27 +03:00
|
|
|
core.log("action", "[MOD] hotbar v" .. VERSION .. " operating in " .. hb.mode.current .. " mode. Slots number is set to " .. hb.slots.current .. ".")
|
2018-04-23 08:45:35 +03:00
|
|
|
|