diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..4ad96d5 --- /dev/null +++ b/depends.txt @@ -0,0 +1 @@ +default diff --git a/init.lua b/init.lua index 83754a1..d7a8379 100644 --- a/init.lua +++ b/init.lua @@ -3,9 +3,189 @@ minetest.register_privilege("builder", { give_to_singleplayer = false }) +local VERSION = "0.1.5a" +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 = {} +if not core.get_mod_storage then + -- MT < 0.4.16 + MOD_STORAGE.present = false + MOD_STORAGE.settings = false +else + -- MT 0.4.16+ + MOD_STORAGE.present = true + MOD_STORAGE.settings = core.get_mod_storage() +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) + if not core.is_singleplayer() then + return MODES.session + end + local value = core.settings:get(key) + 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) + if not core.is_singleplayer() then + 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 + 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) .. ".") + end + return value +end + +local get_and_set_initial_slots = function(storage, mode_value, key, default_value) + local current + if not core.is_singleplayer() then + mode_value = MODES.session + default_value = DEFAULT.slots[mode_value] + end + + if mode_value == MODES.legacy then + local result = tonumber(core.settings:get(key)) + current = result or default_value -- The first time + if not result then + -- first time + 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 + 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 + 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 + 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) .. ".") + end + + return current +end + +local adjust_hotbar = function(name, slots, selected_image, bg_image_getter) + 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) + local mask = {err = "[%s] Wrong slots number specified: the %s accepted value is %i.", + set = "[%s] Hotbar slots number set to %i."} + local display_name = name + if minetest.is_singleplayer() then + display_name = '_' + 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)) + 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 + -- 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 has not 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 + return + end + if hb.mode.current ~= MODES.session then + hb.slots.current = slots + end +end + 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("") + --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/textures/hotbar_selected_slot.png b/textures/hotbar_selected_slot.png new file mode 100644 index 0000000..abb75cc Binary files /dev/null and b/textures/hotbar_selected_slot.png differ diff --git a/textures/hotbar_slots_bg_01.png b/textures/hotbar_slots_bg_01.png new file mode 100644 index 0000000..6e777d1 Binary files /dev/null and b/textures/hotbar_slots_bg_01.png differ diff --git a/textures/hotbar_slots_bg_02.png b/textures/hotbar_slots_bg_02.png new file mode 100644 index 0000000..4fe5210 Binary files /dev/null and b/textures/hotbar_slots_bg_02.png differ diff --git a/textures/hotbar_slots_bg_03.png b/textures/hotbar_slots_bg_03.png new file mode 100644 index 0000000..c702730 Binary files /dev/null and b/textures/hotbar_slots_bg_03.png differ diff --git a/textures/hotbar_slots_bg_04.png b/textures/hotbar_slots_bg_04.png new file mode 100644 index 0000000..d0b8abf Binary files /dev/null and b/textures/hotbar_slots_bg_04.png differ diff --git a/textures/hotbar_slots_bg_05.png b/textures/hotbar_slots_bg_05.png new file mode 100644 index 0000000..82965d2 Binary files /dev/null and b/textures/hotbar_slots_bg_05.png differ diff --git a/textures/hotbar_slots_bg_06.png b/textures/hotbar_slots_bg_06.png new file mode 100644 index 0000000..ebdf59b Binary files /dev/null and b/textures/hotbar_slots_bg_06.png differ diff --git a/textures/hotbar_slots_bg_07.png b/textures/hotbar_slots_bg_07.png new file mode 100644 index 0000000..519fada Binary files /dev/null and b/textures/hotbar_slots_bg_07.png differ diff --git a/textures/hotbar_slots_bg_08.png b/textures/hotbar_slots_bg_08.png new file mode 100644 index 0000000..4f77e12 Binary files /dev/null and b/textures/hotbar_slots_bg_08.png differ diff --git a/textures/hotbar_slots_bg_09.png b/textures/hotbar_slots_bg_09.png new file mode 100644 index 0000000..82fb625 Binary files /dev/null and b/textures/hotbar_slots_bg_09.png differ diff --git a/textures/hotbar_slots_bg_10.png b/textures/hotbar_slots_bg_10.png new file mode 100644 index 0000000..0d27402 Binary files /dev/null and b/textures/hotbar_slots_bg_10.png differ diff --git a/textures/hotbar_slots_bg_11.png b/textures/hotbar_slots_bg_11.png new file mode 100644 index 0000000..842dcb0 Binary files /dev/null and b/textures/hotbar_slots_bg_11.png differ diff --git a/textures/hotbar_slots_bg_12.png b/textures/hotbar_slots_bg_12.png new file mode 100644 index 0000000..13278e4 Binary files /dev/null and b/textures/hotbar_slots_bg_12.png differ diff --git a/textures/hotbar_slots_bg_13.png b/textures/hotbar_slots_bg_13.png new file mode 100644 index 0000000..6e4257c Binary files /dev/null and b/textures/hotbar_slots_bg_13.png differ diff --git a/textures/hotbar_slots_bg_14.png b/textures/hotbar_slots_bg_14.png new file mode 100644 index 0000000..7999ea7 Binary files /dev/null and b/textures/hotbar_slots_bg_14.png differ diff --git a/textures/hotbar_slots_bg_15.png b/textures/hotbar_slots_bg_15.png new file mode 100644 index 0000000..fcecd9d Binary files /dev/null and b/textures/hotbar_slots_bg_15.png differ diff --git a/textures/hotbar_slots_bg_16.png b/textures/hotbar_slots_bg_16.png new file mode 100644 index 0000000..44011b5 Binary files /dev/null and b/textures/hotbar_slots_bg_16.png differ diff --git a/textures/hotbar_slots_bg_17.png b/textures/hotbar_slots_bg_17.png new file mode 100644 index 0000000..ebd6ba6 Binary files /dev/null and b/textures/hotbar_slots_bg_17.png differ diff --git a/textures/hotbar_slots_bg_18.png b/textures/hotbar_slots_bg_18.png new file mode 100644 index 0000000..5aba72a Binary files /dev/null and b/textures/hotbar_slots_bg_18.png differ diff --git a/textures/hotbar_slots_bg_19.png b/textures/hotbar_slots_bg_19.png new file mode 100644 index 0000000..eb2c1d8 Binary files /dev/null and b/textures/hotbar_slots_bg_19.png differ diff --git a/textures/hotbar_slots_bg_20.png b/textures/hotbar_slots_bg_20.png new file mode 100644 index 0000000..56adc51 Binary files /dev/null and b/textures/hotbar_slots_bg_20.png differ diff --git a/textures/hotbar_slots_bg_21.png b/textures/hotbar_slots_bg_21.png new file mode 100644 index 0000000..baa2c2d Binary files /dev/null and b/textures/hotbar_slots_bg_21.png differ diff --git a/textures/hotbar_slots_bg_22.png b/textures/hotbar_slots_bg_22.png new file mode 100644 index 0000000..3e1623e Binary files /dev/null and b/textures/hotbar_slots_bg_22.png differ diff --git a/textures/hotbar_slots_bg_23.png b/textures/hotbar_slots_bg_23.png new file mode 100644 index 0000000..7cb94cd Binary files /dev/null and b/textures/hotbar_slots_bg_23.png differ