minetest <=> core substitution
This commit is contained in:
parent
74f4e29554
commit
be959496f5
76
init.lua
76
init.lua
@ -72,14 +72,14 @@ local VERSION = "0.1.4"
|
||||
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 minetest.get_mod_storage then
|
||||
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 = minetest.get_mod_storage()
|
||||
MOD_STORAGE.settings = core.get_mod_storage()
|
||||
end
|
||||
|
||||
|
||||
@ -102,10 +102,10 @@ local new_masked_array = function(mask, max)
|
||||
end
|
||||
|
||||
local read_mode = function(key, default_value)
|
||||
if not minetest.is_singleplayer() then
|
||||
if not core.is_singleplayer() then
|
||||
return MODES.session
|
||||
end
|
||||
local value = minetest.settings:get(key)
|
||||
local value = core.settings:get(key)
|
||||
if type(value) ~= "string" or #value == 0 then
|
||||
return default_value
|
||||
end
|
||||
@ -117,7 +117,7 @@ local read_mode = function(key, default_value)
|
||||
end
|
||||
|
||||
local get_mode = function(storage, key, default_value)
|
||||
if not minetest.is_singleplayer() then
|
||||
if not core.is_singleplayer() then
|
||||
return MODES.session
|
||||
end
|
||||
local value = read_mode(key, default_value)
|
||||
@ -129,8 +129,8 @@ local get_mode = function(storage, key, default_value)
|
||||
end
|
||||
end
|
||||
if wrong then
|
||||
minetest.settings:set(key, value)
|
||||
minetest.log("error",
|
||||
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 " ..
|
||||
@ -141,26 +141,26 @@ end
|
||||
|
||||
local get_and_set_initial_slots = function(storage, mode_value, key, default_value)
|
||||
local current
|
||||
if not minetest.is_singleplayer() then
|
||||
if not core.is_singleplayer() then
|
||||
mode_value = MODES.session
|
||||
end
|
||||
|
||||
if mode_value == MODES.legacy then
|
||||
local result = tonumber(minetest.settings:get(key))
|
||||
local result = tonumber(core.settings:get(key))
|
||||
current = result or default_value -- The first time
|
||||
if not result then
|
||||
minetest.settings:set(key, current)
|
||||
core.settings:set(key, current)
|
||||
else
|
||||
result = math.floor(result)
|
||||
end
|
||||
|
||||
elseif mode_value == MODES.world then
|
||||
local result = minetest.deserialize(storage.settings:get_string(key))
|
||||
local result = core.deserialize(storage.settings:get_string(key))
|
||||
if type(result) == "number" then
|
||||
current = result
|
||||
else
|
||||
current = default_value -- The first time
|
||||
storage.settings:set_string(key, minetest.serialize(current))
|
||||
storage.settings:set_string(key, core.serialize(current))
|
||||
end
|
||||
|
||||
elseif mode_value == MODES.session then
|
||||
@ -168,7 +168,7 @@ local get_and_set_initial_slots = function(storage, mode_value, key, default_val
|
||||
|
||||
else
|
||||
current = default_value -- Unplanned case
|
||||
minetest.log("error",
|
||||
core.log("error",
|
||||
"[MOD] hotbar v" .. VERSION ..
|
||||
": the specified mode - " .. string.upper(mode_value) ..
|
||||
" - is unmanaged and has been overridden and set to " ..
|
||||
@ -179,7 +179,7 @@ local get_and_set_initial_slots = function(storage, mode_value, key, default_val
|
||||
end
|
||||
|
||||
local adjust_hotbar = function(name, slots, selected_image, bg_image)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
local player = core.get_player_by_name(name)
|
||||
if slots == 0 then
|
||||
player:hud_set_flags({hotbar = false, wielditem = false})
|
||||
else
|
||||
@ -210,28 +210,28 @@ hb.slots.set = function(name, slots)
|
||||
local mask = {err = "[_] Wrong slots number specified: the %s accepted value is %i.",
|
||||
set = "[_] Hotbar slots number set to %i."}
|
||||
if slots < hb.slots.min then
|
||||
minetest.chat_send_player(name, mask.err:format("minimum", hb.slots.min))
|
||||
core.chat_send_player(name, mask.err:format("minimum", hb.slots.min))
|
||||
return
|
||||
end
|
||||
if slots > hb.slots.max then
|
||||
minetest.chat_send_player(name, mask.err:format("maximum", hb.slots.max))
|
||||
core.chat_send_player(name, mask.err:format("maximum", hb.slots.max))
|
||||
return
|
||||
end
|
||||
slots = math.floor(slots) -- to avoid fractions
|
||||
hb.adjust(name, slots, hb.image.selected, hb.image.bg.get(slots))
|
||||
|
||||
if hb.mode.current == MODES.legacy then
|
||||
minetest.settings:set(hb.slots.key, slots)
|
||||
core.settings:set(hb.slots.key, slots)
|
||||
elseif hb.mode.current == MODES.world then
|
||||
MOD_STORAGE.settings:set_string(hb.slots.key, minetest.serialize(slots))
|
||||
minetest.log("warning",
|
||||
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 minetest.is_singleplayer() then
|
||||
-- This is an ephemral / transient storage that is to survive while in a map
|
||||
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.
|
||||
@ -241,23 +241,23 @@ hb.slots.set = function(name, slots)
|
||||
DEFAULT.slots[hb.mode.current] = slots
|
||||
end
|
||||
else
|
||||
minetest.log("error",
|
||||
core.log("error",
|
||||
"[MOD] hotbar v" .. VERSION ..
|
||||
": it is still not possible to set the slots number in " ..
|
||||
string.upper(hb.mode.current) .. " mode.")
|
||||
minetest.chat_send_player(name, string.upper(hb.mode.current) .. " mode is not managed yet!")
|
||||
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
|
||||
minetest.chat_send_player(name, mask.set:format(slots))
|
||||
core.chat_send_player(name, mask.set:format(slots))
|
||||
end
|
||||
|
||||
hb.slots.command = function(name, slots)
|
||||
local new_slots = tonumber(slots)
|
||||
if not new_slots then
|
||||
minetest.chat_send_player(name, "[_] Hotbar slots: " .. hb.slots.current)
|
||||
core.chat_send_player(name, "[_] Hotbar slots: " .. hb.slots.current)
|
||||
return
|
||||
end
|
||||
hb.slots.set(name, new_slots)
|
||||
@ -266,12 +266,12 @@ end
|
||||
hb.mode.command = function(name, mode)
|
||||
local message
|
||||
|
||||
if not minetest.is_singleplayer() or #mode == 0 then
|
||||
if not core.is_singleplayer() or #mode == 0 then
|
||||
-- display current settings
|
||||
local player = minetest.get_player_by_name(name)
|
||||
minetest.chat_send_player(name, "[_] Hotbar mode: " .. string.upper(hb.mode.current))
|
||||
minetest.chat_send_player(name, "[_] Hotbar slots: " .. player:hud_get_hotbar_itemcount())
|
||||
minetest.chat_send_player(name, "[_] Hotbar version: " .. VERSION)
|
||||
local player = core.get_player_by_name(name)
|
||||
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
|
||||
end
|
||||
|
||||
@ -284,10 +284,8 @@ hb.mode.command = function(name, mode)
|
||||
message = "Your request to change the hotbar mode to " ..
|
||||
string.upper(mode) .. " has been declined because it is unmanaged."
|
||||
|
||||
minetest.log("error",
|
||||
"[MOD] hotbar v" .. VERSION ..
|
||||
": " .. message)
|
||||
minetest.chat_send_player(name, "[_] " .. message)
|
||||
core.log("error", "[MOD] hotbar v" .. VERSION .. ": " .. message)
|
||||
core.chat_send_player(name, "[_] " .. message)
|
||||
return
|
||||
end
|
||||
else
|
||||
@ -295,17 +293,17 @@ hb.mode.command = function(name, mode)
|
||||
-- no log is required.
|
||||
message = "[_] Wrong hotbar mode - " ..
|
||||
string.upper(mode) .. " - specified."
|
||||
minetest.chat_send_player(name, message)
|
||||
core.chat_send_player(name, message)
|
||||
return
|
||||
end
|
||||
if mode == MODES.legacy or mode == MODES.world or mode == MODES.session then
|
||||
minetest.settings:set(hb.mode.key, mode)
|
||||
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)
|
||||
minetest.log("warning", "[MOD] hotbar v" .. VERSION .. ": [" .. name .. "] " .. message)
|
||||
minetest.chat_send_player(name, "[_] " .. message)
|
||||
core.log("warning", "[MOD] hotbar v" .. VERSION .. ": [" .. name .. "] " .. message)
|
||||
core.chat_send_player(name, "[_] " .. message)
|
||||
end
|
||||
|
||||
hb.on_joinplayer = function(player)
|
||||
@ -337,5 +335,5 @@ minetest.register_chatcommand("hotbar_mode", {
|
||||
privs = {interact = true},
|
||||
})
|
||||
|
||||
minetest.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 .. ".")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user