From 554acd002bbd47ad64bee1316789c31a6f2e3f19 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sun, 2 May 2021 19:22:45 +0200 Subject: [PATCH 01/13] Small cleaning --- init.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/init.lua b/init.lua index f5c76f0..b8a8728 100644 --- a/init.lua +++ b/init.lua @@ -1249,8 +1249,6 @@ local function select_item(player, name, data, _f) if not item then return - elseif sub(item, -4) == "_inv" then - item = sub(item, 1, -5) elseif sub(item, 1, 1) == "_" then item = sub(item, 2) elseif sub(item, 1, 6) == "group|" then @@ -1839,7 +1837,7 @@ local function get_items_fs(fs, data, extend) local Y = round((i % ipp - X) / rows + 1, 0) Y = Y - (Y * (extend and 0.085 or 0.035)) + 0.95 - fs[#fs + 1] = fmt("item_image_button", X, Y, size, size, item, fmt("%s_inv", item), "") + fs[#fs + 1] = fmt("item_image_button", X, Y, size, size, item, item, "") end end From 8d55d89efe4120e60eaa57b7188bab22786a5e8d Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Fri, 7 May 2021 00:33:34 +0200 Subject: [PATCH 02/13] Chech another param --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index b8a8728..824742d 100644 --- a/init.lua +++ b/init.lua @@ -2770,7 +2770,7 @@ core.register_on_chatcommand(function(name, command, params) params = split(params, " ") for _, v in ipairs(params) do - if find(v, "creative") then + if find(v, "creative") or find(v, "all") then local data = pdata[name] reset_data(data) data.favs = {} From 9ab92ba0566a339a34bdecadb0f2ff7dff30d3e9 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Fri, 7 May 2021 03:16:11 +0200 Subject: [PATCH 03/13] *Really* disable sfinv --- .luacheckrc | 3 ++- init.lua | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index e767495..5840689 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -6,7 +6,6 @@ ignore = { read_globals = { "minetest", - "default", "armor", "skins", "awards", @@ -19,4 +18,6 @@ read_globals = { globals = { "i3", "core", + "sfinv", + "unified_inventory", } diff --git a/init.lua b/init.lua index 824742d..2a59dae 100644 --- a/init.lua +++ b/init.lua @@ -16,7 +16,8 @@ local progressive_mode = core.settings:get_bool "i3_progressive_mode" local damage_enabled = core.settings:get_bool "enable_damage" local __3darmor, __skinsdb, __awards -local sfinv, unified_inventory, old_unified_inventory_fn +local __sfinv, old_sfinv_fn +local __unified_inventory, old_unified_inventory_fn local http = core.request_http_api() local singleplayer = core.is_singleplayer() @@ -2948,15 +2949,17 @@ end on_mods_loaded(function() get_init_items() - sfinv = rawget(_G, "sfinv") + __sfinv = rawget(_G, "sfinv") - if sfinv then + if __sfinv then + old_sfinv_fn = sfinv.set_player_inventory_formspec + function sfinv.set_player_inventory_formspec() return end sfinv.enabled = false end - unified_inventory = rawget(_G, "unified_inventory") + __unified_inventory = rawget(_G, "unified_inventory") - if unified_inventory then + if __unified_inventory then old_unified_inventory_fn = unified_inventory.set_inventory_formspec function unified_inventory.set_inventory_formspec() return end end @@ -3038,14 +3041,15 @@ on_joinplayer(function(player) local info = get_player_info(name) if get_formspec_version(info) < MIN_FORMSPEC_VERSION then - if sfinv then + if __sfinv then + sfinv.set_player_inventory_formspec = old_sfinv_fn sfinv.enabled = true end - if unified_inventory then + if __unified_inventory then unified_inventory.set_inventory_formspec = old_unified_inventory_fn - if sfinv then + if __sfinv then sfinv.enabled = false end end From e0166288c66ef6e8ea0add57dbc59a88194be6a8 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sun, 9 May 2021 16:54:09 +0200 Subject: [PATCH 04/13] Hide hearts when damage ain't enabled --- init.lua | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/init.lua b/init.lua index 2a59dae..fb6a486 100644 --- a/init.lua +++ b/init.lua @@ -2046,23 +2046,25 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a local name = player:get_player_name() add_subtitle(fs, "player_name", 0, ctn_len, 22, true, ESC(name)) - local hp = damage_enabled and (data.hp or player:get_hp()) or 20 - local half = ceil((hp / 2) % 1) - local hearts = (hp / 2) + half - local heart_size = 0.35 - local heart_x, heart_h = 0.65, yoffset + 0.75 - - for i = 1, 10 do - fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h, - heart_size, heart_size, PNG.heart_grey) - end - if damage_enabled then + local hp = data.hp or player:get_hp() or 20 + local half = ceil((hp / 2) % 1) + local hearts = (hp / 2) + half + local heart_size = 0.35 + local heart_x, heart_h = 0.65, yoffset + 0.75 + + for i = 1, 10 do + fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h, + heart_size, heart_size, PNG.heart_grey) + end + for i = 1, hearts do fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h, heart_size, heart_size, (half == 1 and i == floor(hearts)) and PNG.heart_half or PNG.heart) end + else + yoffset = yoffset - 0.5 end fs(fmt("list[current_player;craft;%f,%f;3,3;]", 0, yoffset + 1.45)) @@ -2071,7 +2073,7 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a fmt("list[detached:i3_trash;main;%f,%f;1,1;]", 4.45, yoffset + 3.75)) fs("image", 4.45, yoffset + 3.75, 1, 1, PNG.trash) - local yextra = 5.5 + local yextra = damage_enabled and 5.5 or 5 for i, title in ipairs(SUBCAT) do local btn_name = fmt("btn_%s", title) @@ -2529,7 +2531,7 @@ local function get_inventory_fs(player, data, fs) local award_list, award_list_nb local awards_unlocked = 0 - local max_val = 12 + local max_val = damage_enabled and 12 or 7 if __3darmor and data.subcat == 2 then if data.scrbar_inv >= max_val then @@ -2566,7 +2568,7 @@ local function get_inventory_fs(player, data, fs) scrollbar[%f,0.2;0.2,%f;vertical;scrbar_inv;%u] scrollbaroptions[arrows=default;thumbsize=0;max=1000] ]], - (max_val * 4) / 15, max_val, 9.8, ctn_hgt, data.scrbar_inv)) + (max_val * 4) / 12, max_val, 9.8, ctn_hgt, data.scrbar_inv)) fs(fmt("scroll_container[3.9,0.2;%f,%f;scrbar_inv;vertical]", ctn_len, ctn_hgt)) From a98b0c08af8a181f95dcaad3b340038f5d8ad113 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Tue, 11 May 2021 00:20:46 +0200 Subject: [PATCH 05/13] Fix crash with pipeworks --- init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/init.lua b/init.lua index fb6a486..b4c66d6 100644 --- a/init.lua +++ b/init.lua @@ -2251,6 +2251,7 @@ local function make_fs(player, data) end function i3.set_fs(player, _fs) + if not player or player.is_fake_player then return end local name = player:get_player_name() local data = pdata[name] if not data then return end From 612039cc2600a05df83a94175509f271bb9d96fa Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Fri, 14 May 2021 03:26:50 +0200 Subject: [PATCH 06/13] Code cleaning --- init.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/init.lua b/init.lua index b4c66d6..7fbeb68 100644 --- a/init.lua +++ b/init.lua @@ -2769,12 +2769,12 @@ if rawget(_G, "awards") then core.register_on_dieplayer(set_fs) end -core.register_on_chatcommand(function(name, command, params) - if sub(command, 1, 5) == "grant" then - params = split(params, " ") +local _privs = {"creative", "teleport", "all"} - for _, v in ipairs(params) do - if find(v, "creative") or find(v, "all") then +core.register_on_chatcommand(function(name, command, params) + if sub(command, 1, 5) == "grant" or sub(command, 1, 6) == "revoke" then + for _, priv in ipairs(_privs) do + if sub(params, -#priv) == priv then local data = pdata[name] reset_data(data) data.favs = {} From 2274b426a59a211a2cff6a77478a8d2157406124 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sat, 15 May 2021 00:18:47 +0200 Subject: [PATCH 07/13] Do things the right way --- init.lua | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/init.lua b/init.lua index 7fbeb68..b4284e0 100644 --- a/init.lua +++ b/init.lua @@ -2769,24 +2769,22 @@ if rawget(_G, "awards") then core.register_on_dieplayer(set_fs) end -local _privs = {"creative", "teleport", "all"} - -core.register_on_chatcommand(function(name, command, params) - if sub(command, 1, 5) == "grant" or sub(command, 1, 6) == "revoke" then - for _, priv in ipairs(_privs) do - if sub(params, -#priv) == priv then - local data = pdata[name] - reset_data(data) - data.favs = {} - break - end - end - end - +core.register_on_chatcommand(function(name) local player = core.get_player_by_name(name) after(0, set_fs, player) end) +core.register_on_priv_grant(function(name, _, priv) + if priv == "creative" or priv == "all" then + local data = pdata[name] + reset_data(data) + data.favs = {} + + local player = core.get_player_by_name(name) + after(0, set_fs, player) + end +end) + i3.register_craft_type("digging", { description = ES"Digging", icon = "i3_steelpick.png", From d612f8697d4b5863f0060e8250d0276ecd9b1ee6 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 31 May 2021 20:11:57 +0200 Subject: [PATCH 08/13] Prevent adding waypoint at same pos --- init.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index b4284e0..f865125 100644 --- a/init.lua +++ b/init.lua @@ -74,7 +74,7 @@ local min, max, floor, ceil, random = local pairs, ipairs, next, type, setmetatable, tonum, unpack, select = pairs, ipairs, next, type, setmetatable, tonumber, unpack, select -local vec_add, vec_mul = vector.add, vector.multiply +local vec_add, vec_mul, vec_eq, vec_round = vector.add, vector.multiply, vector.equals, vector.round local MAX_FAVS = 6 local ITEM_BTN_SIZE = 1.1 @@ -2676,6 +2676,14 @@ i3.new_tab { return elseif fields.waypoint_add then + local pos = player:get_pos() + + for _, v in ipairs(data.waypoints) do + if vec_eq(vec_round(pos), vec_round(v.pos)) then + return msg(name, "You already set a waypoint at this position") + end + end + local waypoint = fields.waypoint_name if fields.waypoint_name == "" then @@ -2683,7 +2691,6 @@ i3.new_tab { end local color = random(0xffffff) - local pos = player:get_pos() local id = player:hud_add { hud_elem_type = "waypoint", From 978c2b23a27c72bda783e18835e120d7b69eba1c Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 31 May 2021 20:48:12 +0200 Subject: [PATCH 09/13] Change the way metadata are saved --- init.lua | 85 +++++++++++++++++++++----------------------------------- 1 file changed, 32 insertions(+), 53 deletions(-) diff --git a/init.lua b/init.lua index f865125..de87766 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,10 @@ i3 = {} +local storage = core.get_mod_storage() +local slz, dslz = core.serialize, core.deserialize +local pdata = dslz(storage:get_string "pdata") or {} + -- Caches -local pdata = {} local init_items = {} local searches = {} local recipes_cache = {} @@ -46,9 +49,7 @@ local get_craft_result = core.get_craft_result local translate = minetest.get_translated_string local on_joinplayer = core.register_on_joinplayer local get_all_recipes = core.get_all_craft_recipes -local slz, dslz = core.serialize, core.deserialize local on_mods_loaded = core.register_on_mods_loaded -local on_leaveplayer = core.register_on_leaveplayer local get_player_info = core.get_player_information local create_inventory = core.create_detached_inventory local on_receive_fields = core.register_on_player_receive_fields @@ -88,8 +89,6 @@ local HUD_TIMER_MAX = 1.5 local MIN_FORMSPEC_VERSION = 4 -local META_SAVES = {"bag_size", "waypoints"} - local BAG_SIZES = { small = INV_SIZE + 3, medium = INV_SIZE + 6, @@ -469,18 +468,6 @@ local function table_replace(t, val, new) end end -local function save_meta(player, entries) - local name = player:get_player_name() - local data = pdata[name] - if not data then return end - - local meta = player:get_meta() - - for _, entry in ipairs(entries) do - meta:set_string(entry, slz(data[entry])) - end -end - local craft_types = {} function i3.register_craft_type(name, def) @@ -2345,27 +2332,19 @@ end local function init_data(player, info) local name = player:get_player_name() - - pdata[name] = { - filter = "", - pagenum = 1, - items = init_items, - items_raw = init_items, - favs = {}, - export_counts = {}, - current_tab = 1, - subcat = 1, - scrbar_inv = 0, - lang_code = get_lang_code(info), - } - + pdata[name] = pdata[name] or {} local data = pdata[name] - local meta = player:get_meta() - for i = 1, #META_SAVES do - local recover = META_SAVES[i] - data[recover] = dslz(meta:get_string(recover)) - end + data.filter = "" + data.pagenum = 1 + data.items = init_items + data.items_raw = init_items + data.favs = {} + data.export_counts = {} + data.current_tab = 1 + data.subcat = 1 + data.scrbar_inv = 0 + data.lang_code = get_lang_code(info) after(0, set_fs, player) end @@ -2750,7 +2729,6 @@ end if rawget(_G, "skins") then __skinsdb = true - insert(META_SAVES, "skin_id") end if rawget(_G, "awards") then @@ -3089,20 +3067,24 @@ core.register_on_dieplayer(function(player) set_fs(player) end) -on_leaveplayer(function(player) - save_meta(player, META_SAVES) - - local name = player:get_player_name() - pdata[name] = nil -end) +local META_SAVES = { + bag_size = true, + waypoints = true, + skin_id = true, + inv_items = true, + known_recipes = true, +} on_shutdown(function() - local players = get_players() - - for i = 1, #players do - local player = players[i] - save_meta(player, META_SAVES) + for name, v in pairs(pdata) do + for dat in pairs(v) do + if not META_SAVES[dat] then + pdata[name][dat] = nil + end end + end + + storage:set_string("pdata", slz(pdata)) end) on_receive_fields(function(player, formname, fields) @@ -3353,11 +3335,10 @@ if progressive_mode then on_joinplayer(function(player) local name = player:get_player_name() - local meta = player:get_meta() local data = pdata[name] - data.inv_items = dslz(meta:get_string "inv_items") or {} - data.known_recipes = dslz(meta:get_string "known_recipes") or 0 + data.inv_items = data.inv_items or {} + data.known_recipes = data.known_recipes or 0 local items = get_filtered_items(player, data) data.items_raw = items @@ -3367,8 +3348,6 @@ if progressive_mode then init_hud(player, data) end end) - - table_merge(META_SAVES, {"inv_items", "known_recipes"}) end local bag_recipes = { From d392f04c11b5884db1e3b823d38f7d83d71f8ab3 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 31 May 2021 21:35:09 +0200 Subject: [PATCH 10/13] Small fix --- init.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index de87766..9766d6c 100644 --- a/init.lua +++ b/init.lua @@ -75,7 +75,8 @@ local min, max, floor, ceil, random = local pairs, ipairs, next, type, setmetatable, tonum, unpack, select = pairs, ipairs, next, type, setmetatable, tonumber, unpack, select -local vec_add, vec_mul, vec_eq, vec_round = vector.add, vector.multiply, vector.equals, vector.round +local vec_new, vec_add, vec_mul, vec_eq, vec_round = + vector.new, vector.add, vector.multiply, vector.equals, vector.round local MAX_FAVS = 6 local ITEM_BTN_SIZE = 1.1 @@ -2605,7 +2606,7 @@ i3.new_tab { remove(data.waypoints, id) elseif action == "teleport" then - local pos = waypoint.pos + local pos = vec_new(waypoint.pos) pos.y = pos.y + 0.5 player:set_pos(pos) From 6ccfd5226739c2f0567c10beea4fa4fc4c795071 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 31 May 2021 21:50:43 +0200 Subject: [PATCH 11/13] Safe teleport --- init.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/init.lua b/init.lua index 9766d6c..09e6872 100644 --- a/init.lua +++ b/init.lua @@ -2609,7 +2609,10 @@ i3.new_tab { local pos = vec_new(waypoint.pos) pos.y = pos.y + 0.5 + local vel = player:get_velocity() + player:add_velocity(vec_mul(vel, -1)) player:set_pos(pos) + msg(name, fmt("Teleported to %s", clr("#ff0", waypoint.name))) elseif action == "refresh" then From ce9a29de8085b23e781012c31c55016d435197ab Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 31 May 2021 22:46:02 +0200 Subject: [PATCH 12/13] Save player data at interval of 10 min --- init.lua | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index 09e6872..9bffef7 100644 --- a/init.lua +++ b/init.lua @@ -49,6 +49,7 @@ local get_craft_result = core.get_craft_result local translate = minetest.get_translated_string local on_joinplayer = core.register_on_joinplayer local get_all_recipes = core.get_all_craft_recipes +local on_leaveplayer = core.register_on_leaveplayer local on_mods_loaded = core.register_on_mods_loaded local get_player_info = core.get_player_information local create_inventory = core.create_detached_inventory @@ -84,6 +85,9 @@ local ITEM_BTN_SIZE = 1.1 local INV_SIZE = 36 local HOTBAR_COUNT = 9 +-- Players data interval +local SAVE_INTERVAL = 600 + -- Progressive mode local POLL_FREQ = 0.25 local HUD_TIMER_MAX = 1.5 @@ -3079,18 +3083,38 @@ local META_SAVES = { known_recipes = true, } -on_shutdown(function() - for name, v in pairs(pdata) do +local function save_data(player_name) + local _pdata = copy(pdata) + + for name, v in pairs(_pdata) do for dat in pairs(v) do if not META_SAVES[dat] then - pdata[name][dat] = nil + _pdata[name][dat] = nil + + if player_name then + pdata[player_name][dat] = nil -- To free up some memory + end end end end - storage:set_string("pdata", slz(pdata)) + storage:set_string("pdata", slz(_pdata)) +end + +on_leaveplayer(function(player) + local name = player:get_player_name() + save_data(name) end) +on_shutdown(save_data) + +local function routine() + save_data() + after(SAVE_INTERVAL, routine) +end + +after(SAVE_INTERVAL, routine) + on_receive_fields(function(player, formname, fields) if formname ~= "" then return false From dde5148934d7a5bec1b00d199e2fbe88a27222c4 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Sat, 12 Jun 2021 16:44:57 +0200 Subject: [PATCH 13/13] Show a tooltip for waypoints --- init.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 9bffef7..9376fcc 100644 --- a/init.lua +++ b/init.lua @@ -41,6 +41,7 @@ local write_json = core.write_json local get_inv = core.get_inventory local chat_send = core.chat_send_player local show_formspec = core.show_formspec +local pos_to_string = core.pos_to_string local check_privs = core.check_player_privs local globalstep = core.register_globalstep local on_shutdown = core.register_on_shutdown @@ -1973,7 +1974,7 @@ local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlo end end -local function get_waypoint_fs(fs, data, name, yextra, ctn_len) +local function get_waypoint_fs(fs, data, player, yextra, ctn_len) fs(fmt("box[0,%f;4.9,0.6;#bababa25]", yextra + 1.1)) fs("label", 0, yextra + 0.85, ES"Waypoint name:") fs(fmt("field[0.1,%f;4.8,0.6;waypoint_name;;]", yextra + 1.1)) @@ -2005,6 +2006,10 @@ local function get_waypoint_fs(fs, data, name, yextra, ctn_len) fs("label", 0.15, y + 0.33, clr(fmt("#%s", hex), waypoint_name)) + fs("tooltip", 0, y, ctn_len - 2.5, 0.65, + fmt("Name: %s\nPosition:%s", clr("#ff0", v.name), + pos_to_string(v.pos, 0):sub(2,-2):gsub("(%-*%d+)", clr("#ff0", " %1")))) + local del = fmt("waypoint_%u_delete", i) fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", del, PNG.trash, PNG.trash_hover)) fs("image_button", ctn_len - 0.5, yi, icon_size, icon_size, "", del, "") @@ -2020,7 +2025,7 @@ local function get_waypoint_fs(fs, data, name, yextra, ctn_len) fs("image_button", ctn_len - 1.5, yi, icon_size, icon_size, "", vsb, "") fs(fmt("tooltip[%s;%s]", vsb, v.hide and ES"Show waypoint" or ES"Hide waypoint")) - if check_privs(name, {teleport = true}) then + if check_privs(player, {teleport = true}) then local tp = fmt("waypoint_%u_teleport", i) fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", @@ -2132,7 +2137,7 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a end elseif data.subcat == 5 then - get_waypoint_fs(fs, data, name, yextra, ctn_len) + get_waypoint_fs(fs, data, player, yextra, ctn_len) end end