Skip to content

Commit

Permalink
Allow saving the order of items in your inv
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Oct 25, 2023
1 parent 4367da8 commit 2a2737f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
105 changes: 105 additions & 0 deletions mods/ctf/ctf_modebase/player.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
ctf_modebase.player = {}

local simplify_for_saved_stuff = function(iname)
if not iname or iname == "" then return iname end

if iname:match("default:pick_(%a+)") then
return "pick"
elseif iname:match("default:axe_(%a+)") then
return "axe"
elseif iname:match("default:shovel_(%a+)") then
return "shovel"
elseif iname:match("ctf_mode_nade_fight:") then
return "nade_fight_grenade"
end

local mod, match = iname:match("(%a+):sword_(%a+)")

if mod and (mod == "default" or mod == "ctf_melee") and match then
return "sword"
end

return iname
end

function ctf_modebase.player.save_initial_stuff_order(player, soft)
if not ctf_modebase.current_mode then return end

local inv = player:get_inventory()
local meta = player:get_meta()
local ssp = meta:get_string("ctf_modebase:player:initial_stuff_order:"..ctf_modebase.current_mode)

if ssp == "" then
ssp = {}
else
ssp = minetest.deserialize(ssp)
end

local done = {}
for i, s in pairs(inv:get_list("main")) do
if s:get_name() ~= "" then
local k = simplify_for_saved_stuff(s:get_name():match("[^%s]*"))

if not soft or not ssp[k] then
if not done[k] or i < k then
ssp[k] = i
done[k] = true
end
end
end
end

meta:set_string("ctf_modebase:player:initial_stuff_order:"..ctf_modebase.current_mode, minetest.serialize(ssp))
end

local function get_initial_stuff(player, f)
local mode = ctf_modebase:get_current_mode()
if mode and mode.stuff_provider then
Expand All @@ -19,6 +71,8 @@ function ctf_modebase.player.give_initial_stuff(player)
minetest.log("action", "Giving initial stuff to player " .. player:get_player_name())

local inv = player:get_inventory()
local meta = player:get_meta()

local item_level = {}
get_initial_stuff(player, function(item)
local mode = ctf_modebase:get_current_mode()
Expand Down Expand Up @@ -57,6 +111,57 @@ function ctf_modebase.player.give_initial_stuff(player)
inv:remove_item("main", item)
inv:add_item("main", item)
end)

-- Check for new items not yet in the order list
ctf_modebase.player.save_initial_stuff_order(player, true)

local saved_stuff_positions = meta:get_string("ctf_modebase:player:initial_stuff_order:"..ctf_modebase.current_mode)

if saved_stuff_positions == "" then
saved_stuff_positions = {}
else
saved_stuff_positions = minetest.deserialize(saved_stuff_positions)
end

local new = {}
local tmp = {}
local current = inv:get_list("main")
for search, idx in pairs(saved_stuff_positions) do
for sidx, stack in ipairs(current) do
if stack then
local sname = simplify_for_saved_stuff(stack:get_name())

if sname ~= "" and sname:match(search) then
tmp[stack] = idx
current[sidx] = false
end
end
end
end

for stack, idx in pairs(tmp) do
for i = 1, #new+1 do
if new[i] then
if idx < tmp[new[i]] then
table.insert(new, i, stack)
break
end
else
new[i] = stack
break
end
end
end

for _, stack in ipairs(current) do
if stack then
table.insert(new, stack)
end
end

inv:set_list("main", new)
end

end

function ctf_modebase.player.empty_inv(player)
Expand Down
14 changes: 13 additions & 1 deletion mods/other/crafting/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,26 @@ if minetest.global_exists("sfinv") then

local formspec = crafting.make_result_selector(player, { x = 8, y = 3 }, context)
formspec = formspec .. "list[detached:crafting_trash;main;0,3.4;1,1;]" ..
"image[0.05,3.5;0.8,0.8;crafting_trash_icon.png]"
"image[0.05,3.5;0.8,0.8;crafting_trash_icon.png]" ..
"image_button[1,3.4;1,1;crafting_save_icon.png;save_inv_order;]" ..
"tooltip[save_inv_order;Saves the order of the items in your inventory" ..
"\n(Your saved order is used when you respawn, and is per-mode)]"

return sfinv.make_formspec(player, context, formspec, true)
end,
on_player_receive_fields = function(self, player, context, fields)
if crafting.result_select_on_receive_results(player, context, fields) then
sfinv.set_player_inventory_formspec(player)
end

if fields.save_inv_order then
ctf_modebase.player.save_initial_stuff_order(player)

minetest.sound_play("crafting_save_sound", {
to_player = player:get_player_name(),
}, true)
end

return true
end
})
Expand Down
Binary file not shown.
Binary file added mods/other/crafting/textures/crafting_save_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2a2737f

Please sign in to comment.