i3/init.lua

244 lines
5.3 KiB
Lua
Raw Normal View History

2021-11-22 17:37:28 +01:00
local modpath = core.get_modpath"i3"
2021-11-29 02:47:36 +01:00
local _loadfile = dofile(modpath .. "/src/operators.lua")
2021-10-19 05:54:51 +02:00
local function lf(path)
2021-11-29 02:47:36 +01:00
return _loadfile(modpath .. path)
2021-10-19 05:54:51 +02:00
end
i3 = {
modules = {},
http = core.request_http_api(),
MAX_FAVS = 6,
INV_SIZE = 4*9,
HOTBAR_LEN = 9,
ITEM_BTN_SIZE = 1.1,
DROP_BAG_ON_DIE = true,
MIN_FORMSPEC_VERSION = 4,
SAVE_INTERVAL = 600, -- Player data save interval (in seconds)
2021-11-26 03:32:04 +01:00
HUD_TIMER_MAX = 1.5,
HUD_SPEED = 1,
SUBCAT = {
"bag",
"armor",
"skins",
"awards",
"waypoints",
},
META_SAVES = {
bag = true,
home = true,
waypoints = true,
inv_items = true,
drop_items = true,
known_recipes = true,
},
-- Caches
init_items = {},
fuel_cache = {},
usages_cache = {},
recipes_cache = {},
cubes = {},
tabs = {},
craft_types = {},
recipe_filters = {},
search_filters = {},
sorting_methods = {},
2021-10-19 05:54:51 +02:00
files = {
2021-11-28 23:55:11 +01:00
api = lf"/src/api.lua",
bags = lf"/src/bags.lua",
caches = lf"/src/caches.lua",
callbacks = lf"/src/callbacks.lua",
common = lf"/src/common.lua",
compress = lf"/src/compress.lua",
detached = lf"/src/detached_inv.lua",
groups = lf"/src/groups.lua",
gui = lf"/src/gui.lua",
hud = lf"/src/hud.lua",
model_alias = lf"/src/model_aliases.lua",
progressive = lf"/src/progressive.lua",
styles = lf"/src/styles.lua",
tests = {
tabs = lf"/tests/test_tabs.lua",
operators = lf"/tests/test_operators.lua",
compression = lf"/tests/test_compression.lua",
custom_recipes = lf"/tests/test_custom_recipes.lua",
}
},
2021-01-16 03:30:12 +01:00
progressive_mode = core.settings:get_bool"i3_progressive_mode",
item_compression = core.settings:get_bool("i3_item_compression", true),
}
2021-01-16 01:46:26 +01:00
2021-10-25 07:09:21 +02:00
i3.files.common()
2021-10-25 20:31:20 +02:00
i3.files.api()
i3.files.compress()
i3.files.groups()
i3.files.callbacks()
2021-10-25 20:31:20 +02:00
local storage = core.get_mod_storage()
2021-11-22 17:37:28 +01:00
local slz, dslz, copy = i3.get("slz", "dslz", "copy")
2021-11-15 23:50:48 +01:00
local set_fs = i3.set_fs
2021-01-30 23:21:20 +01:00
2021-11-22 17:37:28 +01:00
i3.data = dslz(storage:get_string"data") or {}
2021-01-30 23:21:20 +01:00
local init_bags = i3.files.bags()
2021-11-26 04:58:57 +01:00
local init_detached = i3.files.detached()
local fill_caches = i3.files.caches()
2021-11-26 03:32:04 +01:00
local init_hud = i3.files.hud()
2021-01-28 21:00:08 +01:00
local function get_lang_code(info)
return info and info.lang_code
2021-01-28 21:00:08 +01:00
end
local function get_formspec_version(info)
return info and info.formspec_version or 1
end
2021-01-28 21:00:08 +01:00
local function outdated(name)
local fs = ("size[6.3,1.3]image[0,0;1,1;i3_book.png]label[1,0;%s]button_exit[2.6,0.8;1,1;;OK]"):format(
"Your Minetest client is outdated.\nGet the latest version on minetest.net to play the game.")
2021-01-28 21:00:08 +01:00
core.show_formspec(name, "i3_outdated", fs)
2021-01-28 21:00:08 +01:00
end
2020-12-31 01:34:08 +01:00
if rawget(_G, "armor") then
i3.modules.armor = true
2021-11-15 23:50:48 +01:00
armor:register_on_update(set_fs)
2020-12-31 01:34:08 +01:00
end
2021-01-03 12:12:28 +01:00
if rawget(_G, "skins") then
i3.modules.skins = true
2021-01-03 12:12:28 +01:00
end
2021-01-10 20:56:37 +01:00
if rawget(_G, "awards") then
i3.modules.awards = true
2021-01-10 20:56:37 +01:00
core.register_on_craft(function(_, player)
2021-11-15 23:50:48 +01:00
set_fs(player)
2021-01-10 20:56:37 +01:00
end)
core.register_on_dignode(function(_, _, player)
2021-11-15 23:50:48 +01:00
set_fs(player)
2021-01-10 20:56:37 +01:00
end)
core.register_on_placenode(function(_, _, player)
2021-11-15 23:50:48 +01:00
set_fs(player)
2021-01-10 20:56:37 +01:00
end)
2021-01-29 02:24:17 +01:00
core.register_on_chat_message(function(name)
2021-01-10 21:45:57 +01:00
local player = core.get_player_by_name(name)
2021-11-15 23:50:48 +01:00
set_fs(player)
2021-01-10 20:56:37 +01:00
end)
end
2021-10-25 05:31:58 +02:00
local function disable_inventories()
if rawget(_G, "sfinv") then
2021-05-07 03:16:11 +02:00
function sfinv.set_player_inventory_formspec() return end
2020-12-30 23:21:05 +01:00
sfinv.enabled = false
end
if rawget(_G, "unified_inventory") then
2020-12-30 23:21:05 +01:00
function unified_inventory.set_inventory_formspec() return end
end
2021-10-25 05:31:58 +02:00
end
local function init_data(player, info)
local name = player:get_player_name()
i3.data[name] = i3.data[name] or {}
local data = i3.data[name]
2021-11-15 00:06:07 +01:00
data.player_name = name
data.filter = ""
data.pagenum = 1
data.items = i3.init_items
data.items_raw = i3.init_items
data.favs = {}
data.sort = "alphabetical"
data.show_setting = "home"
data.ignore_hotbar = false
data.auto_sorting = false
data.reverse_sorting = false
data.inv_compress = true
data.export_counts = {}
2021-11-01 15:34:10 +01:00
data.tab = 1
data.itab = 1
data.subcat = 1
data.scrbar_inv = 0
data.lang_code = get_lang_code(info)
data.fs_version = info.formspec_version
2021-10-25 05:31:58 +02:00
local inv = player:get_inventory()
inv:set_size("main", i3.INV_SIZE)
2021-11-15 23:50:48 +01:00
core.after(0, set_fs, player)
2021-10-25 05:31:58 +02:00
end
2020-12-30 23:21:05 +01:00
2021-05-31 22:46:02 +02:00
local function save_data(player_name)
2021-11-21 18:44:49 +01:00
local _data = copy(i3.data)
2021-05-31 22:46:02 +02:00
for name, v in pairs(_data) do
2021-05-31 20:48:12 +02:00
for dat in pairs(v) do
if not i3.META_SAVES[dat] then
_data[name][dat] = nil
2021-05-31 22:46:02 +02:00
if player_name and i3.data[player_name] then
i3.data[player_name][dat] = nil -- To free up some memory
2021-05-31 22:46:02 +02:00
end
2021-05-31 20:48:12 +02:00
end
2020-12-31 00:17:38 +01:00
end
2021-05-31 20:48:12 +02:00
end
storage:set_string("data", slz(_data))
2021-05-31 22:46:02 +02:00
end
2021-10-25 05:31:58 +02:00
core.register_on_mods_loaded(function()
fill_caches()
2021-10-25 05:31:58 +02:00
disable_inventories()
end)
core.register_on_joinplayer(function(player)
local name = player:get_player_name()
local info = core.get_player_information and core.get_player_information(name)
if not info or get_formspec_version(info) < i3.MIN_FORMSPEC_VERSION then
return outdated(name)
end
init_data(player, info)
init_bags(player)
2021-11-26 04:58:57 +01:00
init_detached(player)
2021-11-26 03:32:04 +01:00
init_hud(player)
2021-10-25 05:31:58 +02:00
end)
2021-06-25 03:44:38 +02:00
core.register_on_leaveplayer(function(player)
2021-05-31 22:46:02 +02:00
local name = player:get_player_name()
save_data(name)
2020-12-30 23:21:05 +01:00
end)
2021-06-25 03:44:38 +02:00
core.register_on_shutdown(save_data)
2021-05-31 22:46:02 +02:00
local function routine()
save_data()
core.after(i3.SAVE_INTERVAL, routine)
2021-05-31 22:46:02 +02:00
end
core.after(i3.SAVE_INTERVAL, routine)
2020-12-30 23:21:05 +01:00
if i3.progressive_mode then
2021-10-19 05:54:51 +02:00
i3.files.progressive()
2021-01-22 00:42:48 +01:00
end
2020-12-30 23:21:05 +01:00
2021-11-28 23:55:11 +01:00
--i3.files.tests.tabs()
--i3.files.tests.operators()
--i3.files.tests.compression()
--i3.files.tests.custom_recipes()