singleplayer / network usage check for feedback purposes

This commit is contained in:
Aristotle 2018-08-24 17:29:35 +02:00
parent be959496f5
commit 26df76cbe5

128
init.lua
View File

@ -68,7 +68,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.4" local VERSION = "0.1.5"
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 = {}
@ -178,15 +178,18 @@ local get_and_set_initial_slots = function(storage, mode_value, key, default_val
return current return current
end end
local adjust_hotbar = function(name, slots, selected_image, bg_image) 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 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}) player:hud_set_flags({hotbar = false, wielditem = false})
else else
player:hud_set_flags({hotbar = true, wielditem = true})
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) player:hud_set_hotbar_image(bg_image_getter(slots))
player:hud_set_flags({hotbar = true, wielditem = true})
end end
end end
@ -207,18 +210,22 @@ hb.image.bg.get = function(slots)
end end
hb.slots.set = function(name, slots) hb.slots.set = function(name, slots)
local mask = {err = "[_] Wrong slots number specified: the %s accepted value is %i.", local mask = {err = "[%s] Wrong slots number specified: the %s accepted value is %i.",
set = "[_] Hotbar slots number set to %i."} set = "[%s] Hotbar slots number set to %i."}
local display_name = name
if minetest.is_singleplayer() then
display_name = '_'
end
if slots < hb.slots.min then if slots < hb.slots.min then
core.chat_send_player(name, mask.err:format("minimum", hb.slots.min)) core.chat_send_player(name, mask.err:format(display_name, "minimum", hb.slots.min))
return return
end end
if slots > hb.slots.max then if slots > hb.slots.max then
core.chat_send_player(name, mask.err:format("maximum", hb.slots.max)) core.chat_send_player(name, mask.err:format(display_name, "maximum", hb.slots.max))
return return
end 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(slots)) 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)
@ -251,16 +258,67 @@ hb.slots.set = function(name, slots)
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(slots)) core.chat_send_player(name, mask.set:format(display_name, slots))
end
show_info = function(arg)
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 end
hb.slots.command = function(name, slots) hb.slots.command = function(name, slots)
local new_slots = tonumber(slots) if slots == nil then
if not new_slots then show_info({name = name, mode = hb.mode.current, wanted = {version = false, slots = true}})
core.chat_send_player(name, "[_] Hotbar slots: " .. hb.slots.current)
return return
end end
local new_slots = tonumber(slots)
if type(new_slots) == 'number' then
hb.slots.set(name, new_slots) hb.slots.set(name, new_slots)
else
-- new_slots type is nil => is type string?
hb.mode.command(name, slots)
end
end end
hb.mode.command = function(name, mode) hb.mode.command = function(name, mode)
@ -268,17 +326,14 @@ hb.mode.command = function(name, mode)
if not core.is_singleplayer() or #mode == 0 then if not core.is_singleplayer() or #mode == 0 then
-- display current settings -- display current settings
local player = core.get_player_by_name(name) show_info({name = name, mode = hb.mode.current, wanted = {version = false, slots = true, mode = true}})
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)
return return
end end
mode = string.lower(mode) mode = string.lower(mode)
if MODES[mode] then if MODES[mode] then
if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then
message = "hotbar mode changed to " .. string.upper(mode) message = "Hotbar mode changed to " .. string.upper(mode) .. "."
else else
-- This is wrong and must be logged for further investigation -- This is wrong and must be logged for further investigation
message = "Your request to change the hotbar mode to " .. message = "Your request to change the hotbar mode to " ..
@ -307,33 +362,34 @@ hb.mode.command = function(name, mode)
end end
hb.on_joinplayer = function(player) hb.on_joinplayer = function(player)
hb.adjust(player:get_player_name(), hb.slots.current, hb.image.selected, hb.image.bg.get(hb.slots.current)) hb.adjust(player:get_player_name(), hb.slots.current, hb.image.selected, hb.image.bg.get)
end end
minetest.register_on_joinplayer(hb.on_joinplayer) minetest.register_on_joinplayer(hb.on_joinplayer)
minetest.register_chatcommand("hotbar", { minetest.register_chatcommand("hotbar", {
params = "[slots]", params = "[slots|mode]",
description = string.format("If slots is not passed then it displays" .. description = "Invoked with no argument, it displays" ..
" the current slots number, else if it is" .. " the current mode and slots number." ..
" set to 0 then the hotbar gets hidden," .. " To set" ..
" while any other value in the range " .. " the slots number any integer in the range" ..
" [%i,%i] will show and accordingly " .. " [0, 23] is valid. If set to 0, the hotbar" ..
" resize the hotbar.", " gets hidden, any other number will unhide" ..
hb.slots.min + 1, " it." ..
hb.slots.max), " Supported modes are " ..
stringified_table_keys(MODES, ", ") .. ".",
func = hb.slots.command, func = hb.slots.command,
privs = {interact = true}, privs = {interact = true},
}) })
minetest.register_chatcommand("hotbar_mode", { -- minetest.register_chatcommand("hotbar_mode", {
params = "[mode]", -- params = "[mode]",
description = "If mode is not passed then it shows the current mode, " .. -- description = "If mode is not passed then it shows the current mode, " ..
"else it will change the mode to one of the supported " .. -- "else it will change the mode to one of the supported " ..
"ones: " .. stringified_table_keys(MODES, ", ") .. ".", -- "ones: " .. stringified_table_keys(MODES, ", ") .. ".",
func = hb.mode.command, -- func = hb.mode.command,
privs = {interact = true}, -- privs = {interact = true},
}) -- })
core.log("action", "[MOD] hotbar v" .. VERSION .. " operating in " .. hb.mode.current .. " mode. Slots number is set to " .. hb.slots.current .. ".") core.log("action", "[MOD] hotbar v" .. VERSION .. " operating in " .. hb.mode.current .. " mode. Slots number is set to " .. hb.slots.current .. ".")