Inventory sorting: Add option for rejecting items

This commit is contained in:
Jean-Patrick Guerrero 2021-11-08 19:53:00 +01:00
parent c51da4a20b
commit 6f5d0a1760
4 changed files with 39 additions and 4 deletions

View File

@ -35,6 +35,7 @@ i3 = {
bag_size = true,
waypoints = true,
inv_items = true,
reject_items = true,
known_recipes = true,
},
@ -76,7 +77,7 @@ i3.files.groups()
i3.files.inventory()
local storage = core.get_mod_storage()
local fmt, copy, slz, dslz = i3.get("fmt", "copy", "slz", "dslz")
local slz, dslz = i3.get("slz", "dslz")
i3.data = dslz(storage:get_string "data") or {}
@ -92,7 +93,7 @@ local function get_formspec_version(info)
end
local function outdated(name)
local fs = fmt("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]",
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.")
core.show_formspec(name, "i3_outdated", fs)
@ -195,7 +196,7 @@ local function init_hudbar(player)
end
local function save_data(player_name)
local _data = copy(i3.data)
local _data = table.copy(i3.data)
for name, v in pairs(_data) do
for dat in pairs(v) do

View File

@ -446,12 +446,32 @@ local function compress_items(list, start_i)
return new_inv
end
local function reject_items(player, inv, list, rej)
for i = 1, #list do
local stack = list[i]
local name = stack:get_name()
for _, it in ipairs(rej) do
if name == it then
spawn_item(player, stack)
inv:set_stack("main", i, ItemStack(""))
end
end
end
return inv:get_list("main")
end
local function sort_inventory(player, data)
local inv = player:get_inventory()
local list = inv:get_list("main")
local size = inv:get_size("main")
local start_i = data.ignore_hotbar and 10 or 1
if true_table(data.reject_items) then
list = reject_items(player, inv, list, data.reject_items)
end
if data.inv_compress then
list = compress_items(list, start_i)
else

View File

@ -475,6 +475,15 @@ local function show_popup(fs, data)
fs("checkbox", 2.4, 10.5, "cb_reverse_sorting", "Reverse sorting", tostring(data.reverse_sorting))
fs("checkbox", 2.4, 10.95, "cb_auto_sorting", "Automatic sorting", tostring(data.auto_sorting))
fs("checkbox", 5.4, 10.05, "cb_ignore_hotbar", "Ignore hotbar", tostring(data.ignore_hotbar))
for _ = 1, 3 do
fs("box", 5.4, 10.68, 2.4, 0.45, "#707070")
end
fs("style[reject_items;font_size=15;font=mono;textcolor=#bddeff]")
fs(fmt("field[5.4,10.68;2.4,0.45;reject_items;Reject items:;%s]",
ESC(concat(data.reject_items or {}, ","))))
fs("field_close_on_enter[reject_items;false]")
end
end
end

View File

@ -3,7 +3,7 @@ local _, get_inventory_fs = i3.files.gui()
local S, clr = i3.get("S", "clr")
local min, ceil, random = i3.get("min", "ceil", "random")
local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases")
local fmt, find, match, sub, lower = i3.get("fmt", "find", "match", "sub", "lower")
local fmt, find, match, sub, lower, split = i3.get("fmt", "find", "match", "sub", "lower", "split")
local vec_new, vec_eq, vec_round = i3.get("vec_new", "vec_eq", "vec_round")
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
@ -55,6 +55,11 @@ i3.new_tab {
skins.set_player_skin(player, _skins[id])
end
if fields.reject_items then
local items = split(fields.reject_items, ",")
data.reject_items = items
end
for field in pairs(fields) do
if sub(field, 1, 4) == "btn_" then
data.subcat = indexof(i3.SUBCAT, sub(field, 5))