From 693afb61effcf5fdd797e6add23b0c8bf6d57006 Mon Sep 17 00:00:00 2001 From: Koldun Date: Sat, 15 Jun 2024 12:53:02 +0500 Subject: [PATCH] edited the code --- init.lua | 194 +++++-------------------------------------------------- mod.conf | 4 ++ 2 files changed, 21 insertions(+), 177 deletions(-) create mode 100644 mod.conf diff --git a/init.lua b/init.lua index 64b2969..19456da 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,9 @@ -- Minetest Mod: hotbar --- Version: 0.1.5a +-- Version: 0.1.5a+mtsr01 -- Licence(s): see the attached license.txt file --- Author: aristotle, a builder on Red Cat Creative +-- Authors: +-- - aristotle, a builder on Red Cat Creative +-- - VinAdmin -- -- This mod allows the player to set his/her own hotbar slots number -- by adding a new command. @@ -79,7 +81,7 @@ -- For now this is all folks: happy builds and explorations! :) -- aristotle -local VERSION = "0.1.5a" +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 = {} @@ -201,17 +203,10 @@ end local adjust_hotbar = function(name, slots, selected_image, bg_image_getter) local player = core.get_player_by_name(name) - if slots == 0 then - player:hud_set_hotbar_itemcount(1) - player:hud_set_hotbar_selected_image(selected_image) - player:hud_set_hotbar_image(bg_image_getter(1)) - player:hud_set_flags({hotbar = false, wielditem = false}) - else 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 end local hb = {} @@ -237,26 +232,15 @@ hb.slots.set = function(name, slots) if minetest.is_singleplayer() then display_name = '_' end - if slots < hb.slots.min then - core.chat_send_player(name, mask.err:format(display_name, "minimum", hb.slots.min)) - return - end - if slots > hb.slots.max then - core.chat_send_player(name, mask.err:format(display_name, "maximum", hb.slots.max)) - return - end + slots = math.floor(slots) -- to avoid fractions + hb.adjust(name, slots, hb.image.selected, hb.image.bg.get) if hb.mode.current == MODES.legacy then core.settings:set(hb.slots.key, slots) elseif hb.mode.current == MODES.world then 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 .. ".") elseif hb.mode.current == MODES.session then if core.is_singleplayer() then -- This is an ephemeral / transient storage that is to survive while in a map @@ -269,166 +253,22 @@ hb.slots.set = function(name, slots) DEFAULT.slots[hb.mode.current] = slots end else - 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!") return end if hb.mode.current ~= MODES.session then hb.slots.current = slots end - core.chat_send_player(name, mask.set:format(display_name, slots)) end -show_info = function(arg) - local normalize = function(request) - local rc = {mode = true, slots = true, version = true} - if type(request) ~= 'table' then - return rc - end - for k, v in pairs(request) do - if k == 'mode' or k == 'slots' or k == 'version' then - if type(v) ~= 'boolean' then - rc[k] = false - else - rc[k] = v - end - end - end - return rc - end - - local player = core.get_player_by_name(arg.name) - local out_name = arg.name - local out_mode = string.upper(arg.mode) - if core.is_singleplayer() then - out_name = "_" -- overridden - end - local message = string.format("[%s] ", out_name) - local slots = player:hud_get_hotbar_itemcount() - local flag = player:hud_get_flags() - if not flag.hotbar then - slots = 0 - end - local wanted = normalize(arg.wanted) - - if wanted.mode and wanted.slots then - message = message .. out_mode .. " / " .. slots - if wanted.version then - message = message .. " [" .. VERSION .. "]" - end - elseif wanted.mode then - message = message .. out_mode - elseif wanted.slots then - message = message .. slots - end - core.chat_send_player(arg.name, message) -end - -hb.slots.command = function(name, slots) - if slots == nil then - show_info({name = name, mode = hb.mode.current, wanted = {version = false, slots = true}}) - return - end - - local new_slots = tonumber(slots) - if type(new_slots) == 'number' then - hb.slots.set(name, new_slots) - else - -- new_slots type is nil => is type string? - hb.mode.command(name, slots) - end -end - -hb.mode.command = function(name, mode) - local singleplayer = core.is_singleplayer() - local display_name = name - if singleplayer then - display_name = '_' - end - local message = string.format("[%s] ", display_name) - - if #mode == 0 then - -- display current settings - show_info({name = name, mode = hb.mode.current, wanted = {version = false, slots = true, mode = true}}) - return - end - - mode = string.lower(mode) - - if MODES[mode] then - if singleplayer then - if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then - message = message .. "Hotbar mode changed to " .. string.upper(mode) .. "." - else - -- This is wrong and must be logged for further investigation - message = message .. "Your request to change the hotbar mode to " .. - string.upper(mode) .. " has been declined because it is unmanaged." - - core.log("error", "[MOD] hotbar v" .. VERSION .. ": " .. message) - core.chat_send_player(name, message) - return - end - else - -- not singleplayer - if mode ~= MODES.session then - message = message .. string.upper(mode) .. " mode cannot be set on a server." - else - message = message .. "Requesting to change mode and then trying to set the same one." - end - core.chat_send_player(name, message) - return - end - else - -- this might just be a mispelled mode, nothing to worry about: - -- no log is required. - message = message .. "Wrong hotbar mode - " .. - string.upper(mode) .. " - specified." - core.chat_send_player(name, message) - return - end - - if not singleplayer then - return - end - - if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then - core.settings:set(hb.mode.key, mode) - 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) - core.log("warning", "[MOD] hotbar v" .. VERSION .. ": " .. message) - core.chat_send_player(name, message) -end - -hb.on_joinplayer = function(player) - hb.adjust(player:get_player_name(), hb.slots.current, hb.image.selected, hb.image.bg.get) -end - -minetest.register_on_joinplayer(hb.on_joinplayer) - -minetest.register_chatcommand("hotbar", { - params = "[slots|mode]", - description = "Invoked with no argument, it displays" .. - " the current mode and slots number." .. - " To set" .. - " the slots number any integer in the range" .. - " [0, 23] is valid. If set to 0, the hotbar" .. - " gets hidden, any other number will unhide" .. - " it." .. - " Supported modes are " .. - stringified_table_keys(MODES, ", ") .. ".", - func = hb.slots.command, - privs = {interact = true}, +minetest.register_privilege("builder", { + description = "Privilege for builders", + give_to_singleplayer = false }) -core.log("action", - "[MOD] hotbar v" .. VERSION .. " operating in " .. hb.mode.current .. - " mode. Slots number is set to " .. hb.slots.current .. ".") - +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) \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..aa891d4 --- /dev/null +++ b/mod.conf @@ -0,0 +1,4 @@ +name = hotbar_16 +description = Hot panel for builders with 16 cells +min_minetest_version = 5.7 +title = Hotbar 16 \ No newline at end of file