hotbar_16/init.lua

214 lines
7.6 KiB
Lua
Raw Normal View History

2018-04-23 08:45:35 +03:00
-- Minetest Mod: hotbar
2024-06-15 10:53:02 +03:00
-- Version: 0.1.5a+mtsr01
2018-04-23 08:45:35 +03:00
-- Licence(s): see the attached license.txt file
2024-06-15 10:53:02 +03:00
-- Authors:
-- - aristotle, a builder on Red Cat Creative
-- - VinAdmin
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;
-- when it is followed by a number in the correct range that is now [0,23],
2018-04-23 08:45:35 +03:00
-- the command accordingly sets the new slots number.
--
-- Features:
-- - It may permanently store the user's preferences by setting & retrieving
-- the "hotbar_slots" & "hotbar_mode" keys in the configuration file.
-- - 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-27 21:11:31 +03:00
-- 0.1.5a
-- - Running luacheck underlined that a couple of fixes were needed:
-- 1. normalize() scope is now local
-- 2. a few lines had their trailing blanks removed.
-- - A couple of "security fixes" have been applied to disallow any
-- floating value inside minetest.conf and the modstorage that might
-- have made the mod fail at load time in legacy and world mode.
2018-08-24 18:36:30 +03:00
-- 0.1.5
-- - Went back to just one command: hotbar, now improved to just not take
-- the size, but the mode as well.
-- - Some feedback messages have been fixed.
-- 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).
-- 0.1.3
-- - New update to assure the full API support because the version 0.1.2
-- update seems to have gone wrong for some reason. (SORRY. Ty _Xenon)
-- The accepted slots number should now be in the range [1,23]!
-- 0.1.2
-- - Some of the existing textures have been modified 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]
-- 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
2024-06-15 10:53:02 +03:00
local VERSION = "0.1.5a+mtsr01"
local MODES = {legacy = "legacy", world = "world", session = "session"} -- this redundancy simplifies later checks
local DEFAULT = {mode = MODES.world, slots = {legacy = 16, world = 10, session = 12}}
local MOD_STORAGE = {}
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
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
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)
end
return value
2018-04-23 08:45:35 +03:00
end
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
mode_value = MODES.session
default_value = DEFAULT.slots[mode_value]
end
if mode_value == MODES.legacy then
2018-08-24 10:45:27 +03:00
local result = tonumber(core.settings:get(key))
current = result or default_value -- The first time
if not result then
-- first time
2018-08-24 10:45:27 +03:00
core.settings:set(key, current)
else
current = math.floor(result)
if current ~= result then
-- result is a float
core.settings:set(key, current)
end
end
elseif mode_value == MODES.world then
2018-08-24 10:45:27 +03:00
local result = core.deserialize(storage.settings:get_string(key))
if type(result) == "number" then
current = math.floor(result)
if current ~= result then
-- result is a float
storage.settings:set_string(key, core.serialize(current))
end
else
current = default_value -- The first time
2018-08-24 10:45:27 +03:00
storage.settings:set_string(key, core.serialize(current))
end
elseif mode_value == MODES.session then
current = default_value -- Session initial value
else
current = default_value -- Unplanned case
end
return current
2018-04-23 08:45:35 +03:00
end
local adjust_hotbar = function(name, slots, selected_image, bg_image_getter)
2018-08-24 10:45:27 +03:00
local player = core.get_player_by_name(name)
player:hud_set_hotbar_itemcount(slots)
player:hud_set_hotbar_selected_image(selected_image)
player:hud_set_hotbar_image(bg_image_getter(slots))
player:hud_set_flags({hotbar = true, wielditem = true})
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)
slots = math.floor(slots) -- to avoid fractions
hb.adjust(name, slots, hb.image.selected, hb.image.bg.get)
end
2024-06-15 10:53:02 +03:00
minetest.register_privilege("builder", {
description = "Privilege for builders",
give_to_singleplayer = false
})
2024-06-15 10:53:02 +03:00
minetest.register_on_joinplayer(function(player)
if minetest.check_player_privs(player, {builder=true}) then
--player:hud_set_hotbar_itemcount(tonumber(16))
--player:hud_set_hotbar_image("")
hb.adjust(player:get_player_name(), 16, hb.image.selected, hb.image.bg.get)
end
end)