edited the code

This commit is contained in:
Koldun 2024-06-15 12:53:02 +05:00
parent b382c6bf9e
commit 693afb61ef
Signed by: Koldun
GPG Key ID: 53DE683337F5D25F
2 changed files with 21 additions and 177 deletions

194
init.lua
View File

@ -1,7 +1,9 @@
-- Minetest Mod: hotbar -- Minetest Mod: hotbar
-- Version: 0.1.5a -- Version: 0.1.5a+mtsr01
-- Licence(s): see the attached license.txt file -- 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 -- This mod allows the player to set his/her own hotbar slots number
-- by adding a new command. -- by adding a new command.
@ -79,7 +81,7 @@
-- For now this is all folks: happy builds and explorations! :) -- For now this is all folks: happy builds and explorations! :)
-- aristotle -- 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 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 DEFAULT = {mode = MODES.world, slots = {legacy = 16, world = 10, session = 12}}
local MOD_STORAGE = {} local MOD_STORAGE = {}
@ -201,17 +203,10 @@ end
local adjust_hotbar = function(name, slots, selected_image, bg_image_getter) local adjust_hotbar = function(name, slots, selected_image, bg_image_getter)
local player = core.get_player_by_name(name) 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_itemcount(slots)
player:hud_set_hotbar_selected_image(selected_image) player:hud_set_hotbar_selected_image(selected_image)
player:hud_set_hotbar_image(bg_image_getter(slots)) player:hud_set_hotbar_image(bg_image_getter(slots))
player:hud_set_flags({hotbar = true, wielditem = true}) player:hud_set_flags({hotbar = true, wielditem = true})
end
end end
local hb = {} local hb = {}
@ -237,26 +232,15 @@ hb.slots.set = function(name, slots)
if minetest.is_singleplayer() then if minetest.is_singleplayer() then
display_name = '_' display_name = '_'
end 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 slots = math.floor(slots) -- to avoid fractions
hb.adjust(name, slots, hb.image.selected, hb.image.bg.get) hb.adjust(name, slots, hb.image.selected, hb.image.bg.get)
if hb.mode.current == MODES.legacy then if hb.mode.current == MODES.legacy then
core.settings:set(hb.slots.key, slots) core.settings:set(hb.slots.key, slots)
elseif hb.mode.current == MODES.world then elseif hb.mode.current == MODES.world then
MOD_STORAGE.settings:set_string(hb.slots.key, core.serialize(slots)) 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 elseif hb.mode.current == MODES.session then
if core.is_singleplayer() then if core.is_singleplayer() then
-- This is an ephemeral / transient storage that is to survive while in a map -- 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 DEFAULT.slots[hb.mode.current] = slots
end end
else 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 return
end end
if hb.mode.current ~= MODES.session then if hb.mode.current ~= MODES.session then
hb.slots.current = slots hb.slots.current = slots
end end
core.chat_send_player(name, mask.set:format(display_name, slots))
end end
show_info = function(arg) minetest.register_privilege("builder", {
local normalize = function(request) description = "Privilege for builders",
local rc = {mode = true, slots = true, version = true} give_to_singleplayer = false
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},
}) })
core.log("action", minetest.register_on_joinplayer(function(player)
"[MOD] hotbar v" .. VERSION .. " operating in " .. hb.mode.current .. if minetest.check_player_privs(player, {builder=true}) then
" mode. Slots number is set to " .. hb.slots.current .. ".") --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)

4
mod.conf Normal file
View File

@ -0,0 +1,4 @@
name = hotbar_16
description = Hot panel for builders with 16 cells
min_minetest_version = 5.7
title = Hotbar 16