2021-10-18 22:54:51 -05:00
|
|
|
local modpath = core.get_modpath "i3"
|
|
|
|
|
|
|
|
local function lf(path)
|
|
|
|
return loadfile(modpath .. path)
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
i3 = {
|
|
|
|
modules = {},
|
2021-10-24 16:31:01 -05:00
|
|
|
http = core.request_http_api(),
|
2021-10-18 20:24:32 -05:00
|
|
|
|
|
|
|
MAX_FAVS = 6,
|
|
|
|
INV_SIZE = 4*9,
|
|
|
|
HOTBAR_LEN = 9,
|
|
|
|
ITEM_BTN_SIZE = 1.1,
|
|
|
|
MIN_FORMSPEC_VERSION = 4,
|
|
|
|
SAVE_INTERVAL = 600, -- Player data save interval (in seconds)
|
|
|
|
|
|
|
|
BAG_SIZES = {
|
|
|
|
4*9 + 3,
|
|
|
|
4*9 + 6,
|
|
|
|
4*9 + 9,
|
|
|
|
},
|
|
|
|
|
|
|
|
SUBCAT = {
|
|
|
|
"bag",
|
|
|
|
"armor",
|
|
|
|
"skins",
|
|
|
|
"awards",
|
|
|
|
"waypoints",
|
|
|
|
},
|
|
|
|
|
|
|
|
META_SAVES = {
|
2021-10-31 16:11:41 -05:00
|
|
|
home = true,
|
2021-10-18 20:24:32 -05:00
|
|
|
bag_item = true,
|
|
|
|
bag_size = true,
|
|
|
|
waypoints = true,
|
|
|
|
inv_items = true,
|
2021-11-08 12:53:00 -06:00
|
|
|
reject_items = true,
|
2021-10-18 20:24:32 -05:00
|
|
|
known_recipes = true,
|
|
|
|
},
|
|
|
|
|
|
|
|
-- Caches
|
2021-10-31 16:11:41 -05:00
|
|
|
init_items = {},
|
|
|
|
fuel_cache = {},
|
|
|
|
usages_cache = {},
|
|
|
|
recipes_cache = {},
|
|
|
|
|
|
|
|
tabs = {},
|
|
|
|
craft_types = {},
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
recipe_filters = {},
|
|
|
|
search_filters = {},
|
2021-10-31 16:11:41 -05:00
|
|
|
sorting_methods = {},
|
2021-10-18 22:54:51 -05:00
|
|
|
|
|
|
|
files = {
|
2021-11-01 20:11:08 -05:00
|
|
|
api = lf("/src/api.lua"),
|
|
|
|
bags = lf("/src/bags.lua"),
|
|
|
|
common = lf("/src/common.lua"),
|
|
|
|
compress = lf("/src/compress.lua"),
|
|
|
|
groups = lf("/src/groups.lua"),
|
|
|
|
gui = lf("/src/gui.lua"),
|
|
|
|
inventory = lf("/src/inventory.lua"),
|
|
|
|
model_alias = lf("/src/model_aliases.lua"),
|
|
|
|
progressive = lf("/src/progressive.lua"),
|
|
|
|
recipes = lf("/src/recipes.lua"),
|
|
|
|
styles = lf("/src/styles.lua"),
|
2021-10-24 16:31:01 -05:00
|
|
|
},
|
2021-01-15 20:30:12 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
progressive_mode = core.settings:get_bool "i3_progressive_mode",
|
|
|
|
item_compression = core.settings:get_bool("i3_item_compression", true),
|
|
|
|
}
|
2021-01-15 18:46:26 -06:00
|
|
|
|
2021-10-25 00:09:21 -05:00
|
|
|
i3.files.common()
|
2021-10-25 13:31:20 -05:00
|
|
|
i3.files.api()
|
|
|
|
i3.files.compress()
|
|
|
|
i3.files.groups()
|
|
|
|
i3.files.inventory()
|
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local storage = core.get_mod_storage()
|
2021-11-08 13:41:47 -06:00
|
|
|
local slz, dslz, str_to_pos, add_hud_waypoint = i3.get("slz", "dslz", "str_to_pos", "add_hud_waypoint")
|
2021-01-30 16:21:20 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
i3.data = dslz(storage:get_string "data") or {}
|
2021-01-30 16:21:20 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local init_backpack = i3.files.bags()
|
2021-10-25 00:09:21 -05:00
|
|
|
local init_recipes = i3.files.recipes()
|
2021-01-28 14:00:08 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local function get_lang_code(info)
|
|
|
|
return info and info.lang_code
|
2021-01-28 14:00:08 -06:00
|
|
|
end
|
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local function get_formspec_version(info)
|
|
|
|
return info and info.formspec_version or 1
|
|
|
|
end
|
2021-01-28 14:00:08 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local function outdated(name)
|
2021-11-08 12:53:00 -06:00
|
|
|
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(
|
2021-10-24 16:31:01 -05:00
|
|
|
"Your Minetest client is outdated.\nGet the latest version on minetest.net to play the game.")
|
2021-01-28 14:00:08 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
core.show_formspec(name, "i3_outdated", fs)
|
2021-01-28 14:00:08 -06:00
|
|
|
end
|
|
|
|
|
2020-12-30 18:34:08 -06:00
|
|
|
if rawget(_G, "armor") then
|
2021-10-18 20:24:32 -05:00
|
|
|
i3.modules.armor = true
|
2021-10-25 13:31:20 -05:00
|
|
|
armor:register_on_update(i3.set_fs)
|
2020-12-30 18:34:08 -06:00
|
|
|
end
|
|
|
|
|
2021-01-03 05:12:28 -06:00
|
|
|
if rawget(_G, "skins") then
|
2021-10-18 20:24:32 -05:00
|
|
|
i3.modules.skins = true
|
2021-01-03 05:12:28 -06:00
|
|
|
end
|
|
|
|
|
2021-01-10 13:56:37 -06:00
|
|
|
if rawget(_G, "awards") then
|
2021-10-18 20:24:32 -05:00
|
|
|
i3.modules.awards = true
|
2021-01-10 13:56:37 -06:00
|
|
|
|
|
|
|
core.register_on_craft(function(_, player)
|
2021-10-25 13:31:20 -05:00
|
|
|
i3.set_fs(player)
|
2021-01-10 13:56:37 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
core.register_on_dignode(function(_, _, player)
|
2021-10-25 13:31:20 -05:00
|
|
|
i3.set_fs(player)
|
2021-01-10 13:56:37 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
core.register_on_placenode(function(_, _, player)
|
2021-10-25 13:31:20 -05:00
|
|
|
i3.set_fs(player)
|
2021-01-10 13:56:37 -06:00
|
|
|
end)
|
|
|
|
|
2021-01-28 19:24:17 -06:00
|
|
|
core.register_on_chat_message(function(name)
|
2021-01-10 14:45:57 -06:00
|
|
|
local player = core.get_player_by_name(name)
|
2021-10-25 13:31:20 -05:00
|
|
|
i3.set_fs(player)
|
2021-01-10 13:56:37 -06:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-10-24 22:31:58 -05:00
|
|
|
local function disable_inventories()
|
2021-07-05 15:40:31 -05:00
|
|
|
if rawget(_G, "sfinv") then
|
2021-05-06 20:16:11 -05:00
|
|
|
function sfinv.set_player_inventory_formspec() return end
|
2020-12-30 16:21:05 -06:00
|
|
|
sfinv.enabled = false
|
|
|
|
end
|
|
|
|
|
2021-07-05 15:40:31 -05:00
|
|
|
if rawget(_G, "unified_inventory") then
|
2020-12-30 16:21:05 -06:00
|
|
|
function unified_inventory.set_inventory_formspec() return end
|
|
|
|
end
|
2021-10-24 22:31:58 -05: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-10-31 16:11:41 -05:00
|
|
|
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"
|
2021-11-01 09:12:32 -05:00
|
|
|
data.ignore_hotbar = false
|
2021-10-31 16:11:41 -05:00
|
|
|
data.auto_sorting = false
|
|
|
|
data.reverse_sorting = false
|
|
|
|
data.inv_compress = true
|
|
|
|
data.export_counts = {}
|
2021-11-01 09:34:10 -05:00
|
|
|
data.tab = 1
|
|
|
|
data.itab = 1
|
2021-10-31 16:11:41 -05:00
|
|
|
data.subcat = 1
|
|
|
|
data.scrbar_inv = 0
|
|
|
|
data.lang_code = get_lang_code(info)
|
|
|
|
data.fs_version = info.formspec_version
|
2021-10-24 22:31:58 -05:00
|
|
|
|
2021-10-25 13:31:20 -05:00
|
|
|
core.after(0, i3.set_fs, player)
|
2021-10-24 22:31:58 -05:00
|
|
|
end
|
2020-12-30 16:21:05 -06:00
|
|
|
|
2021-04-05 19:47:37 -05:00
|
|
|
local function init_waypoints(player)
|
|
|
|
local name = player:get_player_name()
|
2021-10-18 20:24:32 -05:00
|
|
|
local data = i3.data[name]
|
2021-04-11 16:23:03 -05:00
|
|
|
data.waypoints = data.waypoints or {}
|
2021-04-05 19:47:37 -05:00
|
|
|
|
2021-04-11 16:23:03 -05:00
|
|
|
for _, v in ipairs(data.waypoints) do
|
2021-04-11 17:38:24 -05:00
|
|
|
if not v.hide then
|
2021-11-08 13:41:47 -06:00
|
|
|
local id = add_hud_waypoint(player, v.name, str_to_pos(v.pos), v.color)
|
2021-04-11 17:38:24 -05:00
|
|
|
v.id = id
|
2021-04-11 16:23:03 -05:00
|
|
|
end
|
2021-04-05 19:47:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-24 22:31:58 -05:00
|
|
|
local function init_hudbar(player)
|
2021-10-24 16:31:01 -05:00
|
|
|
core.after(0, function()
|
2021-10-18 20:24:32 -05:00
|
|
|
player:hud_set_hotbar_itemcount(i3.HOTBAR_LEN)
|
|
|
|
player:hud_set_hotbar_image("i3_hotbar.png")
|
2021-01-25 12:28:28 -06:00
|
|
|
end)
|
2021-10-24 22:31:58 -05:00
|
|
|
end
|
2021-01-21 17:42:48 -06:00
|
|
|
|
2021-05-31 15:46:02 -05:00
|
|
|
local function save_data(player_name)
|
2021-11-08 12:53:00 -06:00
|
|
|
local _data = table.copy(i3.data)
|
2021-05-31 15:46:02 -05:00
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
for name, v in pairs(_data) do
|
2021-05-31 13:48:12 -05:00
|
|
|
for dat in pairs(v) do
|
2021-10-18 20:24:32 -05:00
|
|
|
if not i3.META_SAVES[dat] then
|
|
|
|
_data[name][dat] = nil
|
2021-05-31 15:46:02 -05:00
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
if player_name and i3.data[player_name] then
|
|
|
|
i3.data[player_name][dat] = nil -- To free up some memory
|
2021-05-31 15:46:02 -05:00
|
|
|
end
|
2021-05-31 13:48:12 -05:00
|
|
|
end
|
2020-12-30 17:17:38 -06:00
|
|
|
end
|
2021-05-31 13:48:12 -05:00
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
storage:set_string("data", slz(_data))
|
2021-05-31 15:46:02 -05:00
|
|
|
end
|
|
|
|
|
2021-10-24 22:31:58 -05:00
|
|
|
core.register_on_mods_loaded(function()
|
2021-10-25 00:09:21 -05:00
|
|
|
init_recipes()
|
2021-10-24 22:31:58 -05: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
|
|
|
|
i3.data[name] = nil
|
|
|
|
return outdated(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
init_data(player, info)
|
|
|
|
init_backpack(player)
|
|
|
|
init_waypoints(player)
|
|
|
|
init_hudbar(player)
|
|
|
|
end)
|
|
|
|
|
2021-06-24 20:44:38 -05:00
|
|
|
core.register_on_leaveplayer(function(player)
|
2021-05-31 15:46:02 -05:00
|
|
|
local name = player:get_player_name()
|
|
|
|
save_data(name)
|
2020-12-30 16:21:05 -06:00
|
|
|
end)
|
|
|
|
|
2021-06-24 20:44:38 -05:00
|
|
|
core.register_on_shutdown(save_data)
|
2021-05-31 15:46:02 -05:00
|
|
|
|
|
|
|
local function routine()
|
|
|
|
save_data()
|
2021-10-24 16:31:01 -05:00
|
|
|
core.after(i3.SAVE_INTERVAL, routine)
|
2021-05-31 15:46:02 -05:00
|
|
|
end
|
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
core.after(i3.SAVE_INTERVAL, routine)
|
2020-12-30 16:21:05 -06:00
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
if i3.progressive_mode then
|
2021-10-18 22:54:51 -05:00
|
|
|
i3.files.progressive()
|
2021-01-21 17:42:48 -06:00
|
|
|
end
|
2020-12-30 16:21:05 -06:00
|
|
|
|
2021-06-25 20:10:05 -05:00
|
|
|
--dofile(modpath .. "/tests/test_tabs.lua")
|
2021-10-24 17:14:21 -05:00
|
|
|
--dofile(modpath .. "/tests/test_compression.lua")
|
2021-06-25 20:10:05 -05:00
|
|
|
--dofile(modpath .. "/tests/test_custom_recipes.lua")
|